Skip to content
This repository has been archived by the owner on Aug 16, 2024. It is now read-only.

Commit

Permalink
move mutex guard from inside a loop (#29)
Browse files Browse the repository at this point in the history
# What ❔

This PR is implementing a small performance improvement in the device
allocation code by moving a mutex guard form inside a loop to the
outside.

## Why ❔

Acquiring mutex in a loop is suboptimal.

## Checklist

- [x] PR title corresponds to the body of PR (we generate changelog
entries from PRs).
- [x] Code has been formatted via `cargo fmt` and linted with `cargo
check`.
  • Loading branch information
robik75 authored Feb 7, 2024
1 parent f243cc6 commit 43a203d
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/static_allocator/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,12 @@ impl StaticDeviceAllocator {
None
}

fn free_block(&self, index: usize) {
self.bitmap.lock().unwrap()[index] = false;
fn free_blocks(&self, index: usize, num_blocks: usize) {
assert!(num_blocks > 0);
let mut guard = self.bitmap.lock().unwrap();
for i in index..index + num_blocks {
guard[i] = false;
}
}

pub fn free(self) -> CudaResult<()> {
Expand Down Expand Up @@ -351,10 +355,7 @@ unsafe impl Allocator for StaticDeviceAllocator {
assert_eq!(offset % self.block_size_in_bytes, 0);
let index = offset / self.block_size_in_bytes;
let num_blocks = size / self.block_size_in_bytes;
assert!(num_blocks > 0);
for actual_idx in index..index + num_blocks {
self.free_block(actual_idx);
}
self.free_blocks(index, num_blocks);
#[cfg(feature = "allocator_stats")]
self.stats.lock().unwrap().free(index);
}
Expand Down

0 comments on commit 43a203d

Please sign in to comment.