Skip to content

Commit

Permalink
Back to keys approach, prevent creating more objects
Browse files Browse the repository at this point in the history
  • Loading branch information
uurien committed May 27, 2024
1 parent e56545f commit 3006ec0
Showing 1 changed file with 22 additions and 16 deletions.
38 changes: 22 additions & 16 deletions packages/dd-trace/src/encode/0.4.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,16 +272,19 @@ class AgentEncoder {
}

_encodeMetaStruct (bytes, value) {
const entries = Array.isArray(value) ? [] : Object.entries(value)
const validEntries = entries.filter(([_, v]) =>
typeof v === 'string' ||
typeof v === 'number' ||
(v !== null && typeof v === 'object'))
const keys = Array.isArray(value) ? [] : Object.keys(value)
const validKeys = keys.filter(key => {
const v = value[key]
return typeof v === 'string' ||
typeof v === 'number' ||
(v !== null && typeof v === 'object')
})

this._encodeMapPrefix(bytes, validEntries.length)
this._encodeMapPrefix(bytes, validKeys.length)

for (const [k, v] of validEntries) {
this._encodeString(bytes, k)
for (const key of validKeys) {
const v = value[key]
this._encodeString(bytes, key)
this._encodeObjectAsByteArray(bytes, v)
}
}
Expand Down Expand Up @@ -316,16 +319,19 @@ class AgentEncoder {
}

_encodeObjectAsMap (bytes, value, circularReferencesDetector) {
const entries = Object.entries(value)
const validEntries = entries.filter(([_, v]) =>
typeof v === 'string' ||
typeof v === 'number' ||
(v !== null && typeof v === 'object' && !circularReferencesDetector.has(v)))
const keys = Object.keys(value)
const validKeys = keys.filter(key => {
const v = value[key]
return typeof v === 'string' ||
typeof v === 'number' ||
(v !== null && typeof v === 'object' && !circularReferencesDetector.has(v))
})

this._encodeMapPrefix(bytes, validEntries.length)
this._encodeMapPrefix(bytes, validKeys.length)

for (const [k, v] of validEntries) {
this._encodeString(bytes, k)
for (const key of validKeys) {
const v = value[key]
this._encodeString(bytes, key)
this._encodeObject(bytes, v, circularReferencesDetector)
}
}
Expand Down

0 comments on commit 3006ec0

Please sign in to comment.