-
-
Notifications
You must be signed in to change notification settings - Fork 208
perf: monomorfic variables #801
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Signed-off-by: francesco <[email protected]>
Signed-off-by: francesco <[email protected]>
Signed-off-by: francesco <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
I have no opinion on this, but it feels strange, that primitives drop by nearly 50 %. |
Yep, this PR is not good. I want try to fix primitive type |
I’ve a bad news. TLTR: the benchmark is not reliable I was trying to understand why primitive types performed worse than objects in my PR, but I realized that on primitive types the code of the eg, see this benchmark case https://github.com/fastify/fast-json-stringify/blob/main/benchmark/bench.js#L44 This schema {
"type": "string"
} generate in both case (master and PR), this code: module.exports = function anonymous(validator, serializer
) {
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 = ''
if (typeof input !== 'string') {
if (input === null) {
json += JSON_STR_EMPTY_STRING
} else if (input instanceof Date) {
json += JSON_STR_QUOTE + input.toISOString() + JSON_STR_QUOTE
} else if (input instanceof RegExp) {
json += asString(input.source)
} else {
json += asString(input.toString())
}
} else {
json += asString(input)
}
return json
}
return main
}(validator, serializer) The same code had to give the same results... so I decided to benchmark master vs master to see if I got the same results ![]()
instead, even if I compared the same branch, I got the result that the second run was better than the first one! We need a more reliable benchmark to accept PR on performance like this one (see #803). |
It'a a good performance improvments on Array and Object, but not on primitive types
Raw benchmark