File tree Expand file tree Collapse file tree 1 file changed +12
-9
lines changed Expand file tree Collapse file tree 1 file changed +12
-9
lines changed Original file line number Diff line number Diff line change @@ -50,6 +50,7 @@ use crate::vm::VirtualMachine;
50
50
pub struct PyString {
51
51
value : String ,
52
52
hash : AtomicCell < Option < pyhash:: PyHash > > ,
53
+ len : AtomicCell < Option < usize > > ,
53
54
}
54
55
impl ThreadSafe for PyString { }
55
56
@@ -71,6 +72,7 @@ impl From<String> for PyString {
71
72
PyString {
72
73
value : s,
73
74
hash : AtomicCell :: default ( ) ,
75
+ len : AtomicCell :: default ( ) ,
74
76
}
75
77
}
76
78
}
@@ -294,20 +296,21 @@ impl PyString {
294
296
295
297
#[ pymethod( name = "__hash__" ) ]
296
298
fn hash ( & self ) -> pyhash:: PyHash {
297
- match self . hash . load ( ) {
298
- Some ( hash) => hash,
299
- None => {
300
- let hash = pyhash:: hash_value ( & self . value ) ;
301
- self . hash . store ( Some ( hash) ) ;
302
- hash
303
- }
304
- }
299
+ self . hash . load ( ) . unwrap_or_else ( || {
300
+ let hash = pyhash:: hash_value ( & self . value ) ;
301
+ self . hash . store ( Some ( hash) ) ;
302
+ hash
303
+ } )
305
304
}
306
305
307
306
#[ pymethod( name = "__len__" ) ]
308
307
#[ inline]
309
308
fn len ( & self ) -> usize {
310
- self . value . chars ( ) . count ( )
309
+ self . len . load ( ) . unwrap_or_else ( || {
310
+ let len = self . value . chars ( ) . count ( ) ;
311
+ self . len . store ( Some ( len) ) ;
312
+ len
313
+ } )
311
314
}
312
315
313
316
#[ pymethod( name = "__sizeof__" ) ]
You can’t perform that action at this time.
0 commit comments