Skip to content

Commit

Permalink
V2: Avoid Array.flatMap (#904)
Browse files Browse the repository at this point in the history
  • Loading branch information
timostamm authored Jun 26, 2024
1 parent 793ff3b commit a4863aa
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 36 deletions.
10 changes: 5 additions & 5 deletions packages/bundle-size/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ usually do. We repeat this for an increasing number of files.
<!--- TABLE-START -->
| code generator | files | bundle size | minified | compressed |
|-----------------|----------|------------------------:|-----------------------:|-------------------:|
| protobuf-es | 1 | 125,833 b | 65,603 b | 15,257 b |
| protobuf-es | 4 | 128,022 b | 67,111 b | 15,948 b |
| protobuf-es | 8 | 130,784 b | 68,882 b | 16,433 b |
| protobuf-es | 16 | 141,234 b | 76,863 b | 18,756 b |
| protobuf-es | 32 | 169,025 b | 98,881 b | 24,210 b |
| protobuf-es | 1 | 125,817 b | 65,597 b | 15,272 b |
| protobuf-es | 4 | 128,006 b | 67,105 b | 15,919 b |
| protobuf-es | 8 | 130,768 b | 68,876 b | 16,418 b |
| protobuf-es | 16 | 141,218 b | 76,857 b | 18,753 b |
| protobuf-es | 32 | 169,009 b | 98,875 b | 24,198 b |
| protobuf-javascript | 1 | 339,613 b | 255,820 b | 42,481 b |
| protobuf-javascript | 4 | 366,281 b | 271,092 b | 43,912 b |
| protobuf-javascript | 8 | 388,324 b | 283,409 b | 45,038 b |
Expand Down
12 changes: 6 additions & 6 deletions packages/bundle-size/chart.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 3 additions & 4 deletions packages/protobuf-conformance/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
"include": ["src/**/*.ts"],
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"lib": [
"ES2016",
// ES2020.BigInt for the bigint representation of 64-bit integers
"ES2020.BigInt"
"types": [
// The conformance testee uses Node.js APIs
"node"
]
}
}
5 changes: 3 additions & 2 deletions packages/protobuf-example/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{
"files": ["src/add-person.ts", "src/list-people.ts"],
"compilerOptions": {
"target": "es2016",
"target": "es2017",
"moduleResolution": "Node10",
"strict": true,
"moduleResolution": "Node"
"forceConsistentCasingInFileNames": true
}
}
6 changes: 6 additions & 0 deletions packages/protobuf/src/proto-int64.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,12 @@ interface Int64Support {
uDec(lo: number, hi: number): bigint;
}

// The environment variable BUF_BIGINT_DISABLE=1 disables bigint support for
// testing.
declare const process: {
env: Record<string, string>;
};

function makeInt64Support(): Int64Support {
const dv = new DataView(new ArrayBuffer(8));
// note that Safari 14 implements BigInt, but not the DataView methods
Expand Down
2 changes: 1 addition & 1 deletion packages/protobuf/src/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ export function createFileRegistry(
deps.push(dep);
}
}
return [...deps, ...deps.flatMap((dep) => recurseDeps(dep))];
return deps.concat(...deps.map(recurseDeps));
}
for (const file of [input, ...recurseDeps(input)].reverse()) {
addFile(file, registry);
Expand Down
21 changes: 19 additions & 2 deletions packages/protobuf/src/wire/text-encoding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,12 @@ export function configureTextEncoding(textEncoding: TextEncoding): void {

export function getTextEncoding() {
if ((globalThis as GlobalWithTextEncoding)[symbol] == undefined) {
const te = new globalThis.TextEncoder();
const td = new globalThis.TextDecoder();
const te = new (
globalThis as unknown as GlobalWithTextEncoderDecoder
).TextEncoder();
const td = new (
globalThis as unknown as GlobalWithTextEncoderDecoder
).TextDecoder();
(globalThis as GlobalWithTextEncoding)[symbol] = {
encodeUtf8(text: string): Uint8Array {
return te.encode(text);
Expand All @@ -69,3 +73,16 @@ export function getTextEncoding() {
type GlobalWithTextEncoding = {
[symbol]?: TextEncoding;
};

type GlobalWithTextEncoderDecoder = {
TextEncoder: {
new (): {
encode(text: string): Uint8Array;
};
};
TextDecoder: {
new (): {
decode(data: Uint8Array): string;
};
};
};
11 changes: 1 addition & 10 deletions packages/protobuf/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,5 @@
"src/wkt/index.ts",
"src/wire/index.ts"
],
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"lib": [
"ES2017",
// ES2020.BigInt for the bigint representation of 64-bit integers
// Note that these are only required for using bigint literals
// or the BigInt constructor function.
"ES2020.BigInt"
]
}
"extends": "../../tsconfig.base.json"
}
4 changes: 1 addition & 3 deletions packages/protoplugin-example/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
"compilerOptions": {
"target": "es2017",
"moduleResolution": "Node10",
"esModuleInterop": false,
"resolveJsonModule": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"verbatimModuleSyntax": true
"forceConsistentCasingInFileNames": true
}
}
10 changes: 9 additions & 1 deletion packages/protoplugin/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"esModuleInterop": true,
"allowSyntheticDefaultImports": true
"allowSyntheticDefaultImports": true,
"types": [
// src/run-node.ts uses Node.js APIs
"node"
],
"lib": [
// @typescript/vfs references the LocalStorage type
"dom"
]
}
}
19 changes: 17 additions & 2 deletions tsconfig.base.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
{
"compilerOptions": {
// We target ES2017, but require the Text Encoding API, and optionally
// BigInt and AsyncIterable, see below.
"target": "es2017",
"lib": [
"ES2017",
// ES2020.BigInt for the bigint representation of 64-bit integers
// Note that these are only required for using bigint literals
// or the BigInt constructor function.
"ES2020.BigInt",
// AsyncIterable is required only for src/wire/size-delimited.ts.
"ES2018.AsyncIterable"
],
// Prevent tsc from picking up @types/node and others
"types": [],
// We don't have dependencies that require interop
"esModuleInterop": false,
"forceConsistentCasingInFileNames": true,
// As strict as possible
"strict": true,
"forceConsistentCasingInFileNames": true,
"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
Expand All @@ -15,7 +30,7 @@
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"noImplicitOverride": true,
// We need node's module resolution, so we do not have to skip lib checks
// We're building with Node16 module resolution
"moduleResolution": "Node16",
"module": "Node16",
"verbatimModuleSyntax": true,
Expand Down

0 comments on commit a4863aa

Please sign in to comment.