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

Conversation

paddor
Copy link
Contributor

@paddor paddor commented Dec 22, 2023

Nothing much, just what the title says. Theoretically, if an infinite amount of different regexp are used in SQL queries, it could of course fill up RAM, but I think that's uncommon.

In my case, this brought the runtime from ~300ms down to ~70ms for a query that uses one single regexp in a table with around ~5700 rows.

Copy link
Owner

@jeremyevans jeremyevans left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm OK with supporting this, but it must be conditional and off by default for backwards compatibility. This introduces a potential memory leak for code that does not expect the regexps to be cached.

@paddor
Copy link
Contributor Author

paddor commented Dec 23, 2023

I've changed the implementation. It now only caches Regexp objects if setup_regexp_function => :cached was given.

@paddor
Copy link
Contributor Author

paddor commented Dec 23, 2023

Thinking about it a little longer, maybe the option setup_regexp_function should just support taking a block. That way, the user is able to use modern features like Regexp#match? and Regexp.new(x, timeout: y) or just periodically clear the cache hash to avoid memleaks.

@paddor
Copy link
Contributor Author

paddor commented Dec 23, 2023

This is how I plan to use it now:

regexp_cache = Hash.new { |h, k| h[k] = Regexp.new(k, timeout: 0.005) }
opts = {
  setup_regexp_function: proc { |re_str, str| regexp_cache[re_str].match? str }
}
Sequel.connect('sqlite:///path/db.sqlite3', opts)

Let me know what you think. I left the :cached functionality in, with appropriate changes to the documentation. Let me know if I should remove it.

@paddor paddor changed the title Cache regexp objects in the sqlite adapter to improve performance Improve performance of regexp matching in the sqlite adapter Dec 24, 2023
@jeremyevans
Copy link
Owner

Squash merged at c824824

@paddor
Copy link
Contributor Author

paddor commented Dec 27, 2023

Thanks!

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