Don't recompile the regexes every Note::new
/Note::write_to_db
#15
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hi there - first of all thank you for making this crate, it's super useful!
Motivation
I noticed when profiling my application that quite a chunk of time was being spent in
Note::new
(I end up creating 193 notes in this example):This looks to be mainly due to compiling the "sentinel regex" for every field every time:
genanki-rs/src/model.rs
Line 245 in fe983cf
Since the fields never change, I think we can pre-compile the regexes to save this time.
Benchmark post-change
With this change:
Which comes out as making
Note::new
go from taking 12.6ms per-call to 0.1ms.Adding some once-cells
I then noticed that there were some more regexes getting compiled every
Note::write_to_db
, so have made these only get compiled once usingonce_cell
.This then cuts the time down further:
and now
genanki-rs
is barely visible anymore on the flamegraph!Implementation
I think that what I've done is reasonable - fine to add a new private field to
Model
and the implementation seems to work. Happy to change up the implementation however you see fit though!