Skip to content

Commit 9d4ac37

Browse files
authored
Replace all usages of Registry<T> with ArcSwapMap (#8919)
All our registries are read heavy but rarely require writes --------- Signed-off-by: Robert Kruszewski <[email protected]>
1 parent 3b5ccf9 commit 9d4ac37

29 files changed

Lines changed: 258 additions & 226 deletions

File tree

‎docs/developer-guide/internals/session.md‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ The session is built on two primitives from the `vortex-session` crate:
1515
- **`VortexSession`** -- a cloneable, thread-safe map from Rust `TypeId` to a shared
1616
(`Arc`-wrapped) value. Any type that is `Clone + Send + Sync + Debug + 'static` can be stored
1717
as a session variable.
18-
- **`Registry<T>`** -- a concurrent map from string IDs to values of type `T`, used by each
19-
component to look up registered plugins at runtime.
18+
- **`ArcSwapMap<K, V>`** -- a concurrent copy-on-write map, used both as the backing store of
19+
`VortexSession` itself and (keyed by interned string `Id`s) as the plugin registries each
20+
component uses to look up registered plugins at runtime.
2021

21-
Because `VortexSession` is backed by an `ArcSwap`, cloning is cheap and all clones share the
22+
Because both primitives are backed by an `ArcSwap`, cloning is cheap and all clones share the
2223
same state, with lock-free reads and copy-on-write writes. This makes it safe to hand the
2324
session to multiple threads, tasks, or I/O operations without coordination.
2425

‎encodings/zstd/src/zstd_buffers.rs‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,10 @@ impl ZstdBuffers {
109109
buffer_handles: &[BufferHandle],
110110
session: &VortexSession,
111111
) -> VortexResult<ArrayRef> {
112-
let registry = session.arrays().registry().clone();
113-
let inner_vtable = registry
114-
.find(&array.data().inner_encoding_id)
112+
let inner_vtable = session
113+
.arrays()
114+
.registry()
115+
.get(&array.data().inner_encoding_id)
115116
.ok_or_else(|| {
116117
vortex_err!("Unknown inner encoding: {}", array.data().inner_encoding_id)
117118
})?;

‎vortex-array/src/aggregate_fn/session.rs‎

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use std::any::Any;
55
use std::sync::Arc;
66

7+
use vortex_session::ArcSwapMap;
78
use vortex_session::SessionExt;
89
use vortex_session::SessionGuard;
910
use vortex_session::SessionVar;
@@ -35,7 +36,6 @@ use crate::aggregate_fn::fns::sum::Sum;
3536
use crate::aggregate_fn::fns::uncompressed_size_in_bytes::UncompressedSizeInBytes;
3637
use crate::aggregate_fn::kernels::DynAggregateKernel;
3738
use crate::aggregate_fn::kernels::DynGroupedAggregateKernel;
38-
use crate::arc_swap_map::ArcSwapMap;
3939
use crate::array::ArrayId;
4040
use crate::array::VTable;
4141
use crate::arrays::Chunked;
@@ -54,12 +54,11 @@ use crate::dtype::DType;
5454
/// [`VortexSession`](vortex_session::VortexSession).
5555
#[derive(Clone, Debug)]
5656
pub struct AggregateFnSession {
57-
registry: ArcSwapMap<AggregateFnId, AggregateFnPluginRef>,
57+
registry: AggregateFnRegistry,
5858

59-
kernels: ArcSwapMap<AggregateKernelKey, &'static dyn DynAggregateKernel>,
60-
grouped_kernels: ArcSwapMap<AggregateFnId, &'static dyn DynGroupedAggregateKernel>,
61-
grouped_encoding_kernels:
62-
ArcSwapMap<GroupedEncodingKernelKey, &'static dyn DynGroupedAggregateKernel>,
59+
kernels: AggregateKernelRegistry,
60+
grouped_kernels: GroupedKernelRegistry,
61+
grouped_encoding_kernels: GroupedEncodingKernelRegistry,
6362
}
6463

6564
impl SessionVar for AggregateFnSession {
@@ -75,13 +74,23 @@ impl SessionVar for AggregateFnSession {
7574
type AggregateKernelKey = (ArrayId, Option<AggregateFnId>);
7675
type GroupedEncodingKernelKey = (ArrayId, AggregateFnId);
7776

77+
/// Registry of aggregate function plugins, keyed by aggregate function id.
78+
type AggregateFnRegistry = ArcSwapMap<AggregateFnId, AggregateFnPluginRef>;
79+
/// Registry of aggregate kernels, keyed by encoding and optional aggregate function.
80+
type AggregateKernelRegistry = ArcSwapMap<AggregateKernelKey, &'static dyn DynAggregateKernel>;
81+
/// Registry of encoding-agnostic grouped aggregate kernels, keyed by aggregate function id.
82+
type GroupedKernelRegistry = ArcSwapMap<AggregateFnId, &'static dyn DynGroupedAggregateKernel>;
83+
/// Registry of grouped aggregate kernels, keyed by encoding and aggregate function.
84+
type GroupedEncodingKernelRegistry =
85+
ArcSwapMap<GroupedEncodingKernelKey, &'static dyn DynGroupedAggregateKernel>;
86+
7887
impl Default for AggregateFnSession {
7988
fn default() -> Self {
8089
let this = Self {
81-
registry: ArcSwapMap::default(),
82-
kernels: ArcSwapMap::default(),
83-
grouped_kernels: ArcSwapMap::default(),
84-
grouped_encoding_kernels: ArcSwapMap::default(),
90+
registry: AggregateFnRegistry::default(),
91+
kernels: AggregateKernelRegistry::default(),
92+
grouped_kernels: GroupedKernelRegistry::default(),
93+
grouped_encoding_kernels: GroupedEncodingKernelRegistry::default(),
8594
};
8695

8796
// Register the built-in aggregate functions

‎vortex-array/src/arrays/patched/vtable/mod.rs‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,12 @@ mod tests {
588588
let session = array_session();
589589
session.arrays().register(Patched);
590590

591-
let ctx = ArrayContext::empty().with_valid_ids(session.arrays().registry().ids());
591+
let ctx = ArrayContext::empty().with_allowed_ids(
592+
session
593+
.arrays()
594+
.registry()
595+
.read(|map| map.keys().copied().collect()),
596+
);
592597
let serialized = array
593598
.serialize(&ctx, &session, &SerializeOptions::default())
594599
.unwrap();

‎vortex-array/src/dtype/serde/flatbuffers.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ impl TryFrom<ViewedDType> for DType {
270270
vortex_err!("failed to parse extension metadata from flatbuffer")
271271
})?
272272
.bytes();
273-
let ext_dtype = if let Some(vtable) = vfdt.session.dtypes().registry().find(&id) {
273+
let ext_dtype = if let Some(vtable) = vfdt.session.dtypes().registry().get(&id) {
274274
vtable.deserialize(metadata, storage_dtype)?
275275
} else if vfdt.session.allows_unknown() {
276276
ForeignExtDType::from_parts(id, metadata.to_vec(), storage_dtype)?

‎vortex-array/src/dtype/serde/proto.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ impl DType {
116116
.ok_or_else(|| vortex_err!("Extension DType missing storage proto"))?,
117117
session,
118118
)?;
119-
let ext_dtype = if let Some(vtable) = session.dtypes().registry().find(&id) {
119+
let ext_dtype = if let Some(vtable) = session.dtypes().registry().get(&id) {
120120
vtable.deserialize(e.metadata(), storage_dtype)?
121121
} else if session.allows_unknown() {
122122
ForeignExtDType::from_parts(id, e.metadata().to_vec(), storage_dtype)?

‎vortex-array/src/dtype/serde/serde.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -722,7 +722,7 @@ impl<'de> DeserializeSeed<'de> for DTypeSerde<'_, ExtDTypeRef> {
722722
storage_dtype.ok_or_else(|| de::Error::missing_field("storage_dtype"))?;
723723
let metadata = metadata.ok_or_else(|| de::Error::missing_field("metadata"))?;
724724

725-
if let Some(vtable) = self.session.dtypes().registry().find(&id) {
725+
if let Some(vtable) = self.session.dtypes().registry().get(&id) {
726726
vtable.deserialize(&metadata, storage_dtype).map_err(|e| {
727727
de::Error::custom(format!(
728728
"failed to deserialize extension dtype {}: {}",

‎vortex-array/src/dtype/session.rs‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
use std::any::Any;
77
use std::sync::Arc;
88

9+
use vortex_session::ArcSwapMap;
910
use vortex_session::SessionExt;
1011
use vortex_session::SessionGuard;
1112
use vortex_session::SessionVar;
12-
use vortex_session::registry::Registry;
13+
use vortex_session::registry::Id;
1314

1415
use crate::dtype::extension::ExtDTypePluginRef;
1516
use crate::dtype::extension::ExtVTable;
@@ -19,7 +20,7 @@ use crate::extension::datetime::Timestamp;
1920
use crate::extension::uuid::Uuid;
2021

2122
/// Registry for extension dtypes.
22-
pub type ExtDTypeRegistry = Registry<ExtDTypePluginRef>;
23+
pub type ExtDTypeRegistry = ArcSwapMap<Id, ExtDTypePluginRef>;
2324

2425
/// Session for managing extension dtypes.
2526
#[derive(Clone, Debug)]
@@ -30,7 +31,7 @@ pub struct DTypeSession {
3031
impl Default for DTypeSession {
3132
fn default() -> Self {
3233
let this = Self {
33-
registry: Registry::default(),
34+
registry: ExtDTypeRegistry::default(),
3435
};
3536

3637
// Register built-in temporal extension dtypes
@@ -57,7 +58,7 @@ impl DTypeSession {
5758
/// Register an extension DType with the Vortex session.
5859
pub fn register<V: ExtVTable>(&self, vtable: V) {
5960
self.registry
60-
.register(vtable.id(), Arc::new(vtable) as ExtDTypePluginRef);
61+
.insert(vtable.id(), Arc::new(vtable) as ExtDTypePluginRef);
6162
}
6263

6364
/// Return the registry of extension dtypes.

‎vortex-array/src/expr/proto.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ impl Expression {
4747
.map(|e| Expression::from_proto(e, session))
4848
.collect::<VortexResult<Vec<_>>>()?;
4949

50-
let scalar_fn = if let Some(vtable) = session.scalar_fns().registry().find(&expr_id) {
50+
let scalar_fn = if let Some(vtable) = session.scalar_fns().registry().get(&expr_id) {
5151
vtable.deserialize(expr.metadata(), session)?
5252
} else if session.allows_unknown() {
5353
ForeignScalarFnVTable::make_scalar_fn(expr_id, expr.metadata().to_vec(), children.len())

‎vortex-array/src/lib.rs‎

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
//!
55
//! At the heart of Vortex are [arrays](ArrayRef).
66
//!
7-
//! Arrays are typed views of memory buffers that hold [scalars](crate::scalar::Scalar). These
7+
//! Arrays are typed views of memory buffers that hold [scalars](scalar::Scalar). These
88
//! buffers can be held in a number of physical encodings to perform lightweight compression that
99
//! exploits the particular data distribution of the array's values.
1010
//!
@@ -51,10 +51,10 @@
5151
//!
5252
//! # Nulls and Scalars
5353
//!
54-
//! [`Validity`](crate::validity::Validity) separates nullness from values. It can be a cheap
54+
//! [`Validity`](validity::Validity) separates nullness from values. It can be a cheap
5555
//! constant state (`NonNullable`, `AllValid`, `AllInvalid`) or a boolean array that may itself be
56-
//! encoded. [`Scalar`](crate::scalar::Scalar) is the single-value counterpart: it pairs a
57-
//! [`DType`] with an optional [`ScalarValue`](crate::scalar::ScalarValue).
56+
//! encoded. [`Scalar`](scalar::Scalar) is the single-value counterpart: it pairs a
57+
//! [`DType`] with an optional [`ScalarValue`](scalar::ScalarValue).
5858
//!
5959
//! # Extending Vortex
6060
//!
@@ -66,16 +66,16 @@
6666
//! - [`OperationsVTable`] provides scalar access.
6767
//! - [`ValidityVTable`] exposes validity only for nullable arrays.
6868
//!
69-
//! New logical extension dtypes implement [`ExtVTable`](crate::dtype::extension::ExtVTable) and
69+
//! New logical extension dtypes implement [`ExtVTable`](dtype::extension::ExtVTable) and
7070
//! store values in an ordinary Vortex storage dtype.
7171
//!
72-
//! [`PrimitiveArray`]: crate::arrays::PrimitiveArray
73-
//! [`DType`]: crate::dtype::DType
74-
//! [`ChunkedArray`]: crate::arrays::ChunkedArray
75-
//! [`ConstantArray`]: crate::arrays::ConstantArray
76-
//! [`FilterArray`]: crate::arrays::FilterArray
77-
//! [`SliceArray`]: crate::arrays::SliceArray
78-
//! [`ScalarFnArray`]: crate::arrays::ScalarFnArray
72+
//! [`PrimitiveArray`]: arrays::PrimitiveArray
73+
//! [`DType`]: dtype::DType
74+
//! [`ChunkedArray`]: arrays::ChunkedArray
75+
//! [`ConstantArray`]: arrays::ConstantArray
76+
//! [`FilterArray`]: arrays::FilterArray
77+
//! [`SliceArray`]: arrays::SliceArray
78+
//! [`ScalarFnArray`]: arrays::ScalarFnArray
7979
8080
extern crate self as vortex_array;
8181

@@ -92,7 +92,7 @@ pub use smallvec;
9292
pub use vortex_array_macros::array_slots;
9393
use vortex_session::SessionExt;
9494
use vortex_session::VortexSession;
95-
use vortex_session::registry::Context;
95+
use vortex_session::registry::Interner;
9696

9797
use crate::aggregate_fn::session::AggregateFnSession;
9898
use crate::dtype::session::DTypeSession;
@@ -105,7 +105,6 @@ use crate::stats::session::StatsSession;
105105
pub mod aggregate_fn;
106106
#[doc(hidden)]
107107
pub mod aliases;
108-
pub mod arc_swap_map;
109108
mod array;
110109
pub mod arrays;
111110
pub mod buffer;
@@ -149,7 +148,7 @@ pub mod flatbuffers {
149148
}
150149

151150
/// Register vortex-array's built-in session-scoped kernels into the active
152-
/// [`ArrayKernels`](crate::optimizer::kernels::ArrayKernels) registry.
151+
/// [`ArrayKernels`](optimizer::kernels::ArrayKernels) registry.
153152
///
154153
/// If the session contains a [`KernelSession`], this registers into its registry. Sessions that use
155154
/// [`KernelSession::default`] already receive these built-in kernels.
@@ -190,4 +189,4 @@ pub fn legacy_session() -> &'static VortexSession {
190189
&LEGACY_SESSION
191190
}
192191

193-
pub type ArrayContext = Context;
192+
pub type ArrayContext = Interner;

0 commit comments

Comments
 (0)