From 37c01ec4c556ab5610457453260f86ef6e348027 Mon Sep 17 00:00:00 2001 From: shane pope Date: Thu, 26 Oct 2023 15:05:35 -0500 Subject: [PATCH] Create a more efficient regex 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 --- lib/semian/activerecord_trilogy_adapter.rb | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/lib/semian/activerecord_trilogy_adapter.rb b/lib/semian/activerecord_trilogy_adapter.rb index 80b364ed..6223d56d 100644 --- a/lib/semian/activerecord_trilogy_adapter.rb +++ b/lib/semian/activerecord_trilogy_adapter.rb @@ -91,11 +91,7 @@ def resource_exceptions end # TODO: share this with Mysql2 - QUERY_ALLOWLIST = Regexp.union( - %r{\A(?:/\*.*?\*/)?\s*ROLLBACK}i, - %r{\A(?:/\*.*?\*/)?\s*COMMIT}i, - %r{\A(?:/\*.*?\*/)?\s*RELEASE\s+SAVEPOINT}i, - ) + QUERY_ALLOWLIST = %r{\A(?:/\*.*?\*/)?\s*(ROLLBACK|COMMIT|RELEASE\s+SAVEPOINT)}i def query_allowlisted?(sql, *) QUERY_ALLOWLIST.match?(sql)