Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 20 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ env_logger = "0.10"
ethnum = { version = "1.5.0", features = ["serde"] }
flate2 = "1.0.24"
flume = { version = "0.11", default-features = false, features = ["async"] }
foldhash = "0.1.4"
foldhash = "0.2.0"
fs-err = "2.9.0"
fs_extra = "1.3.0"
fs2 = "0.4.3"
Expand All @@ -189,7 +189,7 @@ futures-util = "0.3"
getrandom02 = { package = "getrandom", version = "0.2" }
git2 = "0.19"
glob = "0.3.1"
hashbrown = { version = "0.15", default-features = false, features = ["equivalent", "inline-more"] }
hashbrown = { version = "0.16.1", default-features = false, features = ["equivalent", "inline-more", "rayon", "serde"] }
headers = "0.4"
heck = "0.4"
hex = "0.4.3"
Expand Down
2 changes: 2 additions & 0 deletions crates/bench/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,15 @@ spacetimedb-standalone = { path = "../standalone" }
spacetimedb-table = { path = "../table" }
spacetimedb-testing = { path = "../testing" }

ahash.workspace = true
anyhow.workspace = true
anymap.workspace = true
byte-unit.workspace = true
clap.workspace = true
criterion.workspace = true
futures.workspace = true
foldhash.workspace = true
hashbrown.workspace = true
lazy_static.workspace = true
log.workspace = true
mimalloc.workspace = true
Expand Down
6 changes: 3 additions & 3 deletions crates/bench/benches/index.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use core::{hint::black_box, iter::repeat_with, time::Duration};
use core::{hash::BuildHasherDefault, hint::black_box, iter::repeat_with, time::Duration};
use criterion::{
criterion_group, criterion_main,
measurement::{Measurement as _, WallTime},
BenchmarkGroup, Criterion,
};
use foldhash::{HashSet, HashSetExt};
use hashbrown::{hash_map::Entry, HashMap};
use itertools::Itertools as _;
use spacetimedb_data_structures::map::{Entry, HashMap};
use spacetimedb_sats::layout::Size;
use spacetimedb_table::indexes::{PageIndex, PageOffset, RowPointer, SquashedOffset};
use spacetimedb_table::table_index::unique_direct_index::UniqueDirectIndex;
Expand Down Expand Up @@ -191,7 +191,7 @@ impl Index for IBTree {
}

#[derive(Clone)]
struct IAHash(HashMap<K, RowPointer>);
struct IAHash(HashMap<K, RowPointer, BuildHasherDefault<ahash::AHasher>>);
impl Index for IAHash {
const NAME: &'static str = "IAHash";
fn new() -> Self {
Expand Down
1 change: 0 additions & 1 deletion crates/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ flume.workspace = true
fs2.workspace = true
futures.workspace = true
futures-util.workspace = true
hashbrown = { workspace = true, features = ["rayon", "serde"] }
hex.workspace = true
hostname.workspace = true
http.workspace = true
Expand Down
5 changes: 2 additions & 3 deletions crates/core/src/subscription/delta.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use crate::host::module_host::UpdatesRelValue;
use anyhow::Result;
use hashbrown::HashMap;
use spacetimedb_data_structures::map::{HashCollectionExt as _, HashMap};
use spacetimedb_execution::{Datastore, DeltaStore, Row};
use spacetimedb_lib::metrics::ExecutionMetrics;
use spacetimedb_primitives::ColList;
use spacetimedb_sats::product_value::InvalidFieldError;
use spacetimedb_subscription::SubscriptionPlan;
use spacetimedb_vm::relation::RelValue;

use crate::host::module_host::UpdatesRelValue;

/// Evaluate a subscription over a delta update.
/// Returns `None` for empty updates.
///
Expand Down
2 changes: 1 addition & 1 deletion crates/core/src/subscription/module_subscription_actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1253,7 +1253,6 @@ mod tests {
use crate::subscription::query::compile_read_only_query;
use crate::subscription::TableUpdateType;
use core::fmt;
use hashbrown::HashMap;
use itertools::Itertools;
use pretty_assertions::assert_matches;
use spacetimedb_client_api_messages::energy::EnergyQuanta;
Expand All @@ -1262,6 +1261,7 @@ mod tests {
TableUpdate, Unsubscribe, UnsubscribeMulti,
};
use spacetimedb_commitlog::{commitlog, repo};
use spacetimedb_data_structures::map::{HashCollectionExt as _, HashMap};
use spacetimedb_datastore::system_tables::{StRowLevelSecurityRow, ST_ROW_LEVEL_SECURITY_ID};
use spacetimedb_durability::{Durability, EmptyHistory, TxOffset};
use spacetimedb_execution::dml::MutDatastore;
Expand Down
8 changes: 5 additions & 3 deletions crates/core/src/subscription/module_subscription_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@ use crate::subscription::delta::eval_delta;
use crate::subscription::websocket_building::BuildableWebsocketFormat;
use crate::worker_metrics::WORKER_METRICS;
use core::mem;
use hashbrown::hash_map::OccupiedError;
use hashbrown::{HashMap, HashSet};
use parking_lot::RwLock;
use prometheus::IntGauge;
use spacetimedb_client_api_messages::websocket::{
BsatnFormat, CompressableQueryUpdate, FormatSwitch, JsonFormat, QueryId, QueryUpdate, SingleQueryUpdate,
};
use spacetimedb_data_structures::map::{Entry, IntMap};
use spacetimedb_data_structures::map::HashCollectionExt as _;
use spacetimedb_data_structures::map::{
hash_map::{Entry, OccupiedError},
HashMap, HashSet, IntMap,
};
use spacetimedb_datastore::locking_tx_datastore::state_view::StateView;
use spacetimedb_durability::TxOffset;
use spacetimedb_expr::expr::CollectViews;
Expand Down
23 changes: 10 additions & 13 deletions crates/core/src/subscription/tx.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
use std::{
collections::BTreeMap,
ops::{Deref, RangeBounds},
sync::Arc,
};

use hashbrown::HashMap;
use super::module_subscription_manager::QueriedTableIndexIds;
use itertools::Either;
use smallvec::SmallVec;
use spacetimedb_data_structures::map::{HashCollectionExt as _, HashMap};
use spacetimedb_datastore::{
locking_tx_datastore::{state_view::StateView, TxId},
traits::TxData,
};
use spacetimedb_execution::{Datastore, DeltaStore, Row};
use spacetimedb_lib::{query::Delta, AlgebraicValue, ProductValue};
use spacetimedb_primitives::{IndexId, TableId};
use spacetimedb_table::table::{IndexScanRangeIter, TableScanIter};

use spacetimedb_datastore::{
locking_tx_datastore::{state_view::StateView, TxId},
traits::TxData,
use std::{
collections::BTreeMap,
ops::{Deref, RangeBounds},
sync::Arc,
};

use super::module_subscription_manager::QueriedTableIndexIds;

/// If an index is defined on a set of columns,
/// and if that index is used in a subscription query,
/// we build the very same index for each delta table.
Expand Down
33 changes: 22 additions & 11 deletions crates/data-structures/src/map.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,34 @@
use core::hash::{BuildHasher, BuildHasherDefault};
pub use hashbrown::hash_map::Entry;
use nohash_hasher::BuildNoHashHasher;

pub use hashbrown::Equivalent;
pub use nohash_hasher::IsEnabled as ValidAsIdentityHash;

pub mod hash_set {
pub use super::HashSet;
pub use hashbrown::hash_set::*;
}

pub mod hash_map {
pub use super::HashMap;
pub use hashbrown::hash_map::*;
}

pub type DefaultHashBuilder = BuildHasherDefault<ahash::AHasher>;
// TODO(centril): expose two maps instead,
// one `map::fast::HashMap` and one `map::ddos::HashMap`.
// In the first case we won't care about DDoS protection at all and can use `foldhash::fast`.
// In the lattr, we can use e.g., randomized AHash.
pub type HashMap<K, V, S = DefaultHashBuilder> = hashbrown::HashMap<K, V, S>;
pub type HashSet<T, S = DefaultHashBuilder> = hashbrown::HashSet<T, S>;
pub type HashMap<K, V> = hashbrown::HashMap<K, V, DefaultHashBuilder>;
pub type HashSet<T> = hashbrown::HashSet<T, DefaultHashBuilder>;

/// A version of [`HashMap<K, V>`] using the identity hash function,
/// which is valid for any key type that can be converted to a `u64` without truncation.
pub type IntMap<K, V> = HashMap<K, V, BuildNoHashHasher<K>>;
pub type IntMap<K, V> = hashbrown::HashMap<K, V, BuildNoHashHasher<K>>;

/// A version of [`HashSet<K>`] using the identity hash function,
/// which is valid for any key type that can be converted to a `u64` without truncation.
pub type IntSet<K> = HashSet<K, BuildNoHashHasher<K>>;
pub type IntSet<K> = hashbrown::HashSet<K, BuildNoHashHasher<K>>;

pub trait HashCollectionExt {
/// Returns a new collection with default capacity, using `S::default()` to build the hasher.
Expand All @@ -27,22 +38,22 @@ pub trait HashCollectionExt {
fn with_capacity(capacity: usize) -> Self;
}

impl<K, V, S: BuildHasher + Default> HashCollectionExt for HashMap<K, V, S> {
impl<K, V, S: BuildHasher + Default> HashCollectionExt for hashbrown::HashMap<K, V, S> {
fn new() -> Self {
HashMap::with_hasher(S::default())
Self::with_hasher(S::default())
}

fn with_capacity(capacity: usize) -> Self {
HashMap::with_capacity_and_hasher(capacity, S::default())
Self::with_capacity_and_hasher(capacity, S::default())
}
}

impl<K, S: BuildHasher + Default> HashCollectionExt for HashSet<K, S> {
impl<K, S: BuildHasher + Default> HashCollectionExt for hashbrown::HashSet<K, S> {
fn new() -> Self {
HashSet::with_hasher(S::default())
Self::with_hasher(S::default())
}

fn with_capacity(capacity: usize) -> Self {
HashSet::with_capacity_and_hasher(capacity, S::default())
Self::with_capacity_and_hasher(capacity, S::default())
}
}
3 changes: 1 addition & 2 deletions crates/datastore/src/locking_tx_datastore/datastore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -738,8 +738,7 @@ impl TxMetrics {
// For each table, collect the extra stats, that we don't have in `tx_data`.
let table_stats = tx_data
.map(|tx_data| {
let mut table_stats =
<HashMap<_, _, _> as HashCollectionExt>::with_capacity(tx_data.num_tables_affected());
let mut table_stats = HashMap::with_capacity(tx_data.num_tables_affected());
for (table_id, _) in tx_data.table_ids_and_names() {
let stats = committed_state.get_table(table_id).map(|table| TableStats {
row_count: table.row_count,
Expand Down
4 changes: 2 additions & 2 deletions crates/memory-usage/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ license-file = "LICENSE"
description = "Provides the trait `MemoryUsage`"

[features]
hashbrown = ["dep:hashbrown"]
hash_map = ["dep:hashbrown"]
smallvec = ["dep:smallvec"]
ethnum = ["dep:ethnum"]
decorum = ["dep:decorum"]
default = ["hashbrown", "smallvec", "ethnum", "decorum"]
default = ["hash_map", "smallvec", "ethnum", "decorum"]

[dependencies]
hashbrown = { workspace = true, optional = true }
Expand Down
4 changes: 2 additions & 2 deletions crates/memory-usage/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ impl<T: MemoryUsage> MemoryUsage for Vec<T> {
}
}

#[cfg(feature = "hashbrown")]
#[cfg(feature = "hash_map")]
impl<K: MemoryUsage + Eq + core::hash::Hash, V: MemoryUsage, S: core::hash::BuildHasher> MemoryUsage
for hashbrown::HashMap<K, V, S>
{
Expand All @@ -112,7 +112,7 @@ impl<K: MemoryUsage + Eq + core::hash::Hash, V: MemoryUsage, S: core::hash::Buil
}
}

#[cfg(feature = "hashbrown")]
#[cfg(feature = "hash_map")]
impl<K: MemoryUsage + Eq + core::hash::Hash, S: core::hash::BuildHasher> MemoryUsage for hashbrown::HashSet<K, S> {
fn heap_usage(&self) -> usize {
self.allocation_size() + self.iter().map(|k| k.heap_usage()).sum::<usize>()
Expand Down
1 change: 0 additions & 1 deletion crates/schema/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ unicode-normalization.workspace = true
petgraph.workspace = true
serde_json.workspace = true
smallvec.workspace = true
hashbrown.workspace = true
enum-as-inner.workspace = true
enum-map.workspace = true
insta.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion crates/schema/src/auto_migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{def::*, error::PrettyAlgebraicType, identifier::Identifier};
use formatter::format_plan;
use spacetimedb_data_structures::{
error_stream::{CollectAllErrors, CombineErrors, ErrorStream},
map::HashSet,
map::{HashCollectionExt as _, HashSet},
};
use spacetimedb_lib::{
db::raw_def::v9::{RawRowLevelSecurityDefV9, TableType},
Expand Down
Loading
Loading