Project

General

Profile

Actions

Bug #19875

open

Ruby 3.0 -> 3.1 Performance regression in String#count

Added by iz (Illia Zub) 8 months ago. Updated 7 months ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:114703]

Description

String#count became slower since Ruby 3.1. Originally found by @Freaky: https://github.com/ruby/ruby/pull/4001#issuecomment-1714779781

Compared using the benchmark-driver gem.

$ benchmark-driver tmp/string_count_benchmark_driver.yml --rbenv '3.1.1;3.1.4;2.7.2;3.2.2;3.0.6'                                                 
Calculating -------------------------------------
                          3.1.1       3.1.4       2.7.2       3.2.2       3.0.6
               count    465.804     463.741     865.783     462.711     857.395 i/s -     10.000k times in 21.468251s 21.563768s 11.550239s 21.611783s 11.663235s

Comparison:
                            count
               2.7.2:       865.8 i/s 
               3.0.6:       857.4 i/s - 1.01x  slower
               3.1.1:       465.8 i/s - 1.86x  slower
               3.1.4:       463.7 i/s - 1.87x  slower
               3.2.2:       462.7 i/s - 1.87x  slower

Benchmark:

$ cat ./tmp/string_count_benchmark_driver.yml 
loop_count: 10_000
prelude: |
  html = "\nruby\n" * 1024 * 1024
benchmark:
  count: html.count($/)

Initially, I noticed the difference between str.count($/) and str.lines.size when working on the performance improvement: https://serpapi.com/blog/lines-count-failed-deployments/


Files

rb_str_len.fast (31.9 KB) rb_str_len.fast rb_str_count disassembly (4001 reverted) Freaky (Thomas Hurst), 09/15/2023 04:19 AM
rb_str_len.slow (34 KB) rb_str_len.slow rb_str_count disassembly (master) Freaky (Thomas Hurst), 09/15/2023 04:19 AM
revert-4001.patch (1.71 KB) revert-4001.patch string.c 4001 reversion patch Freaky (Thomas Hurst), 09/15/2023 04:31 AM
rb_str_count.S (11.8 KB) rb_str_count.S nobu (Nobuyoshi Nakada), 09/15/2023 06:09 AM
bytecount.c (7.23 KB) bytecount.c missing/bytecount.c Freaky (Thomas Hurst), 09/19/2023 11:06 PM
Actions

Also available in: Atom PDF

Like0
Like0Like1Like0Like1Like0Like0Like0Like0Like0Like0Like0Like0Like0Like1Like0Like0Like0Like0Like1