Skip to content

Commit

Permalink
perf: use array.push to prevent reallocation on every field (#804)
Browse files Browse the repository at this point in the history
* fix: initialize undefined optional fields upon use

* perf: use array.push to prevent reallocation on every field
  • Loading branch information
davidzeng0 authored Mar 19, 2023
1 parent ee52e06 commit a6aea2c
Show file tree
Hide file tree
Showing 6 changed files with 351 additions and 173 deletions.
96 changes: 64 additions & 32 deletions integration/groups/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,14 @@ export const GroupsOptionalTest = {
}
const startPos = reader.pos;
reader.skipType(tag & 7);
(message as any)._unknownFields[tag] = [
...((message as any)._unknownFields[tag] || []),
reader.buf.slice(startPos, reader.pos),
];
const buf = reader.buf.slice(startPos, reader.pos);
const list = (message as any)._unknownFields[tag];

if (list === undefined) {
(message as any)._unknownFields[tag] = [buf];
} else {
list.push(buf);
}
}
return message;
},
Expand Down Expand Up @@ -207,10 +211,14 @@ export const GroupsOptionalTest_Group = {
}
const startPos = reader.pos;
reader.skipType(tag & 7);
(message as any)._unknownFields[tag] = [
...((message as any)._unknownFields[tag] || []),
reader.buf.slice(startPos, reader.pos),
];
const buf = reader.buf.slice(startPos, reader.pos);
const list = (message as any)._unknownFields[tag];

if (list === undefined) {
(message as any)._unknownFields[tag] = [buf];
} else {
list.push(buf);
}
}
return message;
},
Expand Down Expand Up @@ -333,10 +341,14 @@ export const GroupsRepeatedTest = {
}
const startPos = reader.pos;
reader.skipType(tag & 7);
(message as any)._unknownFields[tag] = [
...((message as any)._unknownFields[tag] || []),
reader.buf.slice(startPos, reader.pos),
];
const buf = reader.buf.slice(startPos, reader.pos);
const list = (message as any)._unknownFields[tag];

if (list === undefined) {
(message as any)._unknownFields[tag] = [buf];
} else {
list.push(buf);
}
}
return message;
},
Expand Down Expand Up @@ -443,10 +455,14 @@ export const GroupsRepeatedTest_Group = {
}
const startPos = reader.pos;
reader.skipType(tag & 7);
(message as any)._unknownFields[tag] = [
...((message as any)._unknownFields[tag] || []),
reader.buf.slice(startPos, reader.pos),
];
const buf = reader.buf.slice(startPos, reader.pos);
const list = (message as any)._unknownFields[tag];

if (list === undefined) {
(message as any)._unknownFields[tag] = [buf];
} else {
list.push(buf);
}
}
return message;
},
Expand Down Expand Up @@ -580,10 +596,14 @@ export const GroupsNestedTest = {
}
const startPos = reader.pos;
reader.skipType(tag & 7);
(message as any)._unknownFields[tag] = [
...((message as any)._unknownFields[tag] || []),
reader.buf.slice(startPos, reader.pos),
];
const buf = reader.buf.slice(startPos, reader.pos);
const list = (message as any)._unknownFields[tag];

if (list === undefined) {
(message as any)._unknownFields[tag] = [buf];
} else {
list.push(buf);
}
}
return message;
},
Expand Down Expand Up @@ -678,10 +698,14 @@ export const GroupsNestedTest_Group = {
}
const startPos = reader.pos;
reader.skipType(tag & 7);
(message as any)._unknownFields[tag] = [
...((message as any)._unknownFields[tag] || []),
reader.buf.slice(startPos, reader.pos),
];
const buf = reader.buf.slice(startPos, reader.pos);
const list = (message as any)._unknownFields[tag];

if (list === undefined) {
(message as any)._unknownFields[tag] = [buf];
} else {
list.push(buf);
}
}
return message;
},
Expand Down Expand Up @@ -764,10 +788,14 @@ export const GroupsNestedTest_Group_Nested = {
}
const startPos = reader.pos;
reader.skipType(tag & 7);
(message as any)._unknownFields[tag] = [
...((message as any)._unknownFields[tag] || []),
reader.buf.slice(startPos, reader.pos),
];
const buf = reader.buf.slice(startPos, reader.pos);
const list = (message as any)._unknownFields[tag];

if (list === undefined) {
(message as any)._unknownFields[tag] = [buf];
} else {
list.push(buf);
}
}
return message;
},
Expand Down Expand Up @@ -850,10 +878,14 @@ export const GroupsNestedTest_Group_Nested_Nested2 = {
}
const startPos = reader.pos;
reader.skipType(tag & 7);
(message as any)._unknownFields[tag] = [
...((message as any)._unknownFields[tag] || []),
reader.buf.slice(startPos, reader.pos),
];
const buf = reader.buf.slice(startPos, reader.pos);
const list = (message as any)._unknownFields[tag];

if (list === undefined) {
(message as any)._unknownFields[tag] = [buf];
} else {
list.push(buf);
}
}
return message;
},
Expand Down
48 changes: 32 additions & 16 deletions integration/unknown-fields/google/protobuf/compiler/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,14 @@ export const Version = {
}
const startPos = reader.pos;
reader.skipType(tag & 7);
(message as any)._unknownFields[tag] = [
...((message as any)._unknownFields[tag] || []),
reader.buf.slice(startPos, reader.pos),
];
const buf = reader.buf.slice(startPos, reader.pos);
const list = (message as any)._unknownFields[tag];

if (list === undefined) {
(message as any)._unknownFields[tag] = [buf];
} else {
list.push(buf);
}
}
return message;
},
Expand Down Expand Up @@ -305,10 +309,14 @@ export const CodeGeneratorRequest = {
}
const startPos = reader.pos;
reader.skipType(tag & 7);
(message as any)._unknownFields[tag] = [
...((message as any)._unknownFields[tag] || []),
reader.buf.slice(startPos, reader.pos),
];
const buf = reader.buf.slice(startPos, reader.pos);
const list = (message as any)._unknownFields[tag];

if (list === undefined) {
(message as any)._unknownFields[tag] = [buf];
} else {
list.push(buf);
}
}
return message;
},
Expand Down Expand Up @@ -381,10 +389,14 @@ export const CodeGeneratorResponse = {
}
const startPos = reader.pos;
reader.skipType(tag & 7);
(message as any)._unknownFields[tag] = [
...((message as any)._unknownFields[tag] || []),
reader.buf.slice(startPos, reader.pos),
];
const buf = reader.buf.slice(startPos, reader.pos);
const list = (message as any)._unknownFields[tag];

if (list === undefined) {
(message as any)._unknownFields[tag] = [buf];
} else {
list.push(buf);
}
}
return message;
},
Expand Down Expand Up @@ -467,10 +479,14 @@ export const CodeGeneratorResponse_File = {
}
const startPos = reader.pos;
reader.skipType(tag & 7);
(message as any)._unknownFields[tag] = [
...((message as any)._unknownFields[tag] || []),
reader.buf.slice(startPos, reader.pos),
];
const buf = reader.buf.slice(startPos, reader.pos);
const list = (message as any)._unknownFields[tag];

if (list === undefined) {
(message as any)._unknownFields[tag] = [buf];
} else {
list.push(buf);
}
}
return message;
},
Expand Down
Loading

0 comments on commit a6aea2c

Please sign in to comment.