perf(serializer): remove all anonymous functions (huge perf improvements!) #812
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Inline generated serializers to remove anonymous functions
This PR removes the generation of per-schema anonymous serializer functions and fully inlines all serialization logic into a single main function.
Eg. this schema:
Actually generate this output code:
Show
const { asString, asNumber, asBoolean, asDateTime, asDate, asTime, asUnsafeString } = serializer
const asInteger = serializer.asInteger.bind(serializer)
let addComma = false
}
let addComma = false
With this PR the output become
Show
const { asString, asNumber, asBoolean, asDateTime, asDate, asTime, asUnsafeString } = serializer
const asInteger = serializer.asInteger.bind(serializer)
const JSON_STR_BEGIN_OBJECT = '{'
const JSON_STR_END_OBJECT = '}'
const JSON_STR_BEGIN_ARRAY = '['
const JSON_STR_END_ARRAY = ']'
const JSON_STR_COMMA = ','
const JSON_STR_COLONS = ':'
const JSON_STR_QUOTE = '"'
const JSON_STR_EMPTY_OBJECT = JSON_STR_BEGIN_OBJECT + JSON_STR_END_OBJECT
const JSON_STR_EMPTY_ARRAY = JSON_STR_BEGIN_ARRAY + JSON_STR_END_ARRAY
const JSON_STR_EMPTY_STRING = JSON_STR_QUOTE + JSON_STR_QUOTE
const JSON_STR_NULL = 'null'
function main(input) {
let json = ''
const obj_0 = (input && typeof input.toJSON === 'function')
? input.toJSON()
: input
if (obj_0 === null) {
json += JSON_STR_EMPTY_OBJECT
} else {
json += JSON_STR_BEGIN_OBJECT
let addComma_1 = false
}
return json
}
return main
This avoids:
Better JIT optimization opportunities
Inlining allows the JS engine to:
With separate functions, V8 often cannot inline aggressively due to:
Reduced memory pressure:
Benchmark