-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feature #19236] When building a large hash, pre-allocating it with enough capacity can save many re-hashes and significantly improve performance. ``` /opt/rubies/3.3.0/bin/ruby --disable=gems -rrubygems -I./benchmark/lib ./benchmark/benchmark-driver/exe/benchmark-driver \ --executables="compare-ruby::../miniruby-master -I.ext/common --disable-gem" \ --executables="built-ruby::./miniruby --disable-gem" \ --output=markdown --output-compare -v $(find ./benchmark -maxdepth 1 -name 'hash_new' -o -name '*hash_new*.yml' -o -name '*hash_new*.rb' | sort) compare-ruby: ruby 3.4.0dev (2024-03-25T11:48:11Z master f53209f) +YJIT dev [arm64-darwin23] last_commit=[ruby/irb] Cache RDoc::RI::Driver.new (ruby/irb#911) built-ruby: ruby 3.4.0dev (2024-03-25T15:29:40Z hash-new-rb 77652b08a2) +YJIT dev [arm64-darwin23] warming up... | |compare-ruby|built-ruby| |:-------------------|-----------:|---------:| |new | 7.614M| 5.976M| | | 1.27x| -| |new_with_capa_1k | 13.931k| 15.698k| | | -| 1.13x| |new_with_capa_100k | 124.746| 148.283| | | -| 1.19x| ```
- Loading branch information
Showing
6 changed files
with
100 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
prelude: | | ||
has_hash_with_capa = Hash.instance_method(:initialize).parameters.include?([:key, :capacity]) | ||
strings_1k = 1_000.times.map { |i| i.to_s.freeze } | ||
strings_100k = 100_000.times.map { |i| i.to_s.freeze } | ||
benchmark: | ||
new: Hash.new | ||
new_with_capa_1k: | | ||
h = has_hash_with_capa ? Hash.new(capacity: strings_1k.size) : {} | ||
strings_1k.each do |x| | ||
h[x] = true | ||
end | ||
new_with_capa_100k: | | ||
h = has_hash_with_capa ? Hash.new(capacity: strings_100k.size) : {} | ||
strings_100k.each do |x| | ||
h[x] = true | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
class Hash | ||
# call-seq: | ||
# Hash.new(default_value = nil) -> new_hash | ||
# Hash.new(default_value = nil, capacity: size) -> new_hash | ||
# Hash.new {|hash, key| ... } -> new_hash | ||
# Hash.new(capacity: size) {|hash, key| ... } -> new_hash | ||
# | ||
# Returns a new empty +Hash+ object. | ||
# | ||
# The initial default value and initial default proc for the new hash | ||
# depend on which form above was used. See {Default Values}[rdoc-ref:Hash@Default+Values]. | ||
# | ||
# If neither an argument nor a block is given, | ||
# initializes both the default value and the default proc to <tt>nil</tt>: | ||
# h = Hash.new | ||
# h.default # => nil | ||
# h.default_proc # => nil | ||
# | ||
# If argument <tt>default_value</tt> is given but no block is given, | ||
# initializes the default value to the given <tt>default_value</tt> | ||
# and the default proc to <tt>nil</tt>: | ||
# h = Hash.new(false) | ||
# h.default # => false | ||
# h.default_proc # => nil | ||
# | ||
# If a block is given but no <tt>default_value</tt>, stores the block as the default proc | ||
# and sets the default value to <tt>nil</tt>: | ||
# h = Hash.new {|hash, key| "Default value for #{key}" } | ||
# h.default # => nil | ||
# h.default_proc.class # => Proc | ||
# h[:nosuch] # => "Default value for nosuch" | ||
# | ||
# If both a block and a <tt>default_value</tt> are given, raises an +ArgumentError+ | ||
# | ||
# If the optional keyword argument +capacity+ is given, the hash will be allocated | ||
# with enough capacity to accomodate this many keys without having to be resized. | ||
def initialize(ifnone = (ifnone_unset = true), capacity: 0, &block) | ||
if Primitive.mandatory_only? | ||
Primitive.attr! :leaf | ||
else | ||
b = block # TODO: builtin bug | ||
Primitive.cstmt!(%{ | ||
rb_hash_init(self, NUM2LONG(capacity), ifnone_unset, ifnone, b); | ||
return Qnil; | ||
}) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters