Skip to content
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ node_modules
/packages/*/dist
/packages/typescript-compat/*/dist
/packages/protobuf-test/*.0x
/packages/protobuf-test/*-v8.log
/.tmp
/packages/*/.tmp
20 changes: 19 additions & 1 deletion package-lock.json

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

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 | 125,973 b | 65,131 b | 15,859 b |
| protobuf-es | 126,105 b | 65,235 b | 15,901 b |
| protobuf-javascript | 394,384 b | 288,654 b | 45,122 b |
2 changes: 2 additions & 0 deletions packages/protobuf-test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"generate:js": "protoc --es_out=src/gen/js --es_opt=ts_nocheck=false,target=js+dts,import_extension=.js --proto_path=. $(buf ls-files extra) --proto_path=$(upstream-include test) $(upstream-files test) google/protobuf/type.proto",
"postgenerate": "license-header src/gen",
"perf": "tsx src/perf.ts benchmark '.*'",
"profile": "dexnode dist/esm/perf.js run 'fromBinary perf-payload.bin' 10000",
Copy link
Member Author

Choose a reason for hiding this comment

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

The resulting logs can be inspected with the deopt explorer. Seems worth keeping this script and dep.

"flamegraph": "npx 0x -- node dist/esm/perf.js run 'fromBinary perf-payload.bin' 10000",
"test": "npm run test:bigint && npm run test:string",
"test:bigint": "BUF_BIGINT_DISABLE=0 NODE_OPTIONS=--experimental-vm-modules npx jest",
Expand All @@ -29,6 +30,7 @@
"@types/benchmark": "^2.1.5",
"0x": "^5.7.0",
"benchmark": "^2.1.4",
"dexnode": "^1.2.2",
"long": "~5.2.3",
"upstream-protobuf": "*"
}
Expand Down
9 changes: 4 additions & 5 deletions packages/protobuf/src/from-binary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,19 +158,18 @@ export function readField(
readListField(message, reader, options, field, wireType);
break;
case "map":
// eslint-disable-next-line no-case-declarations
const [key, val] = readMapEntry(field, reader, options);
message.setMapEntry(field, key, val);
readMapEntry(message, field, reader, options);
Copy link
Member Author

Choose a reason for hiding this comment

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

Avoiding an eager bailout here.

break;
}
}

// Read a map field, expecting key field = 1, value field = 2
function readMapEntry(
message: ReflectMessage,
field: DescField & { fieldKind: "map" },
reader: BinaryReader,
options: BinaryReadOptions,
): [MapEntryKey, ScalarValue | ReflectMessage] {
): void {
let key: MapEntryKey | undefined,
val: ScalarValue | ReflectMessage | undefined;
const end = reader.pos + reader.uint32();
Expand Down Expand Up @@ -211,7 +210,7 @@ function readMapEntry(
break;
}
}
return [key, val];
message.setMapEntry(field, key, val);
}

function readListField(
Expand Down
9 changes: 8 additions & 1 deletion packages/protobuf/src/reflect/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -784,6 +784,7 @@ function newField(
| keyof (DescField & { fieldKind: "list" })
| keyof (DescField & { fieldKind: "scalar" });
const field: Partial<Record<AllKeys, unknown>> = {
kind: "field",
proto,
deprecated: proto.options?.deprecated ?? false,
name: proto.name,
Expand All @@ -792,6 +793,13 @@ function newField(
message: undefined,
enum: undefined,
presence: getFieldPresence(proto, oneof, isExtension, parentOrFile),
listKind: undefined,
mapKind: undefined,
mapKey: undefined,
delimitedEncoding: undefined,
Comment on lines +796 to +799
Copy link
Member Author

Choose a reason for hiding this comment

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

Adding these properties means that the JIT can use the same map. It adds some bytes to the bundle size, but that's always the trade-off. I think it's worth it here for the >10% gain.

packed: undefined,
longType: undefined,
getDefaultValue: undefined,
};
if (isExtension) {
// extension field
Expand All @@ -815,7 +823,6 @@ function newField(
// regular field
const parent = parentOrFile;
assert(parent.kind == "message");
field.kind = "field";
field.parent = parent;
field.oneof = oneof;
field.localName = oneof
Expand Down