ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
From: nobu@ruby-lang.org
To: ruby-core@ruby-lang.org
Subject: [ruby-core:90076] [Ruby trunk Feature#15331] [PATCH] Faster hashing for short string literals
Date: Mon, 26 Nov 2018 13:47:27 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-75200.20181126134725.2741c79725960547@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-15331.20181122220623@ruby-lang.org

Issue #15331 has been updated by nobu (Nobuyoshi Nakada).


alanwu (Alan Wu) wrote:
> I've taken multi-byte terminators into account and updated my implementation. I've also included a test that should warn us about hash clobbering.

Seems nice.

About the macro `BUILTIN_SINGLE_BYTE_ENC_IDX_P`, its name is misleading as it is true for UTF-8.
In common, single-byte encoding means that it uses just one byte per character, 0..255 range, e.g., ASCII-8BIT, US-ASCII, and ISO-8859 family.
UTF-8 is one of mutli-byte encodings.
Also it doesn't cover all non-wchar encodings, so it doesn't seem generic enough to be shared in encindex.h.

> I had to change the hash table interaction for fstring creation a bit to make up the performance difference.
> Apologies for the bigger patch size.

It may be nice to separate `st_update_with_hash` patch.


----------------------------------------
Feature #15331: [PATCH] Faster hashing for short string literals
https://bugs.ruby-lang.org/issues/15331#change-75200

* Author: alanwu (Alan Wu)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
## 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 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)

---Files--------------------------------
string_key_vs_symbol_key.rb (594 Bytes)
0001-Hash-code-memoization-for-short-fstrings.patch (3.62 KB)
0001-Hash-code-memoization-for-short-fstrings-v2.patch (4.29 KB)
0001-Hash-code-memoization-for-short-fstrings-v3.patch (4.36 KB)
0001-Hash-code-memoization-for-short-fstrings-v4.patch (4.31 KB)
0001-Hash-code-memoization-for-short-fstrings-v5.patch (12.9 KB)


-- 
https://bugs.ruby-lang.org/

  parent reply	other threads:[~2018-11-26 13:47 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <redmine.issue-15331.20181122220623@ruby-lang.org>
2018-11-22 22:06 ` [ruby-core:89985] [Ruby trunk Feature#15331] [PATCH] Faster hashing for short string literals alanwucanada
2018-11-23  1:46 ` [ruby-core:89994] " matz
2018-11-23 12:00 ` [ruby-core:90005] " nobu
2018-11-23 18:52 ` [ruby-core:90013] " alanwucanada
2018-11-24  5:35 ` [ruby-core:90022] " alanwucanada
2018-11-25  3:22 ` [ruby-core:90054] " nobu
2018-11-26  4:39 ` [ruby-core:90066] " alanwucanada
2018-11-26 13:47 ` nobu [this message]
2018-11-26 16:32 ` [ruby-core:90077] " alanwucanada
2018-12-05 21:30 ` [ruby-core:90329] " alanwucanada
2019-02-07 10:44 ` [ruby-core:91468] " matz
2019-02-07 16:35 ` [ruby-core:91473] " alanwucanada
2019-02-08  6:28 ` [ruby-core:91488] " nobu
2019-02-16  9:33 ` [ruby-core:91576] " duerst
2019-02-17  3:32 ` [ruby-core:91578] " alanwucanada
2019-02-17 21:11 ` [ruby-core:91580] " ko1

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.ruby-lang.org/en/community/mailing-lists/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=redmine.journal-75200.20181126134725.2741c79725960547@ruby-lang.org \
    --to=ruby-core@ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).