Move regexp definitions from Gitlab::Regex into domain specific locations
Problem
Gitlab::Regex
contains a lot of regular expression definitions for cross-domain concerns.
Proposed solution
Move these definitions into related domain namespaces.
For example, Gitlab::Regex::Package
could go into Packages
or Packages::Regexp
.
Performance note
Most regular expressions are defined in a method which caches the regex via an ivar (on module
level because of extend self
). However, using the uncached version or defining regexs as constants is faster.
Click to expand
# frozen_string_literal: true
require "benchmark/ips"
class A
CONST = %r{\Aregex\z}
def cached
@cached ||= %r{\Aregex\z}
end
def normal
%r{\Aregex\z}
end
end
a = A.new
Benchmark.ips do |x|
x.report "cached" do
a.cached.match?("regex")
end
x.report "normal" do
a.normal.match?("regex")
end
x.report "const" do
A::CONST.match?("regex")
end
x.compare!
end
Warming up --------------------------------------
cached 985.232k i/100ms
normal 1.012M i/100ms
const 1.139M i/100ms
Calculating -------------------------------------
cached 9.630M (± 3.0%) i/s - 48.276M in 5.018028s
normal 9.820M (± 6.8%) i/s - 49.586M in 5.081352s
const 11.089M (± 3.8%) i/s - 55.797M in 5.038982s
Comparison:
const: 11089330.2 i/s
normal: 9820407.1 i/s - 1.13x slower
cached: 9629627.0 i/s - 1.15x slower