Skip to content

Commit

Permalink
perf: fromJSON returns object literal to allow v8 optimizations (#463)
Browse files Browse the repository at this point in the history
  • Loading branch information
boukeversteegh authored Jan 6, 2022
1 parent f2a3f02 commit 5fcd05b
Show file tree
Hide file tree
Showing 83 changed files with 1,567 additions and 1,553 deletions.
6 changes: 3 additions & 3 deletions integration/angular/simple-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ export const SimpleMessage = {
},

fromJSON(object: any): SimpleMessage {
const message = createBaseSimpleMessage();
message.numberField = isSet(object.numberField) ? Number(object.numberField) : 0;
return message;
return {
numberField: isSet(object.numberField) ? Number(object.numberField) : 0,
};
},

toJSON(message: SimpleMessage): unknown {
Expand Down
16 changes: 8 additions & 8 deletions integration/avoid-import-conflicts/simple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,10 @@ export const Simple = {
},

fromJSON(object: any): Simple {
const message = createBaseSimple();
message.name = isSet(object.name) ? String(object.name) : '';
message.otherSimple = isSet(object.otherSimple) ? Simple2.fromJSON(object.otherSimple) : undefined;
return message;
return {
name: isSet(object.name) ? String(object.name) : '',
otherSimple: isSet(object.otherSimple) ? Simple2.fromJSON(object.otherSimple) : undefined,
};
},

toJSON(message: Simple): unknown {
Expand Down Expand Up @@ -157,10 +157,10 @@ export const SimpleEnums = {
},

fromJSON(object: any): SimpleEnums {
const message = createBaseSimpleEnums();
message.localEnum = isSet(object.localEnum) ? simpleEnumFromJSON(object.localEnum) : 0;
message.importEnum = isSet(object.importEnum) ? simpleEnumFromJSON3(object.importEnum) : 0;
return message;
return {
localEnum: isSet(object.localEnum) ? simpleEnumFromJSON(object.localEnum) : 0,
importEnum: isSet(object.importEnum) ? simpleEnumFromJSON3(object.importEnum) : 0,
};
},

toJSON(message: SimpleEnums): unknown {
Expand Down
8 changes: 4 additions & 4 deletions integration/avoid-import-conflicts/simple2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ export const Simple = {
},

fromJSON(object: any): Simple {
const message = createBaseSimple();
message.name = isSet(object.name) ? String(object.name) : '';
message.age = isSet(object.age) ? Number(object.age) : 0;
return message;
return {
name: isSet(object.name) ? String(object.name) : '',
age: isSet(object.age) ? Number(object.age) : 0,
};
},

toJSON(message: Simple): unknown {
Expand Down
8 changes: 4 additions & 4 deletions integration/barrel-imports/bar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ export const Bar = {
},

fromJSON(object: any): Bar {
const message = createBaseBar();
message.name = isSet(object.name) ? String(object.name) : '';
message.age = isSet(object.age) ? Number(object.age) : 0;
return message;
return {
name: isSet(object.name) ? String(object.name) : '',
age: isSet(object.age) ? Number(object.age) : 0,
};
},

toJSON(message: Bar): unknown {
Expand Down
8 changes: 4 additions & 4 deletions integration/barrel-imports/foo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ export const Foo = {
},

fromJSON(object: any): Foo {
const message = createBaseFoo();
message.name = isSet(object.name) ? String(object.name) : '';
message.bar = isSet(object.bar) ? Bar.fromJSON(object.bar) : undefined;
return message;
return {
name: isSet(object.name) ? String(object.name) : '',
bar: isSet(object.bar) ? Bar.fromJSON(object.bar) : undefined,
};
},

toJSON(message: Foo): unknown {
Expand Down
73 changes: 39 additions & 34 deletions integration/batching-with-context/batching.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ export const BatchQueryRequest = {
},

fromJSON(object: any): BatchQueryRequest {
const message = createBaseBatchQueryRequest();
message.ids = Array.isArray(object?.ids) ? object.ids.map((e: any) => String(e)) : [];
return message;
return {
ids: Array.isArray(object?.ids) ? object.ids.map((e: any) => String(e)) : [],
};
},

toJSON(message: BatchQueryRequest): unknown {
Expand Down Expand Up @@ -130,9 +130,9 @@ export const BatchQueryResponse = {
},

fromJSON(object: any): BatchQueryResponse {
const message = createBaseBatchQueryResponse();
message.entities = Array.isArray(object?.entities) ? object.entities.map((e: any) => Entity.fromJSON(e)) : [];
return message;
return {
entities: Array.isArray(object?.entities) ? object.entities.map((e: any) => Entity.fromJSON(e)) : [],
};
},

toJSON(message: BatchQueryResponse): unknown {
Expand Down Expand Up @@ -183,9 +183,9 @@ export const BatchMapQueryRequest = {
},

fromJSON(object: any): BatchMapQueryRequest {
const message = createBaseBatchMapQueryRequest();
message.ids = Array.isArray(object?.ids) ? object.ids.map((e: any) => String(e)) : [];
return message;
return {
ids: Array.isArray(object?.ids) ? object.ids.map((e: any) => String(e)) : [],
};
},

toJSON(message: BatchMapQueryRequest): unknown {
Expand Down Expand Up @@ -239,12 +239,14 @@ export const BatchMapQueryResponse = {
},

fromJSON(object: any): BatchMapQueryResponse {
const message = createBaseBatchMapQueryResponse();
message.entities = Object.entries(object.entities ?? {}).reduce<{ [key: string]: Entity }>((acc, [key, value]) => {
acc[key] = Entity.fromJSON(value);
return acc;
}, {});
return message;
return {
entities: isObject(object.entities)
? Object.entries(object.entities).reduce<{ [key: string]: Entity }>((acc, [key, value]) => {
acc[key] = Entity.fromJSON(value);
return acc;
}, {})
: {},
};
},

toJSON(message: BatchMapQueryResponse): unknown {
Expand Down Expand Up @@ -307,10 +309,10 @@ export const BatchMapQueryResponse_EntitiesEntry = {
},

fromJSON(object: any): BatchMapQueryResponse_EntitiesEntry {
const message = createBaseBatchMapQueryResponse_EntitiesEntry();
message.key = isSet(object.key) ? String(object.key) : '';
message.value = isSet(object.value) ? Entity.fromJSON(object.value) : undefined;
return message;
return {
key: isSet(object.key) ? String(object.key) : '',
value: isSet(object.value) ? Entity.fromJSON(object.value) : undefined,
};
},

toJSON(message: BatchMapQueryResponse_EntitiesEntry): unknown {
Expand Down Expand Up @@ -361,9 +363,9 @@ export const GetOnlyMethodRequest = {
},

fromJSON(object: any): GetOnlyMethodRequest {
const message = createBaseGetOnlyMethodRequest();
message.id = isSet(object.id) ? String(object.id) : '';
return message;
return {
id: isSet(object.id) ? String(object.id) : '',
};
},

toJSON(message: GetOnlyMethodRequest): unknown {
Expand Down Expand Up @@ -410,9 +412,9 @@ export const GetOnlyMethodResponse = {
},

fromJSON(object: any): GetOnlyMethodResponse {
const message = createBaseGetOnlyMethodResponse();
message.entity = isSet(object.entity) ? Entity.fromJSON(object.entity) : undefined;
return message;
return {
entity: isSet(object.entity) ? Entity.fromJSON(object.entity) : undefined,
};
},

toJSON(message: GetOnlyMethodResponse): unknown {
Expand Down Expand Up @@ -460,9 +462,9 @@ export const WriteMethodRequest = {
},

fromJSON(object: any): WriteMethodRequest {
const message = createBaseWriteMethodRequest();
message.id = isSet(object.id) ? String(object.id) : '';
return message;
return {
id: isSet(object.id) ? String(object.id) : '',
};
},

toJSON(message: WriteMethodRequest): unknown {
Expand Down Expand Up @@ -503,8 +505,7 @@ export const WriteMethodResponse = {
},

fromJSON(_: any): WriteMethodResponse {
const message = createBaseWriteMethodResponse();
return message;
return {};
},

toJSON(_: WriteMethodResponse): unknown {
Expand Down Expand Up @@ -555,10 +556,10 @@ export const Entity = {
},

fromJSON(object: any): Entity {
const message = createBaseEntity();
message.id = isSet(object.id) ? String(object.id) : '';
message.name = isSet(object.name) ? String(object.name) : '';
return message;
return {
id: isSet(object.id) ? String(object.id) : '',
name: isSet(object.name) ? String(object.name) : '',
};
},

toJSON(message: Entity): unknown {
Expand Down Expand Up @@ -697,6 +698,10 @@ if (util.Long !== Long) {
configure();
}

function isObject(value: any): boolean {
return typeof value === 'object' && value !== null;
}

function isSet(value: any): boolean {
return value !== null && value !== undefined;
}
73 changes: 39 additions & 34 deletions integration/batching/batching.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ export const BatchQueryRequest = {
},

fromJSON(object: any): BatchQueryRequest {
const message = createBaseBatchQueryRequest();
message.ids = Array.isArray(object?.ids) ? object.ids.map((e: any) => String(e)) : [];
return message;
return {
ids: Array.isArray(object?.ids) ? object.ids.map((e: any) => String(e)) : [],
};
},

toJSON(message: BatchQueryRequest): unknown {
Expand Down Expand Up @@ -128,9 +128,9 @@ export const BatchQueryResponse = {
},

fromJSON(object: any): BatchQueryResponse {
const message = createBaseBatchQueryResponse();
message.entities = Array.isArray(object?.entities) ? object.entities.map((e: any) => Entity.fromJSON(e)) : [];
return message;
return {
entities: Array.isArray(object?.entities) ? object.entities.map((e: any) => Entity.fromJSON(e)) : [],
};
},

toJSON(message: BatchQueryResponse): unknown {
Expand Down Expand Up @@ -181,9 +181,9 @@ export const BatchMapQueryRequest = {
},

fromJSON(object: any): BatchMapQueryRequest {
const message = createBaseBatchMapQueryRequest();
message.ids = Array.isArray(object?.ids) ? object.ids.map((e: any) => String(e)) : [];
return message;
return {
ids: Array.isArray(object?.ids) ? object.ids.map((e: any) => String(e)) : [],
};
},

toJSON(message: BatchMapQueryRequest): unknown {
Expand Down Expand Up @@ -237,12 +237,14 @@ export const BatchMapQueryResponse = {
},

fromJSON(object: any): BatchMapQueryResponse {
const message = createBaseBatchMapQueryResponse();
message.entities = Object.entries(object.entities ?? {}).reduce<{ [key: string]: Entity }>((acc, [key, value]) => {
acc[key] = Entity.fromJSON(value);
return acc;
}, {});
return message;
return {
entities: isObject(object.entities)
? Object.entries(object.entities).reduce<{ [key: string]: Entity }>((acc, [key, value]) => {
acc[key] = Entity.fromJSON(value);
return acc;
}, {})
: {},
};
},

toJSON(message: BatchMapQueryResponse): unknown {
Expand Down Expand Up @@ -305,10 +307,10 @@ export const BatchMapQueryResponse_EntitiesEntry = {
},

fromJSON(object: any): BatchMapQueryResponse_EntitiesEntry {
const message = createBaseBatchMapQueryResponse_EntitiesEntry();
message.key = isSet(object.key) ? String(object.key) : '';
message.value = isSet(object.value) ? Entity.fromJSON(object.value) : undefined;
return message;
return {
key: isSet(object.key) ? String(object.key) : '',
value: isSet(object.value) ? Entity.fromJSON(object.value) : undefined,
};
},

toJSON(message: BatchMapQueryResponse_EntitiesEntry): unknown {
Expand Down Expand Up @@ -359,9 +361,9 @@ export const GetOnlyMethodRequest = {
},

fromJSON(object: any): GetOnlyMethodRequest {
const message = createBaseGetOnlyMethodRequest();
message.id = isSet(object.id) ? String(object.id) : '';
return message;
return {
id: isSet(object.id) ? String(object.id) : '',
};
},

toJSON(message: GetOnlyMethodRequest): unknown {
Expand Down Expand Up @@ -408,9 +410,9 @@ export const GetOnlyMethodResponse = {
},

fromJSON(object: any): GetOnlyMethodResponse {
const message = createBaseGetOnlyMethodResponse();
message.entity = isSet(object.entity) ? Entity.fromJSON(object.entity) : undefined;
return message;
return {
entity: isSet(object.entity) ? Entity.fromJSON(object.entity) : undefined,
};
},

toJSON(message: GetOnlyMethodResponse): unknown {
Expand Down Expand Up @@ -458,9 +460,9 @@ export const WriteMethodRequest = {
},

fromJSON(object: any): WriteMethodRequest {
const message = createBaseWriteMethodRequest();
message.id = isSet(object.id) ? String(object.id) : '';
return message;
return {
id: isSet(object.id) ? String(object.id) : '',
};
},

toJSON(message: WriteMethodRequest): unknown {
Expand Down Expand Up @@ -501,8 +503,7 @@ export const WriteMethodResponse = {
},

fromJSON(_: any): WriteMethodResponse {
const message = createBaseWriteMethodResponse();
return message;
return {};
},

toJSON(_: WriteMethodResponse): unknown {
Expand Down Expand Up @@ -553,10 +554,10 @@ export const Entity = {
},

fromJSON(object: any): Entity {
const message = createBaseEntity();
message.id = isSet(object.id) ? String(object.id) : '';
message.name = isSet(object.name) ? String(object.name) : '';
return message;
return {
id: isSet(object.id) ? String(object.id) : '',
name: isSet(object.name) ? String(object.name) : '',
};
},

toJSON(message: Entity): unknown {
Expand Down Expand Up @@ -645,6 +646,10 @@ if (util.Long !== Long) {
configure();
}

function isObject(value: any): boolean {
return typeof value === 'object' && value !== null;
}

function isSet(value: any): boolean {
return value !== null && value !== undefined;
}
Loading

0 comments on commit 5fcd05b

Please sign in to comment.