Skip to content

Improve performance of row ids #180

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Jul 14, 2025
Merged
Show file tree
Hide file tree
Changes from 7 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

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

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

5 changes: 5 additions & 0 deletions examples/quickstart-chat/src/module_bindings/index.ts

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

2 changes: 2 additions & 0 deletions examples/quickstart-chat/src/module_bindings/message_table.ts

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

2 changes: 2 additions & 0 deletions examples/quickstart-chat/src/module_bindings/message_type.ts

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

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

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

2 changes: 2 additions & 0 deletions examples/quickstart-chat/src/module_bindings/user_table.ts

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

2 changes: 2 additions & 0 deletions examples/quickstart-chat/src/module_bindings/user_type.ts

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

3 changes: 3 additions & 0 deletions packages/sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,8 @@
"@clockworklabs/test-app": "file:../test-app",
"tsup": "^8.1.0",
"undici": "^6.19.2"
},
"dependencies": {
"base64-js": "^1.5.1"
}
}
63 changes: 61 additions & 2 deletions packages/sdk/src/algebraic_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { TimeDuration } from './time_duration';
import { Timestamp } from './timestamp';
import { ConnectionId } from './connection_id';
import type BinaryReader from './binary_reader';
import type BinaryWriter from './binary_writer';
import BinaryWriter from './binary_writer';
import { Identity } from './identity';
import ScheduleAt from './schedule_at';

Expand Down Expand Up @@ -164,7 +164,31 @@ export class ProductType {
}
};

deserialize = (reader: BinaryReader): any => {
intoMapKey(value: any): ComparablePrimitive {
if (this.elements.length === 1) {
if (this.elements[0].name === '__time_duration_micros__') {
return (value as TimeDuration).__time_duration_micros__;
}

if (this.elements[0].name === '__timestamp_micros_since_unix_epoch__') {
return (value as Timestamp).__timestamp_micros_since_unix_epoch__;
}

if (this.elements[0].name === '__identity__') {
return (value as Identity).__identity__;
}

if (this.elements[0].name === '__connection_id__') {
return (value as ConnectionId).__connection_id__;
}
}
// The fallback is to serialize and base64 encode the bytes.
const writer = new BinaryWriter(10);
this.serialize(writer, value);
return writer.toBase64();
}

deserialize = (reader: BinaryReader): { [key: string]: any } => {
let result: { [key: string]: any } = {};
if (this.elements.length === 1) {
if (this.elements[0].name === '__time_duration_micros__') {
Expand Down Expand Up @@ -216,6 +240,8 @@ type AnyType =
| TypeRef
| None;

export type ComparablePrimitive = number | string | String | boolean | bigint;

/**
* The SpacetimeDB Algebraic Type System (SATS) is a structural type system in
* which a nominal type system can be constructed.
Expand Down Expand Up @@ -449,6 +475,39 @@ export class AlgebraicType {
return this.#isI64Newtype('__time_duration_micros__');
}

/**
* Convert a value of the algebraic type into something that can be used as a key in a map.
* There are no guarantees about being able to order it.
* This is only guaranteed to be comparable to other values of the same type.
* @param value A value of the algebraic type
* @returns Something that can be used as a key in a map.
*/
intoMapKey(value: any): ComparablePrimitive {
switch (this.type) {
case Type.U8:
case Type.U16:
case Type.U32:
case Type.U64:
case Type.U128:
case Type.U256:
case Type.I8:
case Type.I16:
case Type.I64:
case Type.I128:
case Type.F32:
case Type.F64:
case Type.String:
case Type.Bool:
return value;
case Type.ProductType:
return this.product.intoMapKey(value);
default:
const writer = new BinaryWriter(10);
this.serialize(writer, value);
return writer.toBase64();
}
}

serialize(writer: BinaryWriter, value: any): void {
switch (this.type) {
case Type.ProductType:
Expand Down
6 changes: 6 additions & 0 deletions packages/sdk/src/binary_writer.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { fromByteArray } from 'base64-js';

export default class BinaryWriter {
#buffer: Uint8Array;
#view: DataView;
Expand All @@ -19,6 +21,10 @@ export default class BinaryWriter {
this.#view = new DataView(this.#buffer.buffer);
}

toBase64(): string {
return fromByteArray(this.#buffer.subarray(0, this.#offset));
}

getBuffer(): Uint8Array {
return this.#buffer.slice(0, this.#offset);
}
Expand Down
Loading