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

Improve performance of regexp matching in the sqlite adapter #2108

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

* Add auto_cast_date_and_time extension, for casting date and time values using SQL standard functions (jeremyevans)

* Cache regexp objects in the sqlite adapter to improve performance (paddor)

=== 5.75.0 (2023-12-01)

* Make any_not_empty? extension support passing pattern argument to any? (jeremyevans) (#2100)
Expand Down
4 changes: 2 additions & 2 deletions doc/opening_databases.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -388,8 +388,8 @@ The following additional options are supported:
:readonly :: open database in read-only mode
:timeout :: the busy timeout to use in milliseconds (default: 5000).
:setup_regexp_function :: Whether to setup a REGEXP function in the underlying SQLite3::Database object. Doing so
allows you to use regexp support in dataset expressions. Note that this creates a new
Regexp object per call to the function, so it is not an efficient implementation.
allows you to use regexp support in dataset expressions. Note that this caches each
unique regex in the database instance.

Note that SQLite memory databases are restricted to a single connection by
default. This is because SQLite does not allow multiple connections to
Expand Down
3 changes: 2 additions & 1 deletion lib/sequel/adapters/sqlite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,9 @@ def connect(server)
connection_pragmas.each{|s| log_connection_yield(s, db){db.execute_batch(s)}}

if typecast_value_boolean(opts[:setup_regexp_function])
@regexp_cache = Hash.new{|h,k| h[k] = Regexp.new(k)}
db.create_function("regexp", 2) do |func, regexp_str, string|
func.result = Regexp.new(regexp_str).match(string) ? 1 : 0
func.result = @regexp_cache[regexp_str].match(string) ? 1 : 0
end
end

Expand Down