Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Let empty arrays being flattened not allocate additional state #2448

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

methodmissing
Copy link
Contributor

@methodmissing methodmissing commented Sep 10, 2019

Do not alloc the following for empty Arrays being flattened:

  • Stack array
  • Memo number table
#array_flatten.yml
prelude: |
  ary = []

benchmark:
  flatten!: ary.flatten!
  flatten: ary.flatten
loop_count: 1000000
lourens@CarbonX1:~/src/ruby/ruby$ /usr/local/bin/ruby --disable=gems -rrubygems -I./benchmark/lib ./benchmark/benchmark-driver/exe/benchmark-driver             --executables="compare-ruby::~/src/ruby/trunk/ruby --disable=gems -I.ext/common --disable-gem"             --executables="built-ruby::./miniruby -I./lib -I. -I.ext/common  -r./prelude --disable-gem" -v --repeat-count=24 $HOME/src/array_flatten.yml
compare-ruby: ruby 2.7.0dev (2019-09-22T17:21:54Z master 142efba93e) [x86_64-linux]
built-ruby: ruby 2.7.0dev (2019-09-22T18:10:18Z opt-flatten-empty-.. ae17889e1e) [x86_64-linux]
Calculating -------------------------------------
                     compare-ruby  built-ruby 
            flatten!       6.234M     31.453M i/s -      1.000M times in 0.160418s 0.031794s
             flatten       6.271M     31.324M i/s -      1.000M times in 0.159468s 0.031924s

Comparison:
                         flatten!
          built-ruby:  31452589.1 i/s 
        compare-ruby:   6233726.0 i/s - 5.05x  slower

                          flatten
          built-ruby:  31324068.7 i/s 
        compare-ruby:   6270832.4 i/s - 5.00x  slower

@nobu
Copy link
Member

nobu commented Oct 10, 2019

Doesn't it let Array#flatten! make an unused empty array?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants