ruby/benchmark
Jeremy Evans 2217e08340
Optimize compilation of large literal arrays
To avoid stack overflow, Ruby splits compilation of large arrays
into smaller arrays, and concatenates the small arrays together.
It previously used newarray/concatarray for this, which is
inefficient.  This switches the compilation to use pushtoarray,
which is much faster. This makes almost all literal arrays only
allocate a single array.

For cases where there is a large amount of static values in the
array, Ruby will statically compile subarrays, and previously
added them using concatarray.  This switches to concattoarray,
avoiding an array allocation for the append.

Keyword splats are also supported in arrays, and ignored if the
keyword splat is empty.  Previously, this used newarraykwsplat and
concatarray.  This still uses newarraykwsplat, but switches to
concattoarray to save an allocation.  So large arrays with keyword
splats can allocate 2 arrays instead of 1.

Previously, for the following array sizes (assuming local variable
access for each element), Ruby allocated the following number of
arrays:

  1000 elements: 7 arrays
 10000 elements: 79 arrays
100000 elements: 781 arrays

With these changes, only a single array is allocated (or 2 for a
large array with a keyword splat.

Results using the included benchmark:

```
                       array_1000
            miniruby:     34770.0 i/s
   ./miniruby-before:     10511.7 i/s - 3.31x  slower

                      array_10000
            miniruby:      4938.8 i/s
   ./miniruby-before:       483.8 i/s - 10.21x  slower

                     array_100000
            miniruby:       727.2 i/s
   ./miniruby-before:         4.1 i/s - 176.98x  slower
```

Co-authored-by: Nobuyoshi Nakada <nobu@ruby-lang.org>
2024-01-27 10:16:52 -08:00
..
gc
lib Remove MJIT-specific benchmarks 2023-03-06 22:36:57 -08:00
other-lang
README.md
app_answer.rb
app_aobench.rb
app_erb.yml
app_factorial.rb
app_fib.rb
app_lc_fizzbuzz.rb
app_mandelbrot.rb
app_pentomino.rb
app_raise.rb
app_strconcat.rb
app_tak.rb
app_tarai.rb
app_uri.rb
array_flatten.yml
array_intersection.yml
array_large_literal.yml Optimize compilation of large literal arrays 2024-01-27 10:16:52 -08:00
array_max_float.yml
array_max_int.yml
array_max_str.yml
array_min.yml
array_sample.yml
array_sample_100k_10.rb
array_sample_100k_11.rb
array_sample_100k__1k.rb
array_sample_100k__6k.rb
array_sample_100k__100.rb
array_sample_100k___10k.rb
array_sample_100k___50k.rb
array_shift.rb
array_small_and.rb
array_small_diff.rb
array_small_or.rb
array_sort_block.rb
array_sort_float.rb
array_sort_int.yml Introduce BOP_CMP for optimized comparison 2022-12-06 12:37:23 -08:00
array_values_at_int.rb
array_values_at_range.rb
attr_accessor.yml
bighash.rb
buffer_each.yml Add several new methods for getting and setting buffer contents. (#6434) 2022-09-26 18:06:12 +13:00
buffer_get.yml Add several new methods for getting and setting buffer contents. (#6434) 2022-09-26 18:06:12 +13:00
cgi_escape_html.yml Improve HTML escape benchmarks 2022-11-04 23:54:25 -07:00
complex_float_add.yml
complex_float_div.yml
complex_float_mul.yml
complex_float_new.yml
complex_float_power.yml
complex_float_sub.yml
constant_invalidation.rb
dir_empty_p.rb
enum_lazy_flat_map.yml
enum_lazy_grep_v_20.rb
enum_lazy_grep_v_50.rb
enum_lazy_grep_v_100.rb
enum_lazy_uniq_20.rb
enum_lazy_uniq_50.rb
enum_lazy_uniq_100.rb
enum_lazy_zip.yml
enum_minmax.yml Introduce BOP_CMP for optimized comparison 2022-12-06 12:37:23 -08:00
enum_sort.yml Introduce BOP_CMP for optimized comparison 2022-12-06 12:37:23 -08:00
enum_sort_by.yml [Feature #19643] Direct primitive compare sort for `Array#sort_by` 2023-05-20 19:40:27 +09:00
enum_tally.yml
erb_escape_html.yml Improve HTML escape benchmarks 2022-11-04 23:54:25 -07:00
erb_render.yml
fiber_chain.yml
fiber_locals.yml
file_chmod.rb
file_rename.rb
float_methods.yml
float_neg_posi.yml
float_to_s.yml
hash_aref_array.rb
hash_aref_dsym.rb
hash_aref_dsym_long.rb
hash_aref_fix.rb
hash_aref_flo.rb
hash_aref_miss.rb
hash_aref_str.rb
hash_aref_sym.rb
hash_aref_sym_long.rb
hash_defaults.yml
hash_dup.yml
hash_first.yml
hash_flatten.rb
hash_ident_flo.rb
hash_ident_num.rb
hash_ident_obj.rb
hash_ident_str.rb
hash_ident_sym.rb
hash_keys.rb
hash_literal_small2.rb
hash_literal_small4.rb
hash_literal_small8.rb
hash_long.rb
hash_shift.rb
hash_shift_u16.rb
hash_shift_u24.rb
hash_shift_u32.rb
hash_small2.rb
hash_small4.rb
hash_small8.rb
hash_to_proc.rb
hash_values.rb
int_quo.rb
io_copy_stream_write.rb
io_copy_stream_write_socket.rb
io_file_create.rb
io_file_read.rb
io_file_write.rb
io_nonblock_noex.rb
io_nonblock_noex2.rb
io_pipe_rw.rb
io_select.rb
io_select2.rb
io_select3.rb
io_write.rb
irb_color.yml
irb_exec.yml
iseq_load_from_binary.yml
ivar_extend.yml
kernel_clone.yml
kernel_float.yml
kernel_tap.yml
kernel_then.yml
keyword_arguments.yml
loop_each.yml Rewrite Array#each in Ruby using Primitive (#9533) 2024-01-23 20:09:57 +00:00
loop_for.rb
loop_generator.rb Rewrite Kernel#loop in Ruby (#6983) 2022-12-25 21:46:29 -08:00
loop_times.rb
loop_times_megamorphic.yml YJIT: Allow inlining ISEQ calls with a block (#9622) 2024-01-23 19:36:23 +00:00
loop_whileloop.rb
loop_whileloop2.rb
marshal_dump_flo.rb
marshal_dump_load_geniv.rb
marshal_dump_load_integer.yml Optimize Marshal dump/load for large (> 31-bit) FIXNUM (#6229) 2022-08-15 16:14:12 -07:00
marshal_dump_load_time.rb
masgn.yml Update multiple assignment benchmarks to include non-literal array cases 2022-08-09 22:19:46 -07:00
match_gt4.rb
match_small.rb
method_bind_call.yml
module_eqq.yml
nil_p.yml
nilclass.yml
num_zero_p.yml
numeric_methods.yml Improve performance some `Integer` and `Float` methods [Feature #19085] (#6638) 2022-10-27 09:13:16 -07:00
object_allocate.yml
objspace_dump_all.yml
pm_array.yml
ractor_const.yml
ractor_float_to_s.yml
range_bsearch_bignum.yml Add benchmarks for Range#bsearch 2023-09-26 17:31:10 +09:00
range_bsearch_endpointless.yml Add benchmarks for Range#bsearch 2023-09-26 17:31:10 +09:00
range_bsearch_fixnum.yml Add benchmarks for Range#bsearch 2023-09-26 17:31:10 +09:00
range_count.yml Optimize `Range#count` by using `range_size` if possible 2023-10-05 00:19:55 +09:00
range_last.yml
range_min.yml Introduce BOP_CMP for optimized comparison 2022-12-06 12:37:23 -08:00
range_overlap.yml [Feature #19839] Fix `Range#overlap?` for empty ranges 2023-09-16 17:24:21 +09:00
range_reverse_each.yml Add benchmarks for Range#reverse_each 2023-10-12 17:34:49 +09:00
realpath.yml
regexp_dup.yml Optimize `Regexp#dup` and `Regexp.new(/RE/)` 2023-06-09 20:22:30 +09:00
regexp_new.yml Optimize `Regexp#dup` and `Regexp.new(/RE/)` 2023-06-09 20:22:30 +09:00
require.yml
require_thread.yml
securerandom.rb
so_ackermann.rb
so_array.rb
so_binary_trees.rb
so_concatenate.rb
so_count_words.yml Clean up temporary file, wc.input [ci skip] 2023-10-24 12:30:10 +09:00
so_exception.rb
so_fannkuch.rb
so_fasta.rb
so_k_nucleotide.yml
so_lists.rb
so_mandelbrot.rb
so_matrix.rb
so_meteor_contest.rb Fix spelling (#7405) 2023-02-28 10:05:30 -08:00
so_nbody.rb Make benchmark indentation consistent 2022-08-19 14:44:08 -07:00
so_nested_loop.rb
so_nsieve.rb
so_nsieve_bits.rb
so_object.rb
so_partial_sums.rb
so_pidigits.rb
so_random.rb
so_reverse_complement.yml
so_sieve.rb
so_spectralnorm.rb
string_capitalize.yml
string_casecmp.yml
string_casecmp_p.yml
string_concat.yml Benchmark String interpolation across size pools 2023-01-13 10:31:35 -05:00
string_downcase.yml
string_dup.yml Specialize String#dup 2023-11-20 14:33:20 +01:00
string_index.rb
string_rpartition.yml Make `rb_str_rindex` return byte index 2023-07-09 16:39:28 +09:00
string_scan_re.rb
string_scan_str.rb
string_slice.yml
string_split.yml
string_swapcase.yml
string_upcase.yml
struct_accessor.yml Support tracing of struct member accessor methods 2023-12-07 10:29:33 -08:00
time_at.yml
time_new.yml
time_now.yml
time_parse.yml [Feature #18033] Make Time.new parse time strings 2022-12-16 22:52:59 +09:00
time_strptime.yml
time_subsec.rb
vm_array.yml
vm_attr_ivar.yml
vm_attr_ivar_set.yml
vm_backtrace.rb
vm_bigarray.yml
vm_bighash.yml
vm_block.yml
vm_block_handler.yml
vm_blockparam.yml
vm_blockparam_call.yml
vm_blockparam_pass.yml
vm_blockparam_yield.yml
vm_call_bmethod.yml Speed up calling iseq bmethods 2023-04-25 08:06:16 -07:00
vm_call_method_missing.yml Optimize method_missing calls 2023-04-25 08:06:16 -07:00
vm_call_send_iseq.yml Optimize send calls 2023-04-25 08:06:16 -07:00
vm_call_symproc.yml Optimize symproc calls 2023-04-25 08:06:16 -07:00
vm_case.yml
vm_case_classes.yml
vm_case_lit.yml
vm_clearmethodcache.rb
vm_const.yml New constant caching insn: opt_getconstant_path 2022-09-01 15:20:49 -07:00
vm_cvar.yml
vm_defined_method.yml
vm_dstr.yml
vm_dstr_ary.rb
vm_dstr_bool.rb
vm_dstr_class_module.rb
vm_dstr_digit.rb
vm_dstr_int.rb
vm_dstr_nil.rb
vm_dstr_obj.rb
vm_dstr_obj_def.rb
vm_dstr_str.rb
vm_dstr_sym.rb
vm_ensure.yml
vm_eval.yml
vm_fiber_allocate.yml
vm_fiber_count.yml
vm_fiber_reuse.yml
vm_fiber_reuse_gc.yml
vm_fiber_switch.yml
vm_float_simple.yml
vm_freezeobj.yml Adds a benchmark to measure freezing objects 2022-09-22 10:29:43 -07:00
vm_freezestring.yml
vm_gc.rb
vm_gc_old_full.rb
vm_gc_old_immediate.rb
vm_gc_old_lazy.rb
vm_gc_short_lived.yml
vm_gc_short_with_complex_long.yml
vm_gc_short_with_long.yml
vm_gc_short_with_symbol.yml
vm_gc_wb_ary.yml
vm_gc_wb_ary_promoted.yml
vm_gc_wb_obj.yml
vm_gc_wb_obj_promoted.yml
vm_iclass_super.yml
vm_ivar.yml
vm_ivar_embedded_obj_init.yml Fixes ivar benchmarks to not depend on object allocation 2022-07-15 10:29:42 -04:00
vm_ivar_extended_obj_init.yml Fixes ivar benchmarks to not depend on object allocation 2022-07-15 10:29:42 -04:00
vm_ivar_generic_get.yml Add benchmarks for setting / getting ivars on generics 2022-07-15 13:39:02 -07:00
vm_ivar_generic_set.yml Add benchmarks for setting / getting ivars on generics 2022-07-15 13:39:02 -07:00
vm_ivar_get.yml Fix style on vm_ivar benchmarks (#6379) 2022-09-15 09:39:39 +09:00
vm_ivar_get_unintialized.yml Fix style on vm_ivar benchmarks (#6379) 2022-09-15 09:39:39 +09:00
vm_ivar_ic_miss.yml Update benchmark/vm_ivar_ic_miss.yml 2023-10-24 10:52:06 -07:00
vm_ivar_lazy_set.yml Fix style on vm_ivar benchmarks (#6379) 2022-09-15 09:39:39 +09:00
vm_ivar_memoize.yml vm_getivar: assume the cached shape_id like have a common ancestor 2023-11-03 12:47:43 +01:00
vm_ivar_of_class.yml
vm_ivar_of_class_set.yml
vm_ivar_set.yml
vm_ivar_set_on_instance.yml Make benchmark indentation consistent 2022-08-19 14:44:08 -07:00
vm_ivar_set_subclass.yml Fixes ivar benchmarks to not depend on object allocation 2022-07-15 10:29:42 -04:00
vm_length.yml
vm_lvar_cond_set.yml avoid extra dup and pop in compile_op_asgn2 2022-09-22 09:47:13 -07:00
vm_lvar_init.yml
vm_lvar_set.yml
vm_method.yml
vm_method_missing.yml
vm_method_splat_calls.yml Add benchmark for recent optimization to avoid implicit allocations 2023-12-07 11:27:55 -08:00
vm_method_splat_calls2.yml Add benchmark for implicit array/hash allocation reduction changes 2024-01-24 18:25:55 -08:00
vm_method_with_block.yml
vm_module_ann_const_set.yml
vm_module_const_set.yml
vm_mutex.yml
vm_neq.yml
vm_newlambda.yml
vm_not.yml
vm_poly_method.yml
vm_poly_method_ov.yml
vm_poly_same_method.yml
vm_poly_singleton.yml
vm_proc.yml
vm_raise1.yml
vm_raise2.yml
vm_regexp.yml
vm_rescue.yml
vm_send.yml
vm_send_cfunc.yml Optimize cfunc calls for f(*a) and f(*a, **kw) if kw is empty 2023-04-25 08:06:16 -07:00
vm_simplereturn.yml
vm_string_literal.yml
vm_struct_big_aref_hi.yml
vm_struct_big_aref_lo.yml
vm_struct_big_aset.yml
vm_struct_big_href_hi.yml
vm_struct_big_href_lo.yml
vm_struct_big_hset.yml
vm_struct_small_aref.yml
vm_struct_small_aset.yml
vm_struct_small_href.yml
vm_struct_small_hset.yml
vm_super.yml
vm_swap.yml
vm_symbol_block_pass.rb
vm_thread_alive_check.yml
vm_thread_close.rb
vm_thread_condvar1.rb
vm_thread_condvar2.rb
vm_thread_create_join.rb
vm_thread_mutex1.rb
vm_thread_mutex2.rb
vm_thread_mutex3.rb
vm_thread_pass.rb
vm_thread_pass_flood.rb
vm_thread_pipe.rb
vm_thread_queue.rb
vm_thread_sized_queue.rb
vm_thread_sized_queue2.rb
vm_thread_sized_queue3.rb
vm_thread_sized_queue4.rb
vm_thread_sleep.yml
vm_unif1.yml
vm_yield.yml
vm_zsuper.yml

README.md

ruby/benchmark

This directory has benchmark definitions to be run with benchmark_driver.gem.

Normal usage

Execute gem install benchmark_driver and run a command like:

# Run a benchmark script with the ruby in the $PATH
benchmark-driver benchmark/app_fib.rb

# Run benchmark scripts with multiple Ruby executables or options
benchmark-driver benchmark/*.rb -e /path/to/ruby -e '/path/to/ruby --jit'

# Or compare Ruby versions managed by rbenv
benchmark-driver benchmark/*.rb --rbenv '2.5.1;2.6.0-preview2 --jit'

# You can collect many metrics in many ways
benchmark-driver benchmark/*.rb --runner memory --output markdown

# Some are defined with YAML for complex setup or accurate measurement
benchmark-driver benchmark/*.yml

See also:

Usage: benchmark-driver [options] RUBY|YAML...
    -r, --runner TYPE                Specify runner type: ips, time, memory, once, block (default: ips)
    -o, --output TYPE                Specify output type: compare, simple, markdown, record, all (default: compare)
    -e, --executables EXECS          Ruby executables (e1::path1 arg1; e2::path2 arg2;...)
        --rbenv VERSIONS             Ruby executables in rbenv (x.x.x arg1;y.y.y arg2;...)
        --repeat-count NUM           Try benchmark NUM times and use the fastest result or the worst memory usage
        --repeat-result TYPE         Yield "best", "average" or "worst" result with --repeat-count (default: best)
        --alternate                  Alternate executables instead of running the same executable in a row with --repeat-count
        --bundler                    Install and use gems specified in Gemfile
        --filter REGEXP              Filter out benchmarks with given regexp
        --run-duration SECONDS       Warmup estimates loop_count to run for this duration (default: 3)
        --timeout SECONDS            Timeout ruby command execution with timeout(1)
    -v, --verbose                    Verbose mode. Multiple -v options increase visilibity (max: 2)

make benchmark

Using make benchmark, make update-benchmark-driver automatically downloads the supported version of benchmark_driver, and it runs benchmarks with the downloaded benchmark_driver.

# Run all benchmarks with the ruby in the $PATH and the built ruby
make benchmark

# Or compare with specific ruby binary
make benchmark COMPARE_RUBY="/path/to/ruby --jit"

# Run vm benchmarks
make benchmark ITEM=vm

# Run some limited benchmarks in ITEM-matched files
make benchmark ITEM=vm OPTS=--filter=block

# You can specify the benchmark by an exact filename instead of using the default argument:
# ARGS = $$(find $(srcdir)/benchmark -maxdepth 1 -name '*$(ITEM)*.yml' -o -name '*$(ITEM)*.rb')
make benchmark ARGS=benchmark/erb_render.yml

# You can specify any option via $OPTS
make benchmark OPTS="--help"

# With `make benchmark`, some special runner plugins are available:
#   -r peak, -r size, -r total, -r utime, -r stime, -r cutime, -r cstime
make benchmark ITEM=vm_bigarray OPTS="-r peak"