-
Notifications
You must be signed in to change notification settings - Fork 12.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #118636 - h1467792822:dev, r=michaelwoerister
Add the unstable option to reduce the binary size of dynamic library… # Motivation The average length of symbol names in the rust standard library is about 100 bytes, while the average length of symbol names in the C++ standard library is about 65 bytes. In some embedded environments where dynamic library are widely used, rust dynamic library symbol name space hash become one of the key bottlenecks of application, Especially when the existing C/C++ module is reconstructed into the rust module. The unstable option `-Z symbol_mangling_version=hashed` is added to solve the bottleneck caused by too long dynamic library symbol names. ## Test data The following is a set of test data on the ubuntu 18.04 LTS environment. With this plug-in, the space saving rate of dynamic libraries can reach about 20%. The test object is the standard library of rust (built based on Xargo), tokio crate, and hyper crate. The contents of the Cargo.toml file in the construction project of the three dynamic libraries are as follows: ```txt # Cargo.toml [profile.release] panic = "abort" opt-leve="z" codegen-units=1 strip=true debug=true ``` The built dynamic library also removes the `.rustc` segments that are not needed at run time and then compares the size. The detailed data is as follows: 1. libstd.so > | symbol_mangling_version | size | saving rate | > | --- | --- | --- | > | legacy | 804896 || > | hashed | 608288 | 0.244 | > | v0 | 858144 || > | hashed | 608288 | 0.291 | 2. libhyper.so > | symbol_mangling_version(libhyper.so) | symbol_mangling_version(libstd.so) | size | saving rate | > | --- | --- | --- | --- | > | legacy | legacy | 866312 || > | hashed | legacy | 645128 |0.255| > | legacy | hashed | 854024 || > | hashed | hashed | 632840 |0.259|
- Loading branch information
Showing
15 changed files
with
211 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
use crate::v0; | ||
use rustc_data_structures::stable_hasher::{Hash64, HashStable, StableHasher}; | ||
use rustc_hir::def_id::CrateNum; | ||
use rustc_middle::ty::{Instance, TyCtxt}; | ||
|
||
use std::fmt::Write; | ||
|
||
pub(super) fn mangle<'tcx>( | ||
tcx: TyCtxt<'tcx>, | ||
instance: Instance<'tcx>, | ||
instantiating_crate: Option<CrateNum>, | ||
full_mangling_name: impl FnOnce() -> String, | ||
) -> String { | ||
// The symbol of a generic function may be scattered in multiple downstream dylibs. | ||
// If the symbol of a generic function still contains `crate name`, hash conflicts between the | ||
// generic funcion and other symbols of the same `crate` cannot be detected in time during | ||
// construction. This symbol conflict is left over until it occurs during run time. | ||
// In this case, `instantiating-crate name` is used to replace `crate name` can completely | ||
// eliminate the risk of the preceding potential hash conflict. | ||
let crate_num = | ||
if let Some(krate) = instantiating_crate { krate } else { instance.def_id().krate }; | ||
|
||
let mut symbol = "_RNxC".to_string(); | ||
v0::push_ident(tcx.crate_name(crate_num).as_str(), &mut symbol); | ||
|
||
let hash = tcx.with_stable_hashing_context(|mut hcx| { | ||
let mut hasher = StableHasher::new(); | ||
full_mangling_name().hash_stable(&mut hcx, &mut hasher); | ||
hasher.finish::<Hash64>().as_u64() | ||
}); | ||
|
||
push_hash64(hash, &mut symbol); | ||
|
||
symbol | ||
} | ||
|
||
// The hash is encoded based on `base-62` and the final terminator `_` is removed because it does | ||
// not help prevent hash collisions | ||
fn push_hash64(hash: u64, output: &mut String) { | ||
let hash = v0::encode_integer_62(hash); | ||
let hash_len = hash.len(); | ||
let _ = write!(output, "{hash_len}H{}", &hash[..hash_len - 1]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
include ../tools.mk | ||
|
||
# ignore-cross-compile | ||
# only-linux | ||
# only-x86_64 | ||
|
||
NM=nm -D | ||
RLIB_NAME=liba_rlib.rlib | ||
DYLIB_NAME=liba_dylib.so | ||
SO_NAME=libb_dylib.so | ||
BIN_NAME=b_bin | ||
|
||
ifeq ($(UNAME),Darwin) | ||
NM=nm -gU | ||
RLIB_NAME=liba_rlib.rlib | ||
DYLIB_NAME=liba_dylib.dylib | ||
SO_NAME=libb_dylib.dylib | ||
BIN_NAME=b_bin | ||
endif | ||
|
||
ifdef IS_WINDOWS | ||
NM=nm -g | ||
RLIB_NAME=liba_rlib.dll.a | ||
DYLIB_NAME=liba_dylib.dll | ||
SO_NAME=libb_dylib.dll | ||
BIN_NAME=b_bin.exe | ||
endif | ||
|
||
all: | ||
$(RUSTC) -C prefer-dynamic -Z unstable-options -C symbol-mangling-version=hashed -C metadata=foo a_dylib.rs | ||
$(RUSTC) -C prefer-dynamic -Z unstable-options -C symbol-mangling-version=hashed -C metadata=bar a_rlib.rs | ||
$(RUSTC) -C prefer-dynamic -L $(TMPDIR) b_dylib.rs | ||
$(RUSTC) -C prefer-dynamic -L $(TMPDIR) b_bin.rs | ||
|
||
# Check hashed symbol name | ||
|
||
[ "$$($(NM) $(TMPDIR)/$(DYLIB_NAME) | grep -c hello)" -eq "0" ] | ||
[ "$$($(NM) $(TMPDIR)/$(DYLIB_NAME) | grep _RNxC7a_dylib | grep -c ' T ')" -eq "1" ] | ||
|
||
[ "$$($(NM) $(TMPDIR)/$(SO_NAME) | grep b_dylib | grep -c hello)" -eq "1" ] | ||
[ "$$($(NM) $(TMPDIR)/$(SO_NAME) | grep _RNxC6a_rlib | grep -c ' T ')" -eq "1" ] | ||
[ "$$($(NM) $(TMPDIR)/$(SO_NAME) | grep _RNxC7a_dylib | grep -c ' U ')" -eq "1" ] | ||
|
||
[ "$$($(NM) $(TMPDIR)/$(BIN_NAME) | grep _RNxC6a_rlib | grep -c ' U ')" -eq "1" ] | ||
[ "$$($(NM) $(TMPDIR)/$(BIN_NAME) | grep _RNxC7a_dylib | grep -c ' U ')" -eq "1" ] | ||
[ "$$($(NM) $(TMPDIR)/$(BIN_NAME) | grep b_dylib | grep hello | grep -c ' U ')" -eq "1" ] | ||
|
||
$(call RUN,$(BIN_NAME)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#![crate_type="dylib"] | ||
pub fn hello() { | ||
println!("hello dylib"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#![crate_type="rlib"] | ||
|
||
pub fn hello() { | ||
println!("hello rlib"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
extern crate a_rlib; | ||
extern crate a_dylib; | ||
extern crate b_dylib; | ||
|
||
fn main() { | ||
a_rlib::hello(); | ||
a_dylib::hello(); | ||
b_dylib::hello(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#![crate_type="dylib"] | ||
|
||
extern crate a_rlib; | ||
extern crate a_dylib; | ||
|
||
pub fn hello() { | ||
a_rlib::hello(); | ||
a_dylib::hello(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
error: incorrect value `bad-value` for codegen option `symbol-mangling-version` - either `legacy` or `v0` (RFC 2603) was expected | ||
error: incorrect value `bad-value` for codegen option `symbol-mangling-version` - one of: `legacy`, `v0` (RFC 2603), or `hashed` was expected | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
error: incorrect value `` for codegen option `symbol-mangling-version` - either `legacy` or `v0` (RFC 2603) was expected | ||
error: incorrect value `` for codegen option `symbol-mangling-version` - one of: `legacy`, `v0` (RFC 2603), or `hashed` was expected | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
error: codegen option `symbol-mangling-version` requires either `legacy` or `v0` (RFC 2603) (C symbol-mangling-version=<value>) | ||
error: codegen option `symbol-mangling-version` requires one of: `legacy`, `v0` (RFC 2603), or `hashed` (C symbol-mangling-version=<value>) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
error: `-C symbol-mangling-version=hashed` requires `-Z unstable-options` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
// revisions: legacy legacy-ok | ||
// revisions: legacy legacy-ok hashed hashed-ok | ||
// [legacy] compile-flags: -Csymbol-mangling-version=legacy | ||
// [legacy-ok] check-pass | ||
// [legacy-ok] compile-flags: -Zunstable-options -Csymbol-mangling-version=legacy | ||
// [hashed] compile-flags: -Csymbol-mangling-version=hashed | ||
// [hashed-ok] check-pass | ||
// [hashed-ok] compile-flags: -Zunstable-options -Csymbol-mangling-version=hashed | ||
|
||
fn main() {} |