diff options
author | Lamont Granquist <lamont@scriptkiddie.org> | 2020-07-23 16:26:31 -0700 |
---|---|---|
committer | Lamont Granquist <lamont@scriptkiddie.org> | 2020-07-23 16:26:31 -0700 |
commit | c9c9615cda01e2284e77578f679cc9a9745c0da6 (patch) | |
tree | 02895761d75c6b0bcb6af8e6a4a1f7650e01761a /chef-config/lib | |
parent | e48341724607e2fdfecfe74f2a7b80ea1ff6185a (diff) | |
download | chef-c9c9615cda01e2284e77578f679cc9a9745c0da6.tar.gz |
Reduce path_helper allocations
Eliminates a very large number of allocation in this routine simply
by moving code which never changes to a static method + constant.
Signed-off-by: Lamont Granquist <lamont@scriptkiddie.org>
Diffstat (limited to 'chef-config/lib')
-rw-r--r-- | chef-config/lib/chef-config/path_helper.rb | 24 |
1 files changed, 15 insertions, 9 deletions
diff --git a/chef-config/lib/chef-config/path_helper.rb b/chef-config/lib/chef-config/path_helper.rb index 570e666e10..7c959f71e7 100644 --- a/chef-config/lib/chef-config/path_helper.rb +++ b/chef-config/lib/chef-config/path_helper.rb @@ -55,18 +55,24 @@ module ChefConfig end end - def self.join(*args) - path_separator_regex = Regexp.escape(File::SEPARATOR) - unless path_separator == File::SEPARATOR - path_separator_regex << Regexp.escape(path_separator) - end + def self.path_separator_regex + @path_separator_regex ||= + begin + path_separator_regex = Regexp.escape(File::SEPARATOR) + unless path_separator == File::SEPARATOR + path_separator_regex << Regexp.escape(path_separator) + end + path_separator_regex + end + end - trailing_slashes = /[#{path_separator_regex}]+$/ - leading_slashes = /^[#{path_separator_regex}]+/ + TRAILING_SLASHES_REGEX = /[#{path_separator_regex}]+$/.freeze + LEADING_SLASHES_REGEX = /^[#{path_separator_regex}]+/.freeze + def self.join(*args) args.flatten.inject do |joined_path, component| - joined_path = joined_path.sub(trailing_slashes, "") - component = component.sub(leading_slashes, "") + joined_path = joined_path.sub(TRAILING_SLASHES_REGEX, "") + component = component.sub(LEADING_SLASHES_REGEX, "") joined_path + "#{path_separator}#{component}" end end |