Skip to content

Commit

Permalink
perf: speed up the SFU WS message decoding (#1224)
Browse files Browse the repository at this point in the history
Includes the newly generated protobuf-ts models based on
GetStream/protocol#347
The newest version of `protobuf-ts` includes an important performance
optimization that we can benefit from in our SFU WS message decoding
flow.

More about it: timostamm/protobuf-ts#582
  • Loading branch information
oliverlaz authored Dec 26, 2023
1 parent 3814113 commit 986bd6d
Show file tree
Hide file tree
Showing 8 changed files with 240 additions and 412 deletions.
6 changes: 3 additions & 3 deletions packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
"CHANGELOG.md"
],
"dependencies": {
"@protobuf-ts/runtime": "^2.9.1",
"@protobuf-ts/runtime-rpc": "^2.9.1",
"@protobuf-ts/twirp-transport": "^2.9.1",
"@protobuf-ts/runtime": "^2.9.3",
"@protobuf-ts/runtime-rpc": "^2.9.3",
"@protobuf-ts/twirp-transport": "^2.9.3",
"@types/ws": "^8.5.7",
"axios": "^1.6.0",
"base64-js": "^1.5.1",
Expand Down
31 changes: 12 additions & 19 deletions packages/client/src/gen/google/protobuf/struct.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable */
// @generated by protobuf-ts 2.9.0 with parameter long_type_string,client_generic,server_none,eslint_disable
// @generated by protobuf-ts 2.9.3 with parameter long_type_string,client_generic,server_none,eslint_disable
// @generated from protobuf file "google/protobuf/struct.proto" (package "google.protobuf", syntax proto3)
// tslint:disable
//
Expand Down Expand Up @@ -46,7 +46,6 @@ import type {
} from '@protobuf-ts/runtime';
import {
isJsonObject,
MESSAGE_TYPE,
MessageType,
reflectionMergePartial,
typeofJsonValue,
Expand Down Expand Up @@ -226,11 +225,8 @@ class Struct$Type extends MessageType<Struct> {
return target;
}
create(value?: PartialMessage<Struct>): Struct {
const message = { fields: {} };
globalThis.Object.defineProperty(message, MESSAGE_TYPE, {
enumerable: false,
value: this,
});
const message = globalThis.Object.create(this.messagePrototype!);
message.fields = {};
if (value !== undefined)
reflectionMergePartial<Struct>(this, message, value);
return message;
Expand Down Expand Up @@ -300,7 +296,7 @@ class Struct$Type extends MessageType<Struct> {
options: BinaryWriteOptions,
): IBinaryWriter {
/* map<string, google.protobuf.Value> fields = 1; */
for (let k of Object.keys(message.fields)) {
for (let k of globalThis.Object.keys(message.fields)) {
writer
.tag(1, WireType.LengthDelimited)
.fork()
Expand Down Expand Up @@ -385,7 +381,10 @@ class Value$Type extends MessageType<Value> {
case 'nullValue':
return null;
case 'numberValue':
return message.kind.numberValue;
let numberValue = message.kind.numberValue;
if (typeof numberValue == 'number' && !Number.isFinite(numberValue))
throw new globalThis.Error();
return numberValue;
case 'stringValue':
return message.kind.stringValue;
case 'listValue':
Expand Down Expand Up @@ -446,11 +445,8 @@ class Value$Type extends MessageType<Value> {
return target;
}
create(value?: PartialMessage<Value>): Value {
const message = { kind: { oneofKind: undefined } };
globalThis.Object.defineProperty(message, MESSAGE_TYPE, {
enumerable: false,
value: this,
});
const message = globalThis.Object.create(this.messagePrototype!);
message.kind = { oneofKind: undefined };
if (value !== undefined)
reflectionMergePartial<Value>(this, message, value);
return message;
Expand Down Expand Up @@ -616,11 +612,8 @@ class ListValue$Type extends MessageType<ListValue> {
return target;
}
create(value?: PartialMessage<ListValue>): ListValue {
const message = { values: [] };
globalThis.Object.defineProperty(message, MESSAGE_TYPE, {
enumerable: false,
value: this,
});
const message = globalThis.Object.create(this.messagePrototype!);
message.values = [];
if (value !== undefined)
reflectionMergePartial<ListValue>(this, message, value);
return message;
Expand Down
11 changes: 4 additions & 7 deletions packages/client/src/gen/google/protobuf/timestamp.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable */
// @generated by protobuf-ts 2.9.0 with parameter long_type_string,client_generic,server_none,eslint_disable
// @generated by protobuf-ts 2.9.3 with parameter long_type_string,client_generic,server_none,eslint_disable
// @generated from protobuf file "google/protobuf/timestamp.proto" (package "google.protobuf", syntax proto3)
// tslint:disable
//
Expand Down Expand Up @@ -44,7 +44,6 @@ import type {
PartialMessage,
} from '@protobuf-ts/runtime';
import {
MESSAGE_TYPE,
MessageType,
PbLong,
reflectionMergePartial,
Expand Down Expand Up @@ -285,11 +284,9 @@ class Timestamp$Type extends MessageType<Timestamp> {
return target;
}
create(value?: PartialMessage<Timestamp>): Timestamp {
const message = { seconds: '0', nanos: 0 };
globalThis.Object.defineProperty(message, MESSAGE_TYPE, {
enumerable: false,
value: this,
});
const message = globalThis.Object.create(this.messagePrototype!);
message.seconds = '0';
message.nanos = 0;
if (value !== undefined)
reflectionMergePartial<Timestamp>(this, message, value);
return message;
Expand Down
Loading

0 comments on commit 986bd6d

Please sign in to comment.