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:91488] [Ruby trunk Feature#15331] [PATCH] Faster hashing for short string literals
Date: Fri, 08 Feb 2019 06:28:38 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-76747.20190208062835.2ec30d3256158237@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-15331.20181122220623@ruby-lang.org

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


Isn't 1% a measurement error?
Does it always occur constantly?

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

* Author: alanwu (Alan Wu)
* Status: Closed
* 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)
0001-Add-st_update_with_hash.patch (3.09 KB)
0002-Hash-code-memoization-for-short-fstrings.patch (10 KB)


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

  parent reply	other threads:[~2019-02-08  6:28 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 ` [ruby-core:90076] " nobu
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 ` nobu [this message]
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-76747.20190208062835.2ec30d3256158237@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).