Skip to content

Commit

Permalink
Save 10 ms by using Vec instead of VecDeque (#2254)
Browse files Browse the repository at this point in the history
  • Loading branch information
Centril authored Feb 11, 2025
1 parent 0b2364b commit 14d21c4
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions crates/bindings/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ pub mod table;

use spacetimedb_lib::bsatn;
use std::cell::RefCell;
use std::collections::VecDeque;

pub use log;
#[cfg(feature = "rand")]
Expand Down Expand Up @@ -138,7 +137,7 @@ pub fn table_id_from_name(table_name: &str) -> TableId {
thread_local! {
/// A global pool of buffers used for iteration.
// This gets optimized away to a normal global since wasm32 doesn't have threads by default.
static ITER_BUFS: RefCell<VecDeque<Vec<u8>>> = const { RefCell::new(VecDeque::new()) };
static ITER_BUFS: RefCell<Vec<Vec<u8>>> = const { RefCell::new(Vec::new()) };
}

struct IterBuf {
Expand All @@ -149,7 +148,7 @@ impl IterBuf {
/// Take a buffer from the pool of buffers for row iterators, if one exists. Otherwise, allocate a new one.
fn take() -> Self {
let buf = ITER_BUFS
.with_borrow_mut(|v| v.pop_front())
.with_borrow_mut(|v| v.pop())
.unwrap_or_else(|| Vec::with_capacity(DEFAULT_BUFFER_CAPACITY));
Self { buf }
}
Expand All @@ -170,7 +169,7 @@ impl Drop for IterBuf {
fn drop(&mut self) {
self.buf.clear();
let buf = std::mem::take(&mut self.buf);
ITER_BUFS.with_borrow_mut(|v| v.push_back(buf));
ITER_BUFS.with_borrow_mut(|v| v.push(buf));
}
}

Expand Down

2 comments on commit 14d21c4

@github-actions
Copy link

@github-actions github-actions bot commented on 14d21c4 Feb 11, 2025

Choose a reason for hiding this comment

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

Callgrind benchmark results Error when comparing benchmarks: Couldn't find AWS credentials in environment, credentials file, or IAM role.

Caused by:
Couldn't find AWS credentials in environment, credentials file, or IAM role.

@github-actions
Copy link

@github-actions github-actions bot commented on 14d21c4 Feb 11, 2025

Choose a reason for hiding this comment

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

Criterion benchmark results

Error when comparing benchmarks: Couldn't find AWS credentials in environment, credentials file, or IAM role.

Caused by:
Couldn't find AWS credentials in environment, credentials file, or IAM role.

Please sign in to comment.