Skip to content

Commit b2dab4a

Browse files
0xdeafbeefzaidoon1
authored andcommitted
Add lto feature
1 parent e882eca commit b2dab4a

File tree

5 files changed

+23
-0
lines changed

5 files changed

+23
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* Expose LRU cache options (athre0z)
77
* Add `with_capacity_bytes` to `WriteBatch` (0xdeafbeef)
88
* Add `set_compaction_pri` to `Options` (0xdeafbeef)
9+
* Add `lto` feature to enable link-time optimization using `linker-plugin-lto` (0xdeafbeef)
910

1011
## [Breaking Changes]
1112
* Update jemalloc-sys to 0.6.0 (0xdeafbeef)

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ multi-threaded-cf = []
3434
serde1 = ["serde"]
3535
bindgen-runtime = ["librocksdb-sys/bindgen-runtime"]
3636
bindgen-static = ["librocksdb-sys/bindgen-static"]
37+
lto = ["librocksdb-sys/lto"]
3738

3839
[dependencies]
3940
libc = "0.2"

README.md

+9
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,12 @@ features = ["bindgen-static", "snappy", "lz4", "zstd", "zlib", "bzip2"]
7777
```
7878

7979
Notice that `runtime` and `static` features are mutually exclusive, and won't compile if both enabled.
80+
81+
## LTO
82+
Enable the `lto` feature to enable link-time optimization. It will compile rocksdb with `-flto` flag. This feature is disabled by default.
83+
84+
> [!IMPORTANT]
85+
> You must use clang as `CC`. Eg. `CC=/usr/bin/clang CXX=/usr/bin/clang++`. Clang llvm version must be the same as the one used by rust compiler.
86+
> On the rust side you should use `RUSTFLAGS="-Clinker-plugin-lto -Clinker=clang -Clink-arg=-fuse-ld=lld"`.
87+
88+
Check the [Rust documentation](https://doc.rust-lang.org/rustc/linker-plugin-lto.html) for more information.

librocksdb-sys/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ zstd = ["zstd-sys"]
2929
zlib = ["libz-sys"]
3030
bzip2 = ["bzip2-sys"]
3131
rtti = []
32+
lto = []
3233

3334
[dependencies]
3435
libc = "0.2"

librocksdb-sys/build.rs

+11
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,17 @@ fn build_rocksdb() {
9090
config.define("USE_RTTI", Some("1"));
9191
}
9292

93+
// https://github.com/facebook/rocksdb/blob/be7703b27d9b3ac458641aaadf27042d86f6869c/Makefile#L195
94+
if cfg!(feature = "lto") {
95+
config.flag("-flto");
96+
if !config.get_compiler().is_like_clang() {
97+
panic!(
98+
"LTO is only supported with clang. Either disable the `lto` feature\
99+
or set `CC=/usr/bin/clang CXX=/usr/bin/clang++` environment variables."
100+
);
101+
}
102+
}
103+
93104
config.include(".");
94105
config.define("NDEBUG", Some("1"));
95106

0 commit comments

Comments
 (0)