Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a more efficient regex #508

Merged
merged 1 commit into from
Oct 26, 2023
Merged

Create a more efficient regex #508

merged 1 commit into from
Oct 26, 2023

Conversation

shanempope
Copy link
Contributor

@shanempope shanempope commented Oct 26, 2023

require 'benchmark'

# Original regular expressions
original = Regexp.union(
  %r{\A(?:/\*.*?\*/)?\s*ROLLBACK}i,
  %r{\A(?:/\*.*?\*/)?\s*COMMIT}i,
  %r{\A(?:/\*.*?\*/)?\s*RELEASE\s+SAVEPOINT}i,
)

# Combined regular expression
combined = %r{\A(?:/\*.*?\*/)?\s*(ROLLBACK|COMMIT|RELEASE\s+SAVEPOINT)}i

# Test string
sql = "/* comment */ COMMIT"

n = 1_000_000
Benchmark.bm do |x|
  x.report("original:") { n.times { original.match?(sql) } }
  x.report("combined:") { n.times { combined.match?(sql) } }
end

image

More complicated benchmark:
image

require 'benchmark'

# Original regular expressions
original = Regexp.union(
  %r{\A(?:/\*.*?\*/)?\s*ROLLBACK}i,
  %r{\A(?:/\*.*?\*/)?\s*COMMIT}i,
  %r{\A(?:/\*.*?\*/)?\s*RELEASE\s+SAVEPOINT}i,
)

# Combined regular expression
combined = %r{\A(?:/\*.*?\*/)?\s*(ROLLBACK|COMMIT|RELEASE\s+SAVEPOINT)}i

# Test string
sql = "/* comment */ COMMIT"

n = 1_000_000
Benchmark.bm do |x|
  x.report("original:") { n.times { original.match?(sql) } }
  x.report("combined:") { n.times { combined.match?(sql) } }
end
@rafaelfranca
Copy link
Member

Pro tip, always use benchmark-ips to write benchmarks:

require "bundler/setup"
require "benchmark/ips"

# Original regular expressions
original = Regexp.union(
  %r{\A(?:/\*.*?\*/)?\s*ROLLBACK}i,
  %r{\A(?:/\*.*?\*/)?\s*COMMIT}i,
  %r{\A(?:/\*.*?\*/)?\s*RELEASE\s+SAVEPOINT}i,
)

# Combined regular expression
combined = %r{\A(?:/\*.*?\*/)?\s*(ROLLBACK|COMMIT|RELEASE\s+SAVEPOINT)}i

# Test string
sql = "/* comment */ COMMIT"

Benchmark.ips do |x|
  x.report("original:") { original.match?(sql) }
  x.report("combined:") { combined.match?(sql) }
  x.compare!
end
Warming up --------------------------------------
           original:   111.550k i/100ms
           combined:   387.956k i/100ms
Calculating -------------------------------------
           original:      1.062M (±12.4%) i/s -      5.243M in   5.049497s
           combined:      3.675M (± 9.6%) i/s -     18.234M in   5.029335s

Comparison:
           combined::  3674598.6 i/s
           original::  1061797.2 i/s - 3.46x  (± 0.00) slower

@shanempope shanempope merged commit ad1515c into master Oct 26, 2023
42 checks passed
@shanempope shanempope deleted the shanempope-patch-1 branch October 26, 2023 20:51
@shopify-shipit shopify-shipit bot temporarily deployed to release October 27, 2023 13:27 Inactive
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants