Project

General

Profile

Feature #15331

Updated by alanwu (Alan Wu) about 6 years ago

## Background 

 It's common for applications to use string literals as hash keys, especially 
 for applications that work with YAML or JSON: 

 ``` 
 paylod['name'] 
 ``` 

 At the moment (r65895), hash lookups using a string key is about 30% slower 
 than using a symbol key. 

 ## Proposal 

 We memoize the hash code for short fstrings. There is extra, currently unused 
 space at the end of the RString struct we can use to store the hash code. 
 The unique nature of fstrings makes it so that every user of each fstring benefit 
 from the memoized hash. 

 ## Evaluation 

 The included benchmark hash_aref_fstr.rb hash_aref_str.rb is about 20% faster with this optimization. 
 hash_aref_long_str.rb shows that for long strings which we cannot memoize, there is 
 no visible performance penalty. 

 vm2_freezestring.yml is also not visibly slower after this optimization. 

 I have also attached a bechmark (string_key_vs_symbol_key.rb) that compares hash 
 lookups with symbols against hash lookups with strings. With this optimization, the 
 gap in performance is smaller. (10% slower post patch vs 30% slower on trunk)

Back