rustc: Cache constant C strings. Closes #2264

This commit is contained in:
Brian Anderson 2012-04-21 13:23:25 -07:00
parent bef5cd8e45
commit 734494a04d
2 changed files with 11 additions and 0 deletions

View File

@ -5024,6 +5024,7 @@ fn trans_crate(sess: session::session, crate: @ast::crate, tcx: ty::ctxt,
monomorphized: map::hashmap(hash_mono_id, {|a, b| a == b}),
type_use_cache: util::common::new_def_hash(),
vtables: map::hashmap(hash_mono_id, {|a, b| a == b}),
const_cstr_cache: map::str_hash(),
module_data: str_hash::<ValueRef>(),
lltypes: ty::new_ty_hash(),
names: new_namegen(),

View File

@ -99,6 +99,8 @@ type crate_ctxt = {
type_use_cache: hashmap<ast::def_id, [type_use::type_uses]>,
// Cache generated vtables
vtables: hashmap<mono_id, ValueRef>,
// Cache of constant strings,
const_cstr_cache: hashmap<str, ValueRef>,
module_data: hashmap<str, ValueRef>,
lltypes: hashmap<ty::t, TypeRef>,
names: namegen,
@ -762,6 +764,11 @@ fn C_u8(i: uint) -> ValueRef { ret C_integral(T_i8(), i as u64, False); }
// This is a 'c-like' raw string, which differs from
// our boxed-and-length-annotated strings.
fn C_cstr(cx: @crate_ctxt, s: str) -> ValueRef {
alt cx.const_cstr_cache.find(s) {
some(llval) { ret llval; }
none { }
}
let sc = str::as_c_str(s) {|buf|
llvm::LLVMConstString(buf, str::len(s) as c_uint, False)
};
@ -771,6 +778,9 @@ fn C_cstr(cx: @crate_ctxt, s: str) -> ValueRef {
llvm::LLVMSetInitializer(g, sc);
llvm::LLVMSetGlobalConstant(g, True);
lib::llvm::SetLinkage(g, lib::llvm::InternalLinkage);
cx.const_cstr_cache.insert(s, g);
ret g;
}