Skip to content

Commit

Permalink
Simplify unaligned write for pre-computed string hash
Browse files Browse the repository at this point in the history
  • Loading branch information
XrXr committed Jun 13, 2024
1 parent e8bb729 commit 85d5f5c
Showing 1 changed file with 2 additions and 3 deletions.
5 changes: 2 additions & 3 deletions string.c
Original file line number Diff line number Diff line change
Expand Up @@ -376,9 +376,8 @@ str_precompute_hash(VALUE str)
RUBY_ASSERT(free_bytes >= sizeof(st_index_t));
#endif

typedef struct {char bytes[sizeof(st_index_t)];} unaligned_index;
union {st_index_t i; unaligned_index b;} u = {.i = str_do_hash(str)};
*(unaligned_index *)(RSTRING_END(str) + TERM_LEN(str)) = u.b;
st_index_t hash = str_do_hash(str);
memcpy(RSTRING_END(str) + TERM_LEN(str), &hash, sizeof(hash));

FL_SET(str, STR_PRECOMPUTED_HASH);

Expand Down

0 comments on commit 85d5f5c

Please sign in to comment.