Compare commits

...
10 Commits
Author SHA1 Message Date
josh eff9a10d0c v1.18.0 2019-10-10 16:29:21 -04:00
josh 6ad3fb0785 bundle update 2019-10-10 15:45:00 -04:00
josh 32e8a55b4e CHANGELOG - add v1.18.0 notes 2019-10-04 15:03:50 -04:00
josh 3733cb6114 Backport 2.x VarSet performance improvements to 1.x branch. 2019-10-04 15:01:32 -04:00
josh 5beddd63ac bundle update 2019-07-02 19:46:30 -04:00
josh 4967f89a76 fix inconsistent type handling in VarSet#expand_varref - #73 2019-04-14 14:10:13 -04:00
josh 7cc68b2ce1 Remove Cache dirty-tracking
It really wasn't necessary since Cache#write is not called often.
2019-04-04 15:20:35 -04:00
josh be058dd18b avoid mkdir() race conditions - close #75 2018-12-11 22:13:33 -05:00
josh ad703a5c84 call Bundler.setup in build_tests_spec.rb 2018-12-09 21:20:16 -05:00
John Lindgren fb5d107581 Reduce recursion of VarSet#expand_varref by expanding vars earlier.
It's common for a build command to contain a reference like:

    ${INCPREFIX}${CPPPATH}

Where ${INCPREFIX} is "-I" and ${CPPPATH} is an array of include
paths.  Without this change, Rscons expands ${INCPREFIX} (and then
tries to expand "-I") repeatedly, once for each path.

This change simply makes Rscons intelligent enough to expand
${INCPREFIX} only once, then join the expanded value to each path.

In a mid-sized rake project, ruby-prof shows that the number of
calls to expand_varref is reduced from ~80,000 to ~30,000., and the
total CPU time spent in expand_varref is reduced by about 30%.

In a larger project, the improvement in real build time is small
but consistent enough to be measurable (~0.5 seconds off a total
of ~20 seconds).
2018-12-07 20:06:18 -05:00
10 changed files with 146 additions and 71 deletions

No files matched your search

+13
View File
@@ -1,5 +1,18 @@
## ChangeLog ## ChangeLog
### v1.18.0
#### Performance Improvements
- Backport 2.x VarSet performance improvements to 1.x branch.
- Remove Cache dirty-tracking
- Reduce recursion of VarSet#expand_varref by expanding vars earlier.
#### Fixes
- #73 - fix inconsistent type handling in VarSet#expand_varref
- #75 - avoid mkdir() race conditions
### v1.17.0 ### v1.17.0
#### New Features #### New Features
+20 -20
View File
@@ -1,36 +1,36 @@
PATH PATH
remote: . remote: .
specs: specs:
rscons (1.17.0) rscons (1.18.0)
json (>= 1.8, < 3.0) json (>= 1.8, < 3.0)
GEM GEM
remote: https://rubygems.org/ remote: https://rubygems.org/
specs: specs:
diff-lcs (1.3) diff-lcs (1.3)
docile (1.1.5) docile (1.3.2)
json (2.1.0) json (2.2.0)
rake (12.3.1) rake (13.0.0)
rdoc (5.1.0) rdoc (6.2.0)
rspec (3.6.0) rspec (3.9.0)
rspec-core (~> 3.6.0) rspec-core (~> 3.9.0)
rspec-expectations (~> 3.6.0) rspec-expectations (~> 3.9.0)
rspec-mocks (~> 3.6.0) rspec-mocks (~> 3.9.0)
rspec-core (3.6.0) rspec-core (3.9.0)
rspec-support (~> 3.6.0) rspec-support (~> 3.9.0)
rspec-expectations (3.6.0) rspec-expectations (3.9.0)
diff-lcs (>= 1.2.0, < 2.0) diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.6.0) rspec-support (~> 3.9.0)
rspec-mocks (3.6.0) rspec-mocks (3.9.0)
diff-lcs (>= 1.2.0, < 2.0) diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.6.0) rspec-support (~> 3.9.0)
rspec-support (3.6.0) rspec-support (3.9.0)
simplecov (0.15.0) simplecov (0.17.1)
docile (~> 1.1.0) docile (~> 1.1)
json (>= 1.8, < 3) json (>= 1.8, < 3)
simplecov-html (~> 0.10.0) simplecov-html (~> 0.10.0)
simplecov-html (0.10.2) simplecov-html (0.10.2)
yard (0.9.12) yard (0.9.20)
PLATFORMS PLATFORMS
ruby ruby
@@ -45,4 +45,4 @@ DEPENDENCIES
yard yard
BUNDLED WITH BUNDLED WITH
1.16.6 1.17.3
+2 -8
View File
@@ -85,18 +85,15 @@ module Rscons
@lookup_checksums = {} @lookup_checksums = {}
end end
# Write the cache to disk to be loaded next time. # Write the cache to disk.
# #
# @return [void] # @return [void]
def write def write
if @dirty || (@cache["version"] != VERSION)
@cache["version"] = VERSION @cache["version"] = VERSION
File.open(CACHE_FILE, "w") do |fh| File.open(CACHE_FILE, "w") do |fh|
fh.puts(JSON.dump(@cache)) fh.puts(JSON.dump(@cache))
end end
end end
@dirty = false
end
# Check if target(s) are up to date. # Check if target(s) are up to date.
# #
@@ -249,7 +246,6 @@ module Rscons
} }
end, end,
} }
@dirty = true
end end
end end
@@ -272,9 +268,8 @@ module Rscons
next if parts[i] == "" next if parts[i] == ""
subpath = File.join(*parts[0, i + 1]) subpath = File.join(*parts[0, i + 1])
unless File.exists?(subpath) unless File.exists?(subpath)
FileUtils.mkdir(subpath) FileUtils.mkdir_p(subpath)
@cache["directories"][subpath] = true @cache["directories"][subpath] = true
@dirty = true
end end
end end
end end
@@ -315,7 +310,6 @@ module Rscons
@cache["targets"] ||= {} @cache["targets"] ||= {}
@cache["directories"] ||= {} @cache["directories"] ||= {}
@lookup_checksums = {} @lookup_checksums = {}
@dirty = false
end end
# Return a file's checksum, or the previously calculated checksum for # Return a file's checksum, or the previously calculated checksum for
+23 -5
View File
@@ -280,6 +280,24 @@ module Rscons
@varset.send(:[], *args) @varset.send(:[], *args)
end end
# Access the value of a construction variable.
#
# @since 1.18.0
#
# This method is similar to #[] but does not make a copy-on-access copy of
# the variable accessed. This means that the returned value is NOT safe to
# be modified by the caller. Thus the caller must guarantee that it does
# not modify the returned value.
#
# @param key [String, Symbol]
# The construction variable name.
#
# @return [Object]
# The construction variable's value.
def get_var(key)
@varset.get_var(key)
end
# Set a construction variable's value. # Set a construction variable's value.
# #
# @see VarSet#[]= # @see VarSet#[]=
@@ -782,7 +800,7 @@ module Rscons
append["LDFLAGS", ["-arch", val]] append["LDFLAGS", ["-arch", val]]
end end
skip = true skip = true
elsif word =~ /^#{self["CPPDEFPREFIX"]}(.*)$/ elsif word =~ /^#{get_var("CPPDEFPREFIX")}(.*)$/
handle["CPPDEFINES", $1] handle["CPPDEFINES", $1]
elsif word == "-include" elsif word == "-include"
if val = words[i + 1] if val = words[i + 1]
@@ -795,11 +813,11 @@ module Rscons
append["LDFLAGS", ["-isysroot", val]] append["LDFLAGS", ["-isysroot", val]]
end end
skip = true skip = true
elsif word =~ /^#{self["INCPREFIX"]}(.*)$/ elsif word =~ /^#{get_var("INCPREFIX")}(.*)$/
handle["CPPPATH", $1] handle["CPPPATH", $1]
elsif word =~ /^#{self["LIBLINKPREFIX"]}(.*)$/ elsif word =~ /^#{get_var("LIBLINKPREFIX")}(.*)$/
handle["LIBS", $1] handle["LIBS", $1]
elsif word =~ /^#{self["LIBDIRPREFIX"]}(.*)$/ elsif word =~ /^#{get_var("LIBDIRPREFIX")}(.*)$/
handle["LIBPATH", $1] handle["LIBPATH", $1]
elsif word == "-mno-cygwin" elsif word == "-mno-cygwin"
append["CCFLAGS", [word]] append["CCFLAGS", [word]]
@@ -848,7 +866,7 @@ module Rscons
# @return [void] # @return [void]
def merge_flags(flags) def merge_flags(flags)
flags.each_pair do |key, val| flags.each_pair do |key, val|
if self[key].is_a?(Array) and val.is_a?(Array) if self.get_var(key).is_a?(Array) and val.is_a?(Array)
self[key] += val self[key] += val
else else
self[key] = val self[key] = val
+64 -29
View File
@@ -11,11 +11,13 @@ module Rscons
append(vars) append(vars)
end end
# Access the value of variable. # Access the value of a variable.
# #
# @param key [String, Symbol] The variable name. # @param key [String, Symbol]
# The variable name.
# #
# @return [Object] The variable's value. # @return [Object]
# The variable's value.
def [](key) def [](key)
if @my_vars.include?(key) if @my_vars.include?(key)
@my_vars[key] @my_vars[key]
@@ -30,11 +32,38 @@ module Rscons
end end
end end
# Access the value of a variable.
#
# This method is similar to #[] but does not make a copy-on-access copy of
# the variable accessed. This means that the returned value is NOT safe to
# be modified by the caller. Thus the caller must guarantee that it does
# not modify the returned value.
#
# @param key [String, Symbol]
# The variable name.
#
# @return [Object]
# The variable's value.
def get_var(key)
if @my_vars.include?(key)
@my_vars[key]
else
@coa_vars.each do |coa_vars|
if coa_vars.include?(key)
return coa_vars[key]
end
end
nil
end
end
# Assign a value to a variable. # Assign a value to a variable.
# #
# @param key [String, Symbol] The variable name. # @param key [String, Symbol]
# The variable name.
# #
# @param val [Object] The value to set. # @param val [Object]
# The value to set.
def []=(key, val) def []=(key, val)
@my_vars[key] = val @my_vars[key] = val
end end
@@ -94,36 +123,42 @@ module Rscons
# @return [nil, String, Array, Symbol, TrueClass, FalseClass] # @return [nil, String, Array, Symbol, TrueClass, FalseClass]
# Expanded value with "$!{var}" variable references replaced. # Expanded value with "$!{var}" variable references replaced.
def expand_varref(varref, lambda_args) def expand_varref(varref, lambda_args)
if varref.is_a?(String) case varref
when String
if varref =~ /^(.*)\$\{([^}]+)\}(.*)$/ if varref =~ /^(.*)\$\{([^}]+)\}(.*)$/
prefix, varname, suffix = $1, $2, $3 prefix, varname, suffix = $1, $2, $3
varval = expand_varref(self[varname], lambda_args) prefix = expand_varref(prefix, lambda_args) unless prefix.empty?
if varval.is_a?(String) or varval.nil? varval = expand_varref(get_var(varname), lambda_args)
expand_varref("#{prefix}#{varval}#{suffix}", lambda_args) # suffix needs no expansion since the regex matches the last occurence
elsif varval.is_a?(Array) case varval
varval.map {|vv| expand_varref("#{prefix}#{vv}#{suffix}", lambda_args)}.flatten when Array
if prefix.is_a?(Array)
varval.map {|vv| prefix.map {|p| "#{p}#{vv}#{suffix}"}}.flatten
else else
raise "I do not know how to expand a variable reference to a #{varval.class.name} (from #{varname.inspect} => #{self[varname].inspect})" varval.map {|vv| "#{prefix}#{vv}#{suffix}"}
end
when String, Symbol, true, false, nil
if prefix.is_a?(Array)
prefix.map {|p| "#{p}#{varval}#{suffix}"}
else
"#{prefix}#{varval}#{suffix}"
end
else
raise "Unknown construction variable type: #{varval.class} (from #{varname.inspect} => #{get_var(varname).inspect})"
end end
else else
varref varref
end end
elsif varref.is_a?(Array) when Array
varref.map do |ent| varref.map do |ent|
expand_varref(ent, lambda_args) expand_varref(ent, lambda_args)
end.flatten end.flatten
elsif varref.is_a?(Proc) when Symbol, true, false, nil
varref
when Proc
expand_varref(varref[*lambda_args], lambda_args) expand_varref(varref[*lambda_args], lambda_args)
elsif varref.nil?
nil
elsif varref.is_a?(Symbol)
varref
elsif varref.is_a?(TrueClass)
varref
elsif varref.is_a?(FalseClass)
varref
else else
raise "Unknown varref type: #{varref.class} (#{varref.inspect})" raise "Unknown construction variable type: #{varref.class} (#{varref.inspect})"
end end
end end
@@ -188,16 +223,16 @@ module Rscons
# #
# @return [Object] Deep copied value. # @return [Object] Deep copied value.
def deep_dup(obj) def deep_dup(obj)
obj_class = obj.class case obj
if obj_class == Hash when String
obj.dup
when Array
obj.map { |v| deep_dup(v) }
when Hash
obj.reduce({}) do |result, (k, v)| obj.reduce({}) do |result, (k, v)|
result[k] = deep_dup(v) result[k] = deep_dup(v)
result result
end end
elsif obj_class == Array
obj.map { |v| deep_dup(v) }
elsif obj_class == String
obj.dup
else else
obj obj
end end
+1 -1
View File
@@ -1,4 +1,4 @@
module Rscons module Rscons
# gem version # gem version
VERSION = "1.17.0" VERSION = "1.18.0"
end end
+2
View File
@@ -74,6 +74,8 @@ describe Rscons do
command_name = "#{command_prefix}#{@statics[:build_test_id]}" command_name = "#{command_prefix}#{@statics[:build_test_id]}"
File.open("_simplecov_setup.rb", "w") do |fh| File.open("_simplecov_setup.rb", "w") do |fh|
fh.puts <<EOF fh.puts <<EOF
require "bundler"
Bundler.setup
require "simplecov" require "simplecov"
class MyFormatter class MyFormatter
def format(*args) def format(*args)
+4 -4
View File
@@ -26,13 +26,13 @@ module Rscons
cache = build_from(_cache) cache = build_from(_cache)
expect(File).to receive(:exists?).with("one").and_return(true) expect(File).to receive(:exists?).with("one").and_return(true)
expect(File).to receive(:exists?).with("one/two").and_return(false) expect(File).to receive(:exists?).with("one/two").and_return(false)
expect(FileUtils).to receive(:mkdir).with("one/two") expect(FileUtils).to receive(:mkdir_p).with("one/two")
expect(File).to receive(:exists?).with("one/two/three").and_return(false) expect(File).to receive(:exists?).with("one/two/three").and_return(false)
expect(FileUtils).to receive(:mkdir).with("one/two/three") expect(FileUtils).to receive(:mkdir_p).with("one/two/three")
expect(File).to receive(:exists?).with("one").and_return(true) expect(File).to receive(:exists?).with("one").and_return(true)
expect(File).to receive(:exists?).with("one/two").and_return(true) expect(File).to receive(:exists?).with("one/two").and_return(true)
expect(File).to receive(:exists?).with("one/two/four").and_return(false) expect(File).to receive(:exists?).with("one/two/four").and_return(false)
expect(FileUtils).to receive(:mkdir).with("one/two/four") expect(FileUtils).to receive(:mkdir_p).with("one/two/four")
cache.mkdir_p("one/two/three") cache.mkdir_p("one/two/three")
cache.mkdir_p("one\\two\\four") cache.mkdir_p("one\\two\\four")
expect(cache.directories).to eq ["one/two", "one/two/three", "one/two/four"] expect(cache.directories).to eq ["one/two", "one/two/three", "one/two/four"]
@@ -43,7 +43,7 @@ module Rscons
cache = build_from(_cache) cache = build_from(_cache)
expect(File).to receive(:exists?).with("/one").and_return(true) expect(File).to receive(:exists?).with("/one").and_return(true)
expect(File).to receive(:exists?).with("/one/two").and_return(false) expect(File).to receive(:exists?).with("/one/two").and_return(false)
expect(FileUtils).to receive(:mkdir).with("/one/two") expect(FileUtils).to receive(:mkdir_p).with("/one/two")
cache.mkdir_p("/one/two") cache.mkdir_p("/one/two")
expect(cache.directories).to eq ["/one/two"] expect(cache.directories).to eq ["/one/two"]
end end
+1 -1
View File
@@ -184,7 +184,7 @@ module Rscons
env["foo"] = {} env["foo"] = {}
expect(env.expand_varref(["-p${path}", "${flags}"])).to eq ["-pdir1", "-pdir2", "-x", "-y", "-z"] expect(env.expand_varref(["-p${path}", "${flags}"])).to eq ["-pdir1", "-pdir2", "-x", "-y", "-z"]
expect(env.expand_varref("foo")).to eq "foo" expect(env.expand_varref("foo")).to eq "foo"
expect {env.expand_varref("${foo}")}.to raise_error /Unknown.varref.type/ expect {env.expand_varref("${foo}")}.to raise_error /Unknown.construction.variable.type/
expect(env.expand_varref("${specialflag}")).to eq "-z" expect(env.expand_varref("${specialflag}")).to eq "-z"
expect(env.expand_varref("${path}")).to eq ["dir1", "dir2"] expect(env.expand_varref("${path}")).to eq ["dir1", "dir2"]
end end
+16 -3
View File
@@ -166,13 +166,26 @@ module Rscons
expect(v.expand_varref(false, :lambda_args)).to eq(false) expect(v.expand_varref(false, :lambda_args)).to eq(false)
end end
it "raises an error when given an invalid argument" do it "raises an error when given an invalid argument" do
expect { v.expand_varref({a: :b}, :lambda_args) }.to raise_error /Unknown varref type: Hash/ expect { v.expand_varref({a: :b}, :lambda_args) }.to raise_error /Unknown construction variable type: Hash/
end end
it "raises an error when an expanded variable is an unexpected type" do it "raises an error when an expanded variable is an unexpected type" do
expect(v).to receive(:[]).at_least(1).times.with("bad").and_return("bad_val") expect(v).to receive(:get_var).at_least(1).times.with("bad").and_return("bad_val")
expect(v).to receive(:expand_varref).with("bad_val", :lambda_args).and_return({a: :b}) expect(v).to receive(:expand_varref).with("bad_val", :lambda_args).and_return({a: :b})
expect(v).to receive(:expand_varref).and_call_original expect(v).to receive(:expand_varref).and_call_original
expect { v.expand_varref("${bad}", :lambda_args) }.to raise_error /I do not know how to expand a variable reference to a Hash/ expect { v.expand_varref("${bad}", :lambda_args) }.to raise_error /Unknown construction variable type: Hash/
end
it "expands symbols within a string outside an array" do
v['var'] = :a_symbol
expect(v.expand_varref("this is ${var}", :lambda_args)).to eq "this is a_symbol"
end
it "expands booleans within a string outside an array" do
v['var'] = false
expect(v.expand_varref("this is ${var}", :lambda_args)).to eq "this is false"
end
it "expands symbols and booleans in an array" do
v['var'] = [:a_symbol, false]
expanded = v.expand_varref("this is ${var}", :lambda_args)
expect(expanded).to eq(["this is a_symbol", "this is false"])
end end
end end