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(db): cache db handles using OnceLock #6750

Merged
merged 2 commits into from Feb 26, 2024
Merged

Conversation

DaniPopes
Copy link
Member

@DaniPopes DaniPopes commented Feb 23, 2024

We only need mutable access once to initialize the value.

Note that this doubles the size of the cache field on non-Linux (Once is AtomicU32 on platforms with futex support, otherwise AtomicPtr)

Copy link
Collaborator

@mattsse mattsse left a comment

Choose a reason for hiding this comment

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

lgtm

ptal @joshieDo

@mattsse mattsse added C-debt A section of code is hard to understand or change A-db Related to the database labels Feb 23, 2024
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();
Copy link
Collaborator

Choose a reason for hiding this comment

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

can we map_err

Copy link
Member Author

@DaniPopes DaniPopes Feb 26, 2024

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

@gakonst
Copy link
Member

gakonst commented Feb 26, 2024

What's the perf improvement?

@DaniPopes
Copy link
Member Author

DaniPopes commented Feb 26, 2024

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

Merged via the queue into main with commit ac36fa9 Feb 26, 2024
29 checks passed
@DaniPopes DaniPopes deleted the dani/tx-handles-perf branch February 26, 2024 13:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-db Related to the database C-debt A section of code is hard to understand or change
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants