Skip to content

Commit f50db49

Browse files
Resize coverage count arrays in one step
1 parent 0262a27 commit f50db49

2 files changed

Lines changed: 19 additions & 2 deletions

File tree

ext/ruby/coverage/tracer.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,8 +273,8 @@ static void Ruby_Coverage_Tracer_on_line(rb_event_flag_t event, VALUE data, VALU
273273

274274
// Counts are 1-indexed: index 0 is unused (nil), index N is the hit count
275275
// for source line N. Grow the array if necessary.
276-
while (RARRAY_LEN(tracer->last_counts) <= line) {
277-
rb_ary_push(tracer->last_counts, Qnil);
276+
if (RARRAY_LEN(tracer->last_counts) <= line) {
277+
rb_ary_resize(tracer->last_counts, line + 1);
278278
}
279279

280280
VALUE current = rb_ary_entry(tracer->last_counts, line);

test/ruby/coverage/tracer.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,23 @@ def around
112112
expect(counts[1]).to be == 2
113113
end
114114

115+
it "resizes count arrays for sparse high line numbers" do
116+
files = {}
117+
tracer = Ruby::Coverage::Tracer.new{|path, iseq| files[path] ||= []}
118+
119+
tracer.start
120+
121+
path = "/tmp/ruby_coverage_tracer_sparse.rb"
122+
line = 100
123+
Module.new.module_eval("x = 1", path, line)
124+
125+
tracer.stop
126+
127+
counts = files[path]
128+
expect(counts).not.to be_nil
129+
expect(counts[line]).to be == 1
130+
end
131+
115132
it "returns nil from the callback to skip tracking a file" do
116133
files = {}
117134
tracer = Ruby::Coverage::Tracer.new{|path, iseq| nil}

0 commit comments

Comments
 (0)