@@ -26,18 +26,6 @@ impl<'gcc, 'tcx> CodegenCx<'gcc, 'tcx> {
26
26
bytes_in_context ( self , bytes)
27
27
}
28
28
29
- fn const_cstr ( & self , symbol : Symbol , _null_terminated : bool ) -> LValue < ' gcc > {
30
- // TODO(antoyo): handle null_terminated.
31
- if let Some ( & value) = self . const_cstr_cache . borrow ( ) . get ( & symbol) {
32
- return value;
33
- }
34
-
35
- let global = self . global_string ( symbol. as_str ( ) ) ;
36
-
37
- self . const_cstr_cache . borrow_mut ( ) . insert ( symbol, global) ;
38
- global
39
- }
40
-
41
29
fn global_string ( & self , string : & str ) -> LValue < ' gcc > {
42
30
// TODO(antoyo): handle non-null-terminated strings.
43
31
let string = self . context . new_string_literal ( & * string) ;
@@ -171,8 +159,12 @@ impl<'gcc, 'tcx> ConstMethods<'tcx> for CodegenCx<'gcc, 'tcx> {
171
159
}
172
160
173
161
fn const_str ( & self , s : Symbol ) -> ( RValue < ' gcc > , RValue < ' gcc > ) {
174
- let len = s. as_str ( ) . len ( ) ;
175
- let cs = self . const_ptrcast ( self . const_cstr ( s, false ) . get_address ( None ) ,
162
+ let s_str = s. as_str ( ) ;
163
+ let str_global = * self . const_str_cache . borrow_mut ( ) . entry ( s) . or_insert_with ( || {
164
+ self . global_string ( s_str)
165
+ } ) ;
166
+ let len = s_str. len ( ) ;
167
+ let cs = self . const_ptrcast ( str_global. get_address ( None ) ,
176
168
self . type_ptr_to ( self . layout_of ( self . tcx . types . str_ ) . gcc_type ( self , true ) ) ,
177
169
) ;
178
170
( cs, self . const_usize ( len as u64 ) )
0 commit comments