Skip to content

Commit 248a7b7

Browse files
add contention factor
1 parent e99e380 commit 248a7b7

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

src/common/ttl_map.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,7 @@ where
131131
{
132132
let mut new_entry = false;
133133
let start = std::time::Instant::now();
134-
let entry = self.data.entry(key.clone());
135-
self.dash_map_lock_contention_time
136-
.fetch_add(start.elapsed().as_nanos() as usize, Relaxed);
137-
let value = match entry {
134+
let value = match self.data.entry(key.clone()) {
138135
Entry::Vacant(entry) => {
139136
let value = f();
140137
entry.insert(value.clone());
@@ -143,6 +140,8 @@ where
143140
}
144141
Entry::Occupied(entry) => entry.get().clone(),
145142
};
143+
self.dash_map_lock_contention_time
144+
.fetch_add(start.elapsed().as_nanos() as usize, Relaxed);
146145

147146
// Insert the key into the previous bucket, meaning the key will be evicted
148147
// when the wheel completes a full rotation.
@@ -369,6 +368,7 @@ mod tests {
369368

370369
let start_time = Instant::now();
371370
let task_count = 100_000;
371+
let contention_factor = 10;
372372

373373
// Spawn 10 tasks that repeatedly read the same keys
374374
let mut handles = Vec::new();
@@ -377,7 +377,7 @@ mod tests {
377377
let handle = tokio::spawn(async move {
378378
// All tasks fight for the same keys - maximum contention
379379
let start = Instant::now();
380-
let _value = map.get_or_init(rand::random(), || task_id * 1000).await;
380+
let _value = map.get_or_init(task_id % (task_count / contention_factor), || task_id * 1000).await;
381381
start.elapsed().as_nanos()
382382
});
383383
handles.push(handle);

0 commit comments

Comments
 (0)