Skip to content
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

V2: Speed up reflect with classes #837

Merged
merged 1 commit into from
May 10, 2024
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
2 changes: 1 addition & 1 deletion packages/protobuf-bench/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ server would usually do.

| code generator | bundle size | minified | compressed |
|---------------------|------------------------:|-----------------------:|-------------------:|
| protobuf-es | 126,105 b | 65,235 b | 15,901 b |
| protobuf-es | 126,685 b | 66,254 b | 15,996 b |
| protobuf-javascript | 394,384 b | 288,654 b | 45,122 b |
27 changes: 21 additions & 6 deletions packages/protobuf/src/reflect/reflect-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export interface ReflectMessage {
*/
addListItem<Field extends DescField & { fieldKind: "list" }>(
field: Field,
value: NewListItem<Field>,
value: ReflectAddListItemValue<Field>,
): FieldError | undefined;

/**
Expand All @@ -153,7 +153,7 @@ export interface ReflectMessage {
setMapEntry<Field extends DescField & { fieldKind: "map" }>(
field: Field,
key: MapEntryKey,
value: NewMapEntryValue<Field>,
value: ReflectSetMapEntryValue<Field>,
): FieldError | undefined;

/**
Expand Down Expand Up @@ -264,12 +264,18 @@ export interface ReflectMap<K extends MapEntryKey = MapEntryKey, V = unknown>
[unsafeLocal]: Record<string, unknown>;
}

/**
* A ReflectMap key.
*/
export type MapEntryKey = string | number | bigint | boolean;

type enumVal = number;

/**
* The return type of ReflectMessage.get()
*/
// prettier-ignore
type ReflectGetValue<Field extends DescField = DescField> = (
export type ReflectGetValue<Field extends DescField = DescField> = (
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't infer all types with a class implementation, so we have to export this type. Another disadvantage is that classes are more verbose, hence the small bump in bundle size.

Field extends { fieldKind: "map" } ? (
Field extends { mapKind: "message" } ? ReflectMap<MapEntryKey, ReflectMessage> :
Field extends { mapKind: "enum" } ? ReflectMap<MapEntryKey, enumVal> :
Expand All @@ -283,8 +289,11 @@ type ReflectGetValue<Field extends DescField = DescField> = (
never
);

/**
* The type of the "value" argument of ReflectMessage.set()
*/
// prettier-ignore
type ReflectSetValue<Field extends DescField = DescField> = (
export type ReflectSetValue<Field extends DescField = DescField> = (
Field extends { fieldKind: "map" } ? ReflectMap :
Field extends { fieldKind: "list" } ? ReflectList :
Field extends { fieldKind: "enum" } ? number :
Expand All @@ -293,16 +302,22 @@ type ReflectSetValue<Field extends DescField = DescField> = (
never
);

/**
* The type of the "value" argument of ReflectMessage.addListItem()
*/
// prettier-ignore
type NewListItem<Field extends DescField & { fieldKind: "list" }> = (
export type ReflectAddListItemValue<Field extends DescField & { fieldKind: "list" }> = (
Field extends { listKind: "scalar"; scalar: infer T } ? ScalarValue<T> :
Field extends { listKind: "enum" } ? enumVal :
Field extends { listKind: "message" } ? ReflectMessage :
never
);

/**
* The type of the "value" argument of ReflectMessage.setMapEntry()
*/
// prettier-ignore
type NewMapEntryValue<Field extends DescField & { fieldKind: "map" }> = (
export type ReflectSetMapEntryValue<Field extends DescField & { fieldKind: "map" }> = (
Field extends { mapKind: "enum" } ? enumVal :
Field extends { mapKind: "message" } ? ReflectMessage :
Field extends { mapKind: "scalar"; scalar: infer T } ? ScalarValue<T, LongType.BIGINT> :
Expand Down
Loading