Skip to content

Optimized String#each_char. #2914

Merged
merged 3 commits into from Jan 29, 2014

2 participants

@razielgn

Fixes #2911.
Related to #2907.

See full commit messages for further explanation.

razielgn added some commits Jan 29, 2014
@razielgn razielgn Removed unused variable in String#chomp! a453225
@razielgn razielgn Partially rewrote String#tr_trans.
To accomodate a future optimiziation of String#each_char, we can no
longer allow String#tr_trans to modify itself when consuming results
form String#each_char.

The implementation was changed so that String#tr_trans would modify
a copy and eventually replacing self with it at the end.

Performance-wise, it looks to be around the same.

rubinius/benchmark - core/string/bench_tr.rb

Before:
    Empty string tr!   917008.9 (±2.4%) i/s -    4608414 in   5.028454s
Medium string tr! with empty arguments
                       130328.5 (±4.1%) i/s -     654472 in   5.032046s
Medium string tr! with non match
                        43451.6 (±1.6%) i/s -     220158 in   5.068048s
Medium string tr! with match
                         5192.4 (±1.7%) i/s -      25960 in   5.001121s
Medium string tr! with a long from_str
                         4693.6 (±11.3%) i/s -     23296 in   5.042575s
Medium string tr! with a long non matching from_str
                       108954.2 (±9.3%) i/s -     546517 in   5.069955s
Long string tr! with empty arguments
                        15019.8 (±3.2%) i/s -      75888 in   5.058223s
Long string tr! with non match
                         3298.0 (±1.2%) i/s -      16677 in   5.057421s
Long string tr! with match
                           79.5 (±2.5%) i/s -        402 in   5.056783s
Long string tr! with a long from_str
                           79.5 (±2.5%) i/s -        402 in   5.059238s
Long string tr! with a long non matching from_str
                        15155.9 (±1.2%) i/s -      76500 in   5.048270s
     Empty string tr   874703.1 (±2.6%) i/s -    4398485 in   5.032067s

After:
    Empty string tr!   879563.1 (±1.4%) i/s -    4425876 in   5.032910s
Medium string tr! with empty arguments
                       125491.5 (±0.9%) i/s -     628004 in   5.004786s
Medium string tr! with non match
                        41907.5 (±1.9%) i/s -     212760 in   5.078778s
Medium string tr! with match
                         5270.0 (±2.4%) i/s -      26675 in   5.064785s
Medium string tr! with a long from_str
                         5122.8 (±1.5%) i/s -      25806 in   5.038598s
Medium string tr! with a long non matching from_str
                       117655.0 (±1.4%) i/s -     598104 in   5.084501s
Long string tr! with empty arguments
                        14969.0 (±3.7%) i/s -      75000 in   5.018538s
Long string tr! with non match
                         4541.4 (±1.2%) i/s -      22950 in   5.054259s
Long string tr! with match
                           80.9 (±2.5%) i/s -        408 in   5.042676s
Long string tr! with a long from_str
                           80.1 (±2.5%) i/s -        406 in   5.071241s
Long string tr! with a long non matching from_str
                        14958.9 (±1.1%) i/s -      76092 in   5.087308s
     Empty string tr   811123.2 (±0.8%) i/s -    4112058 in   5.069918s
cf644a5
@razielgn razielgn Improved performance of String#each_char.
Thanks to @dbussink for hinting about this solution.

rubinius-benchmark - core/string/bench_each_char.rb

Before:
  String#each_char mixed UTF-8 string
                          36420.2 (±2.2%) i/s -     185076 in   5.084328s

After:
  String#each_char mixed UTF-8 string
                          95467.5 (±2.3%) i/s -     482496 in   5.056702s
c9a5f5d
@dbussink dbussink merged commit 848d923 into rubinius:master Jan 29, 2014

1 check passed

Details default The Travis CI build passed
@razielgn razielgn deleted the unknown repository branch Jan 29, 2014
@razielgn

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.