Skip to content

Commit

Permalink
Avoid memory allocations on critical path.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Feb 1, 2019
1 parent eb82712 commit 914f0f1
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 32 deletions.
2 changes: 1 addition & 1 deletion lib/covered/capture.rb
Expand Up @@ -29,7 +29,7 @@ def initialize(output)

@trace = TracePoint.new(:line, :call) do |event|
if path = event.path
@output.mark(path, event.lineno)
@output.mark(path, event.lineno, 1)
end
end
end
Expand Down
17 changes: 6 additions & 11 deletions lib/covered/files.rb
Expand Up @@ -36,10 +36,10 @@ def empty?
@paths.empty?
end

def mark(path, *args)
def mark(path, lineno, value)
coverage = (@paths[path] ||= Coverage.new(path))

coverage.mark(*args)
coverage.mark(lineno, value)

return coverage
end
Expand Down Expand Up @@ -93,7 +93,7 @@ def accept?(path)
true
end

def mark(path, *args)
def mark(path, lineno, value)
super if accept?(path)
end

Expand All @@ -109,21 +109,16 @@ def each(&block)
end

class Skip < Filter
def initialize(output, pattern, base = "")
def initialize(output, pattern)
super(output)

@pattern = pattern
@base = self.expand_path(base)
end

attr :pattern

def accept? path
if @base
path = relative_path(path)
end

!(@pattern === path)
!@pattern.match?(path)
end
end

Expand Down Expand Up @@ -156,7 +151,7 @@ def expand_path(path)

def relative_path(path)
if path.start_with?(@path)
path[@path.size+1..-1]
path.slice(@path.size+1, path.size)
else
super
end
Expand Down
2 changes: 1 addition & 1 deletion lib/covered/policy/default.rb
Expand Up @@ -25,7 +25,7 @@
root Dir.pwd

# We will ignore any files in the test or spec directory:
skip /^(test|spec|vendor)/
skip /^.*\/(test|spec|vendor)\//

# We will include all files under lib, even if they aren't loaded:
include "lib/**/*.rb"
Expand Down
2 changes: 1 addition & 1 deletion lib/covered/version.rb
Expand Up @@ -19,5 +19,5 @@
# THE SOFTWARE.

module Covered
VERSION = "0.10.1"
VERSION = "0.10.2"
end
4 changes: 2 additions & 2 deletions lib/covered/wrapper.rb
Expand Up @@ -28,8 +28,8 @@ def initialize(output = nil)

attr :output

def mark(*args)
@output.mark(*args) if @output
def mark(path, lineno, value)
@output.mark(path, lineno, value) if @output
end

def enable
Expand Down
2 changes: 1 addition & 1 deletion spec/covered/brief_summary_spec.rb
Expand Up @@ -29,7 +29,7 @@
let(:io) {StringIO.new}

it "can generate partial summary" do
files.mark(__FILE__, 37)
files.mark(__FILE__, 37, 1)
files.mark(__FILE__, 38, 0)

summary.call(files, io)
Expand Down
20 changes: 10 additions & 10 deletions spec/covered/files_spec.rb
Expand Up @@ -21,14 +21,14 @@
RSpec.describe Covered::Files do
context '#mark' do
it "can mark lines" do
subject.mark("program.rb", 2)
subject.mark("program.rb", 2, 1)

expect(subject.paths["program.rb"][2]).to be == 1
end

it "can mark the same line twice" do
2.times do
subject.mark("program.rb", 2)
subject.mark("program.rb", 2, 1)
end

expect(subject.paths["program.rb"][2]).to be == 2
Expand All @@ -37,7 +37,7 @@

context '#each' do
it "enumerates all paths" do
coverage = subject.mark("program.rb", 2)
coverage = subject.mark("program.rb", 2, 1)

enumerator = subject.each
expect(enumerator.next).to be coverage
Expand All @@ -63,7 +63,7 @@
let(:path) {subject.glob.first}

it "should defer to existing files" do
files.mark(path, 5)
files.mark(path, 5, 1)

paths = subject.to_h

Expand All @@ -83,13 +83,13 @@
subject {described_class.new(files, "file.rb")}

it "should ignore files which match given pattern" do
subject.mark("file.rb", 1)
subject.mark("file.rb", 1, 1)

expect(files).to be_empty
end

it "should include files which don't match given pattern" do
subject.mark("foo.rb", 1)
subject.mark("foo.rb", 1, 1)

expect(files).to_not be_empty
expect(subject.to_h).to include("foo.rb")
Expand All @@ -101,13 +101,13 @@
subject {described_class.new(files, "file.rb")}

it "should ignore files which don't match given pattern" do
subject.mark("foo.rb", 1)
subject.mark("foo.rb", 1, 1)

expect(files).to be_empty
end

it "should include files which match given pattern" do
subject.mark("file.rb", 1)
subject.mark("file.rb", 1, 1)

expect(files).to_not be_empty
end
Expand All @@ -118,13 +118,13 @@
subject {described_class.new(files, "lib/")}

it "should ignore files which don't match root" do
subject.mark("foo.rb", 1)
subject.mark("foo.rb", 1, 1)

expect(files).to be_empty
end

it "should include files which match root" do
subject.mark("lib/foo.rb", 1)
subject.mark("lib/foo.rb", 1, 1)

expect(files).to_not be_empty
end
Expand Down
4 changes: 2 additions & 2 deletions spec/covered/partial_summary_spec.rb
Expand Up @@ -29,7 +29,7 @@
let(:io) {StringIO.new}

it "can generate partial summary" do
files.mark(__FILE__, 37)
files.mark(__FILE__, 37, 1)
files.mark(__FILE__, 38, 0)

summary.call(files, io)
Expand All @@ -40,7 +40,7 @@

it "should break segments with elipsis" do
files.mark(__FILE__, 1, 0)
files.mark(__FILE__, 2)
files.mark(__FILE__, 2, 1)

files.mark(__FILE__, 50, 0)

Expand Down
2 changes: 1 addition & 1 deletion spec/covered/summary_spec.rb
Expand Up @@ -29,7 +29,7 @@
let(:io) {StringIO.new}

it "can generate source code listing" do
files.mark(__FILE__, 24)
files.mark(__FILE__, 24, 1)
files.mark(__FILE__, 25, 0)

summary.call(files, io)
Expand Down
4 changes: 2 additions & 2 deletions spec/covered/wrapper_spec.rb
Expand Up @@ -25,9 +25,9 @@
subject {described_class.new(output)}

it 'passes #mark through' do
expect(output).to receive(:mark).with("fleeb.rb", 5)
expect(output).to receive(:mark).with("fleeb.rb", 5, 1)

subject.mark("fleeb.rb", 5)
subject.mark("fleeb.rb", 5, 1)
end

it 'passes #enable through' do
Expand Down

0 comments on commit 914f0f1

Please sign in to comment.