-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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(db): cache db handles using OnceLock #6750
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
ptal @joshieDo
fn open_and_store_db<T: Table>(&self, slot: &OnceLock<DBI>) -> Result<DBI, DatabaseError> { | ||
match self.inner.open_db(Some(T::NAME)) { | ||
Ok(db) => { | ||
slot.set(db.dbi()).unwrap(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we map_err
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Already tried, this ends up as map(|db| db.dbi()).map_err(|| ..).inspect(|| slot.set().unwrap())
, it's not any better
What's the perf improvement? |
OnceLock is cheaper to "lock" than a mutex. We can use this because we don't need mutable access after the first lazy initialization |
4c08107
to
9c9395f
Compare
9c9395f
to
f769c53
Compare
We only need mutable access once to initialize the value.
Note that this doubles the size of the cache field on non-Linux (
Once
isAtomicU32
on platforms withfutex
support, otherwiseAtomicPtr
)