From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS4713 221.184.0.0/13 X-Spam-Status: No, score=-3.8 required=3.0 tests=AWL,BAYES_00, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED,SPF_PASS shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from neon.ruby-lang.org (neon.ruby-lang.org [221.186.184.75]) by dcvr.yhbt.net (Postfix) with ESMTP id B5BD61F453 for ; Fri, 8 Feb 2019 06:28:45 +0000 (UTC) Received: from neon.ruby-lang.org (localhost [IPv6:::1]) by neon.ruby-lang.org (Postfix) with ESMTP id BE769119151; Fri, 8 Feb 2019 15:28:41 +0900 (JST) Received: from o1678948x4.outbound-mail.sendgrid.net (o1678948x4.outbound-mail.sendgrid.net [167.89.48.4]) by neon.ruby-lang.org (Postfix) with ESMTPS id 0DD1011914F for ; Fri, 8 Feb 2019 15:28:39 +0900 (JST) Received: by filter0061p3mdw1.sendgrid.net with SMTP id filter0061p3mdw1-6291-5C5D2194-7 2019-02-08 06:28:36.215112787 +0000 UTC m=+212490.625600356 Received: from herokuapp.com (ec2-54-226-126-161.compute-1.amazonaws.com [54.226.126.161]) by ismtpd0046p1iad1.sendgrid.net (SG) with ESMTP id 9EZiynq4SOChX8MduPnCtQ for ; Fri, 08 Feb 2019 06:28:36.187 +0000 (UTC) Date: Fri, 08 Feb 2019 06:28:38 +0000 (UTC) From: nobu@ruby-lang.org To: ruby-core@ruby-lang.org Message-ID: References: Mime-Version: 1.0 X-Redmine-MailingListIntegration-Message-Ids: 66941 X-Redmine-Project: ruby-trunk X-Redmine-Issue-Id: 15331 X-Redmine-Issue-Author: alanwu X-Redmine-Sender: nobu X-Mailer: Redmine X-Redmine-Host: bugs.ruby-lang.org X-Redmine-Site: Ruby Issue Tracking System X-Auto-Response-Suppress: All Auto-Submitted: auto-generated X-SG-EID: ync6xU2WACa70kv/Ymy4QrNMhiuLXJG8OTL2vJD1yS6/XbViEDyQdoVD8PckTWGw306PpsIu+KwMTG 7CmRd78ahZDDAB8JCqvlePPdr0GqOPu/OY9O9w9fQRi6SakjJNfkXC1LbzrzIlC2gqBioXDz6VqmnX zJ2gqsEtvZmFVtGLdOhtsxddMxKLSDjJZGX60ZPQrXyjuWtfUrJL0VpFfw== X-ML-Name: ruby-core X-Mail-Count: 91488 Subject: [ruby-core:91488] [Ruby trunk Feature#15331] [PATCH] Faster hashing for short string literals X-BeenThere: ruby-core@ruby-lang.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: Ruby developers List-Id: Ruby developers List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ruby-core-bounces@ruby-lang.org Sender: "ruby-core" 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/