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

Use size to set array capa when possible #444

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions benchmark/bm_enum_to_a_sized.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(2**15).times do |i|
ary = (0...i).to_a
end
16 changes: 16 additions & 0 deletions benchmark/bm_enum_to_a_unsized.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class SizelessEnum
include Enumerable

def initialize(n)
@n = n
end

def each
@n.times { |i| yield i }
end

end

(2**15).times do |i|
ary = SizelessEnum.new(i).to_a
end
3 changes: 2 additions & 1 deletion enum.c
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,8 @@ enum_flat_map(VALUE obj)
static VALUE
enum_to_a(int argc, VALUE *argv, VALUE obj)
{
VALUE ary = rb_ary_new();
VALUE size = rb_check_funcall(obj, id_size, 0, 0);
VALUE ary = rb_ary_new2(size == Qundef ? RARRAY_EMBED_LEN_MAX : NUM2LONG(size));

rb_block_call(obj, id_each, argc, argv, collect_all, ary);
OBJ_INFECT(ary, obj);
Expand Down