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

perf: CoreStatement uses optimize regex for generated key matches #1080

Merged
merged 2 commits into from
Mar 12, 2024

Conversation

schlosna
Copy link
Contributor

While profiling some software that makes heavy use of prepared statements and SQLite, I noticed that each call to java.sql.Connection#prepareStatement(java.lang.String) ended up compiling a regex pattern as part of org.sqlite.core.CoreStatement.<init>(SQLiteConnection) construction. This appears to be a recent addition as part of #1056 with example call trace showing up as over 75% of total allocations in JFR:

image

Because JDK Pattern instances are immutable and thread-safe, we can compile the regex on CoreStatement class load to a static field for reuse, and adjust the regex to avoid additional possible allocations from trimming and converting the SQL to lower case, as well as use non-capturing groups to avoid Pattern#matcher allocating the int[] arrays associated with capture groups.

While profiling some software that makes heavy use of prepared
statements and SQLite, I noticed that each call to
`java.sql.Connection#prepareStatement(java.lang.String)` ended up
compiling a regex pattern as part of
`org.sqlite.core.CoreStatement.<init>(SQLiteConnection)` construction.
This appears to be a recent addition as part of
xerial#1056  with example call trace
showing up as over 75% of total allocations in JFR:

<img width="1135" alt="image" src="https://github.com/xerial/sqlite-jdbc/assets/54594/f5b5d452-f21c-4bf0-a8d6-beb8f5640321">

Because JDK `Pattern` instances are immutable and thread-safe, we can
compile the regex on `CoreStatement` class load to a static field for
reuse, and adjust the regex to avoid additional possible allocations
from trimming and converting the SQL to lower case, as well as use
non-capturing groups to avoid `Pattern#matcher` allocating the `int[]`
arrays associated with capture groups.
@gotson
Copy link
Collaborator

gotson commented Mar 12, 2024

thanks a lot for the PR !

@gotson gotson changed the title fix: CoreStatement uses optimize regex for generated key matches perf: CoreStatement uses optimize regex for generated key matches Mar 12, 2024
@gotson gotson merged commit 95b8efa into xerial:master Mar 12, 2024
28 checks passed
@schlosna schlosna deleted the ds/regex branch March 18, 2024 15:13
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.

2 participants