From ed5e259cfebd01f724d9cd1b8fe9e15c6b5d5d07 Mon Sep 17 00:00:00 2001 From: Pekka Enberg Date: Fri, 14 Apr 2023 12:04:27 +0300 Subject: [PATCH] Switch to parking_lot mutex It's faster than the standard library mutex. --- core/mvcc/database/Cargo.toml | 1 + core/mvcc/database/src/database.rs | 15 ++++++++------- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/core/mvcc/database/Cargo.toml b/core/mvcc/database/Cargo.toml index 16a6d4de8..6f1512e0d 100644 --- a/core/mvcc/database/Cargo.toml +++ b/core/mvcc/database/Cargo.toml @@ -5,6 +5,7 @@ edition = "2021" [dependencies] anyhow = "1.0.70" +parking_lot = "0.12.1" rustyline = "11.0.0" thiserror = "1.0.40" tracing = "0.1.37" diff --git a/core/mvcc/database/src/database.rs b/core/mvcc/database/src/database.rs index 1ac44719a..6f196f291 100644 --- a/core/mvcc/database/src/database.rs +++ b/core/mvcc/database/src/database.rs @@ -2,8 +2,9 @@ use crate::clock::LogicalClock; use crate::errors::DatabaseError; use std::cell::RefCell; use std::collections::{HashMap, HashSet}; +use parking_lot::Mutex; use std::sync::atomic::{AtomicU64, Ordering}; -use std::sync::{Arc, Mutex}; +use std::sync::Arc; type Result = std::result::Result; @@ -129,7 +130,7 @@ impl Database { /// * `row` - the row object containing the values to be inserted. /// pub fn insert(&self, tx_id: TxID, row: Row) -> Result<()> { - let inner = self.inner.lock().unwrap(); + let inner = self.inner.lock(); inner.insert(tx_id, row) } @@ -174,7 +175,7 @@ impl Database { /// Returns `true` if the row was successfully deleted, and `false` otherwise. /// pub fn delete(&self, tx_id: TxID, id: u64) -> Result { - let inner = self.inner.lock().unwrap(); + let inner = self.inner.lock(); inner.delete(tx_id, id) } @@ -193,7 +194,7 @@ impl Database { /// Returns `Some(row)` with the row data if the row with the given `id` exists, /// and `None` otherwise. pub fn read(&self, tx_id: TxID, id: u64) -> Result> { - let inner = self.inner.lock().unwrap(); + let inner = self.inner.lock(); inner.read(tx_id, id) } @@ -203,7 +204,7 @@ impl Database { /// that you can use to perform operations within the transaction. All changes made within the /// transaction are isolated from other transactions until you commit the transaction. pub fn begin_tx(&self) -> TxID { - let mut inner = self.inner.lock().unwrap(); + let mut inner = self.inner.lock(); inner.begin_tx() } @@ -217,7 +218,7 @@ impl Database { /// /// * `tx_id` - The ID of the transaction to commit. pub fn commit_tx(&self, tx_id: TxID) -> Result<()> { - let mut inner = self.inner.lock().unwrap(); + let mut inner = self.inner.lock(); inner.commit_tx(tx_id) } @@ -230,7 +231,7 @@ impl Database { /// /// * `tx_id` - The ID of the transaction to abort. pub fn rollback_tx(&self, tx_id: TxID) { - let inner = self.inner.lock().unwrap(); + let inner = self.inner.lock(); inner.rollback_tx(tx_id); } }