Precompute embedded string literals hash code#10596
Conversation
5281aa9 to
303929b
Compare
This comment has been minimized.
This comment has been minimized.
string.c
Outdated
| RUBY_ASSERT(free_bytes >= sizeof(st_index_t)); | ||
| #endif | ||
|
|
||
| *(st_index_t *)(RSTRING_END(str) + TERM_LEN(str)) = rb_str_hash(str); |
There was a problem hiding this comment.
I am afraid if this may cause a bus error on some platform. Is it safe?
There was a problem hiding this comment.
this may cause a bus error on some platform
Because of alignment? If it's a concern, I think we could ensure it's always aligned, the overhead would be very slightly larger.
There was a problem hiding this comment.
Yes > because of alignment
It would be safer to use aligned address if possible.
There was a problem hiding this comment.
Thanks, I'll look into it.
There was a problem hiding this comment.
| *(st_index_t *)(RSTRING_END(str) + TERM_LEN(str)) = rb_str_hash(str); | |
| typedef struct {char bytes[sizeof(st_index_t)];} unaligned_index; | |
| union {st_index_t i; unaligned_index b;} u = {.i = rb_str_hash(str)}; | |
| *(unaligned_index *)(RSTRING_END(str) + TERM_LEN(str)) = u.b; |
There was a problem hiding this comment.
Thank @nobu, interesting trick. I suppose we need it on read as well?
303929b to
54c8bda
Compare
|
Looks like we broke something after the refactor, I'll investigate. |
54c8bda to
6255749
Compare
| if (FL_TEST_RAW(str, STR_PRECOMPUTED_HASH)) { | ||
| real_size += sizeof(st_index_t); | ||
| } |
There was a problem hiding this comment.
This was the bug. The compactor thought the string could fit in a smaller slot, corrupting the precomputed hash.
2bd83d4 to
8f949a4
Compare
|
Alright, I think the bugs have been squashed now. |
8f949a4 to
39fae7c
Compare
39fae7c to
44bc1f4
Compare
With embedded strings we often have some space left in the slot, which we can use to store the string Hash code. It's probably only worth it for string literals, as they are the ones likely to be used as hash keys. We chose to store the Hash code right after the string terminator as to make it easy/fast to compute, and not require one more union in RString. ``` compare-ruby: ruby 3.4.0dev (2024-04-22T06:32:21Z main f77618c) [arm64-darwin23] built-ruby: ruby 3.4.0dev (2024-04-22T10:13:03Z interned-string-ha.. 8a1a32331b) [arm64-darwin23] last_commit=Precompute embedded string literals hash code | |compare-ruby|built-ruby| |:-----------|-----------:|---------:| |symbol | 39.275M| 39.753M| | | -| 1.01x| |dyn_symbol | 37.348M| 37.704M| | | -| 1.01x| |small_lit | 29.514M| 33.948M| | | -| 1.15x| |frozen_lit | 27.180M| 33.056M| | | -| 1.22x| |iseq_lit | 27.391M| 32.242M| | | -| 1.18x| ``` Co-Authored-By: Étienne Barrié <etienne.barrie@gmail.com>
44bc1f4 to
85b5a30
Compare
[Feature #20415]
With embedded strings we often have some space left in the slot, which we can use to store the string Hash code.
It's probably only worth it for string literals, as they are the ones likely to be used as hash keys.
We chose to store the Hash code right after the string terminator as to make it easy/fast to compute, and not require one more union in RString.