Skip to content

Commit 734494a

Browse files
committed
rustc: Cache constant C strings. Closes #2264
1 parent bef5cd8 commit 734494a

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

src/rustc/middle/trans/base.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5024,6 +5024,7 @@ fn trans_crate(sess: session::session, crate: @ast::crate, tcx: ty::ctxt,
50245024
monomorphized: map::hashmap(hash_mono_id, {|a, b| a == b}),
50255025
type_use_cache: util::common::new_def_hash(),
50265026
vtables: map::hashmap(hash_mono_id, {|a, b| a == b}),
5027+
const_cstr_cache: map::str_hash(),
50275028
module_data: str_hash::<ValueRef>(),
50285029
lltypes: ty::new_ty_hash(),
50295030
names: new_namegen(),

src/rustc/middle/trans/common.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ type crate_ctxt = {
9999
type_use_cache: hashmap<ast::def_id, [type_use::type_uses]>,
100100
// Cache generated vtables
101101
vtables: hashmap<mono_id, ValueRef>,
102+
// Cache of constant strings,
103+
const_cstr_cache: hashmap<str, ValueRef>,
102104
module_data: hashmap<str, ValueRef>,
103105
lltypes: hashmap<ty::t, TypeRef>,
104106
names: namegen,
@@ -762,6 +764,11 @@ fn C_u8(i: uint) -> ValueRef { ret C_integral(T_i8(), i as u64, False); }
762764
// This is a 'c-like' raw string, which differs from
763765
// our boxed-and-length-annotated strings.
764766
fn C_cstr(cx: @crate_ctxt, s: str) -> ValueRef {
767+
alt cx.const_cstr_cache.find(s) {
768+
some(llval) { ret llval; }
769+
none { }
770+
}
771+
765772
let sc = str::as_c_str(s) {|buf|
766773
llvm::LLVMConstString(buf, str::len(s) as c_uint, False)
767774
};
@@ -771,6 +778,9 @@ fn C_cstr(cx: @crate_ctxt, s: str) -> ValueRef {
771778
llvm::LLVMSetInitializer(g, sc);
772779
llvm::LLVMSetGlobalConstant(g, True);
773780
lib::llvm::SetLinkage(g, lib::llvm::InternalLinkage);
781+
782+
cx.const_cstr_cache.insert(s, g);
783+
774784
ret g;
775785
}
776786

0 commit comments

Comments
 (0)