Skip to content

Commit cd07161

Browse files
authored
websocket format: use RawIdentifier (#4181)
# Description of Changes Adjusts the websocket format to use `RawIdentifier` instead of `Box<str>`, avoiding some allocations in the process. This seems to be a net gain of 1.5k TPS on phoenix nap. Based on #4177. # API and ABI breaking changes None # Expected complexity level and risk 1 # Testing Covered by existing tests.
1 parent 50295ac commit cd07161

8 files changed

Lines changed: 22 additions & 25 deletions

File tree

crates/client-api-messages/src/websocket.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ use spacetimedb_primitives::TableId;
2828
use spacetimedb_sats::{
2929
de::{Deserialize, Error},
3030
impl_deserialize, impl_serialize, impl_st,
31+
raw_identifier::RawIdentifier,
3132
ser::Serialize,
3233
AlgebraicType, SpacetimeType,
3334
};
@@ -151,7 +152,7 @@ impl<Args> ClientMessage<Args> {
151152
#[sats(crate = spacetimedb_lib)]
152153
pub struct CallReducer<Args> {
153154
/// The name of the reducer to call.
154-
pub reducer: Box<str>,
155+
pub reducer: RawIdentifier,
155156
/// The arguments to the reducer.
156157
///
157158
/// In the wire format, this will be a [`Bytes`], BSATN or JSON encoded according to the reducer's argument schema
@@ -312,7 +313,7 @@ pub struct OneOffQuery {
312313
/// Parametric over the argument type to enable [`ClientMessage::map_args`].
313314
pub struct CallProcedure<Args> {
314315
/// The name of the procedure to call.
315-
pub procedure: Box<str>,
316+
pub procedure: RawIdentifier,
316317
/// The arguments to the procedure.
317318
///
318319
/// In the wire format, this will be a [`Bytes`], BSATN or JSON encoded according to the reducer's argument schema
@@ -384,7 +385,7 @@ pub struct SubscribeRows<F: WebsocketFormat> {
384385
/// The table ID of the query.
385386
pub table_id: TableId,
386387
/// The table name of the query.
387-
pub table_name: Box<str>,
388+
pub table_name: RawIdentifier,
388389
/// The BSATN row values.
389390
pub table_rows: TableUpdate<F>,
390391
}
@@ -588,7 +589,7 @@ pub struct ReducerCallInfo<F: WebsocketFormat> {
588589
/// We should consider not sending this at all and instead
589590
/// having a startup message where the name <-> id bindings
590591
/// are established between the host and the client.
591-
pub reducer_name: Box<str>,
592+
pub reducer_name: RawIdentifier,
592593
/// The numerical id of the reducer that was called.
593594
pub reducer_id: u32,
594595
/// The arguments to the reducer, encoded as BSATN or JSON according to the reducer's argument schema
@@ -653,7 +654,7 @@ pub struct TableUpdate<F: WebsocketFormat> {
653654
///
654655
/// NOTE(centril, 1.0): we might want to remove this and instead
655656
/// tell clients about changes to table_name <-> table_id mappings.
656-
pub table_name: Box<str>,
657+
pub table_name: RawIdentifier,
657658
/// The sum total of rows in `self.updates`,
658659
pub num_rows: u64,
659660
/// The actual insert and delete updates for this table.
@@ -668,7 +669,7 @@ pub struct SingleQueryUpdate<F: WebsocketFormat> {
668669
}
669670

670671
impl<F: WebsocketFormat> TableUpdate<F> {
671-
pub fn new(table_id: TableId, table_name: Box<str>, update: SingleQueryUpdate<F>) -> Self {
672+
pub fn new(table_id: TableId, table_name: RawIdentifier, update: SingleQueryUpdate<F>) -> Self {
672673
Self {
673674
table_id,
674675
table_name,
@@ -677,7 +678,7 @@ impl<F: WebsocketFormat> TableUpdate<F> {
677678
}
678679
}
679680

680-
pub fn empty(table_id: TableId, table_name: Box<str>) -> Self {
681+
pub fn empty(table_id: TableId, table_name: RawIdentifier) -> Self {
681682
Self {
682683
table_id,
683684
table_name,
@@ -746,7 +747,7 @@ pub struct OneOffQueryResponse<F: WebsocketFormat> {
746747
#[sats(crate = spacetimedb_lib)]
747748
pub struct OneOffTable<F: WebsocketFormat> {
748749
/// The name of the table.
749-
pub table_name: Box<str>,
750+
pub table_name: RawIdentifier,
750751
/// The set of rows which matched the query, encoded as BSATN or JSON according to the table's schema
751752
/// and the client's requested protocol.
752753
///

crates/core/src/client/message_handlers.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ pub async fn handle(client: &ClientConnection, message: DataMessage, timer: Inst
166166
#[derive(thiserror::Error, Debug)]
167167
#[error("error executing message (reducer: {reducer:?}) (err: {err:#})")]
168168
pub struct MessageExecutionError {
169-
pub reducer: Option<Box<str>>,
169+
pub reducer: Option<RawIdentifier>,
170170
pub reducer_id: Option<ReducerId>,
171171
pub caller_identity: Identity,
172172
pub caller_connection_id: Option<ConnectionId>,

crates/core/src/client/messages.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -285,9 +285,9 @@ impl ToProtocol for TransactionUpdateMessage {
285285
reducer_name: event
286286
.function_call
287287
.reducer
288-
.as_ref()
289-
.map(|r| (&**r).into())
290-
.unwrap_or_default(),
288+
.clone()
289+
.map(Into::into)
290+
.unwrap_or_else(|| "".into()),
291291
reducer_id: event.function_call.reducer_id.into(),
292292
args,
293293
request_id,
@@ -455,7 +455,7 @@ impl ToProtocol for SubscriptionMessage {
455455
query_id,
456456
rows: ws::SubscribeRows {
457457
table_id: result.table_id,
458-
table_name: result.table_name.to_boxed_str(),
458+
table_name: result.table_name.into(),
459459
table_rows,
460460
},
461461
}
@@ -468,7 +468,7 @@ impl ToProtocol for SubscriptionMessage {
468468
query_id,
469469
rows: ws::SubscribeRows {
470470
table_id: result.table_id,
471-
table_name: result.table_name.to_boxed_str(),
471+
table_name: result.table_name.into(),
472472
table_rows,
473473
},
474474
}
@@ -486,7 +486,7 @@ impl ToProtocol for SubscriptionMessage {
486486
query_id,
487487
rows: ws::SubscribeRows {
488488
table_id: result.table_id,
489-
table_name: result.table_name.to_boxed_str(),
489+
table_name: result.table_name.into(),
490490
table_rows,
491491
},
492492
}
@@ -499,7 +499,7 @@ impl ToProtocol for SubscriptionMessage {
499499
query_id,
500500
rows: ws::SubscribeRows {
501501
table_id: result.table_id,
502-
table_name: result.table_name.to_boxed_str(),
502+
table_name: result.table_name.into(),
503503
table_rows,
504504
},
505505
}

crates/core/src/host/module_host.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2072,7 +2072,7 @@ impl ModuleHost {
20722072
.map(PipelinedProject::from)
20732073
.collect::<Vec<_>>();
20742074

2075-
let table_name = table_name.to_boxed_str();
2075+
let table_name = table_name.into();
20762076

20772077
if returns_view_table && num_private_cols > 0 {
20782078
let optimized = optimized

crates/core/src/subscription/execution_unit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ impl ExecutionUnit {
261261
let update = F::into_query_update(qu, compression);
262262
TableUpdate::new(
263263
self.return_table(),
264-
self.return_name().to_boxed_str(),
264+
self.return_name().clone().into(),
265265
SingleQueryUpdate { update, num_rows },
266266
)
267267
})

crates/core/src/subscription/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ where
203203
(
204204
TableUpdate::new(
205205
table_id,
206-
table_name.to_boxed_str(),
206+
table_name.clone().into(),
207207
SingleQueryUpdate { update, num_rows },
208208
),
209209
metrics,
@@ -239,7 +239,7 @@ pub fn collect_table_update<F: BuildableWebsocketFormat>(
239239
(
240240
TableUpdate::new(
241241
table_id,
242-
table_name.to_boxed_str(),
242+
table_name.clone().into(),
243243
SingleQueryUpdate { update, num_rows },
244244
),
245245
metrics,

crates/core/src/subscription/module_subscription_manager.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1554,7 +1554,7 @@ impl SendWorker {
15541554
.filter(|upd| !clients_with_errors.contains(&upd.id))
15551555
// Do the aggregation.
15561556
.fold(client_table_id_updates, |mut tables, upd| {
1557-
let table_name = upd.table_name.to_boxed_str();
1557+
let table_name = upd.table_name.into();
15581558
match tables.entry((upd.id, upd.table_id)) {
15591559
Entry::Occupied(mut entry) => match entry.get_mut().zip_mut(upd.update) {
15601560
Bsatn((tbl_upd, update)) => tbl_upd.push(update),

crates/schema/src/table_name.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@ impl TableName {
2020
pub fn for_test(name: &str) -> Self {
2121
Self(Identifier::for_test(name))
2222
}
23-
24-
pub fn to_boxed_str(&self) -> Box<str> {
25-
self.as_ref().into()
26-
}
2723
}
2824

2925
impl Deref for TableName {

0 commit comments

Comments
 (0)