Skip to content

Commit e4344f4

Browse files
perf: merge attributesBaseNames and baseNameToAttr into a single attributesMap pass
Previously the keepAllInAdditional path rebuilt a baseNameToAttr reverse map inside the conditional on every deserialization call. Hoisting it to a single combined loop halves the attributesMap iterations per call for flagged models. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent c162b49 commit e4344f4

2 files changed

Lines changed: 24 additions & 24 deletions

File tree

packages/datadog-api-client-v1/models/ObjectSerializer.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3339,11 +3339,18 @@ export class ObjectSerializer {
33393339

33403340
const instance = new typeMap[type]();
33413341
const attributesMap = typeMap[type].getAttributeTypeMap();
3342-
const attributesBaseNames = Object.keys(attributesMap).reduce(
3343-
(o, key) => Object.assign(o, { [attributesMap[key].baseName]: "" }),
3344-
{}
3345-
);
33463342
const keepAllInAdditional = (typeMap[type] as any)._keepTypedInAdditionalProperties === true;
3343+
// Single pass: build attributesBaseNames (for extra-key detection) and, when needed,
3344+
// baseNameToAttr (for per-field typed deserialization preserving int64 precision).
3345+
const attributesBaseNames: { [key: string]: string } = {};
3346+
const baseNameToAttr: { [key: string]: any } = keepAllInAdditional ? {} : null;
3347+
for (const attrName in attributesMap) {
3348+
const baseName = attributesMap[attrName].baseName;
3349+
attributesBaseNames[baseName] = "";
3350+
if (keepAllInAdditional && attrName !== "additionalProperties") {
3351+
baseNameToAttr[baseName] = attributesMap[attrName];
3352+
}
3353+
}
33473354
const extraAttributes = Object.keys(data).filter(
33483355
(key) => keepAllInAdditional || !Object.prototype.hasOwnProperty.call(attributesBaseNames, key)
33493356
);
@@ -3352,16 +3359,9 @@ export class ObjectSerializer {
33523359
if ("additionalProperties" in attributesMap) {
33533360
const additionalProperties: { [key: string]: any } = {};
33543361
if (keepAllInAdditional) {
3355-
// Build reverse map from JSON baseName → attribute config for per-field deserialization.
3356-
// This preserves int64 precision for typed numeric fields via their declared format.
3357-
const baseNameToAttr: { [key: string]: any } = {};
3358-
for (const attrName in attributesMap) {
3359-
if (attrName !== "additionalProperties") {
3360-
baseNameToAttr[attributesMap[attrName].baseName] = attributesMap[attrName];
3361-
}
3362-
}
33633362
for (const key of extraAttributes) {
33643363
const attrInfo = baseNameToAttr[key];
3364+
// Use per-field type/format for typed attrs to preserve int64 precision.
33653365
additionalProperties[key] = attrInfo
33663366
? ObjectSerializer.deserialize(data[key], attrInfo.type, attrInfo.format)
33673367
: data[key];

packages/datadog-api-client-v2/models/ObjectSerializer.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14355,11 +14355,18 @@ export class ObjectSerializer {
1435514355

1435614356
const instance = new typeMap[type]();
1435714357
const attributesMap = typeMap[type].getAttributeTypeMap();
14358-
const attributesBaseNames = Object.keys(attributesMap).reduce(
14359-
(o, key) => Object.assign(o, { [attributesMap[key].baseName]: "" }),
14360-
{}
14361-
);
1436214358
const keepAllInAdditional = (typeMap[type] as any)._keepTypedInAdditionalProperties === true;
14359+
// Single pass: build attributesBaseNames (for extra-key detection) and, when needed,
14360+
// baseNameToAttr (for per-field typed deserialization preserving int64 precision).
14361+
const attributesBaseNames: { [key: string]: string } = {};
14362+
const baseNameToAttr: { [key: string]: any } = keepAllInAdditional ? {} : null;
14363+
for (const attrName in attributesMap) {
14364+
const baseName = attributesMap[attrName].baseName;
14365+
attributesBaseNames[baseName] = "";
14366+
if (keepAllInAdditional && attrName !== "additionalProperties") {
14367+
baseNameToAttr[baseName] = attributesMap[attrName];
14368+
}
14369+
}
1436314370
const extraAttributes = Object.keys(data).filter(
1436414371
(key) => keepAllInAdditional || !Object.prototype.hasOwnProperty.call(attributesBaseNames, key)
1436514372
);
@@ -14368,16 +14375,9 @@ export class ObjectSerializer {
1436814375
if ("additionalProperties" in attributesMap) {
1436914376
const additionalProperties: { [key: string]: any } = {};
1437014377
if (keepAllInAdditional) {
14371-
// Build reverse map from JSON baseName → attribute config for per-field deserialization.
14372-
// This preserves int64 precision for typed numeric fields via their declared format.
14373-
const baseNameToAttr: { [key: string]: any } = {};
14374-
for (const attrName in attributesMap) {
14375-
if (attrName !== "additionalProperties") {
14376-
baseNameToAttr[attributesMap[attrName].baseName] = attributesMap[attrName];
14377-
}
14378-
}
1437914378
for (const key of extraAttributes) {
1438014379
const attrInfo = baseNameToAttr[key];
14380+
// Use per-field type/format for typed attrs to preserve int64 precision.
1438114381
additionalProperties[key] = attrInfo
1438214382
? ObjectSerializer.deserialize(data[key], attrInfo.type, attrInfo.format)
1438314383
: data[key];

0 commit comments

Comments
 (0)