-
Notifications
You must be signed in to change notification settings - Fork 568
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
chore(performance#38): Performance improvements #1871
Conversation
Codecov ReportBase: 90.35% // Head: 90.33% // Decreases project coverage by
Additional details and impacted files@@ Coverage Diff @@
## main #1871 +/- ##
==========================================
- Coverage 90.35% 90.33% -0.02%
==========================================
Files 70 70
Lines 6042 6044 +2
==========================================
+ Hits 5459 5460 +1
- Misses 583 584 +1
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. ☔ View full report at Codecov. |
get essence () { | ||
return `${this.type}/${this.subtype}` | ||
} | ||
essence: `${type}/${subtype}` |
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.
This was the main driver for the improvements. Tried to find something in the spec that indicates this is a getter
but sadly couldn't find any point indicating it. I might overlook it, but @KhafraDev can you confirm this approach is ok?
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.
This won't work as it can be overwritten... it must be a getter
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.
That was what I was missing then. Does that mean that the mime type object can be altered after creation, right?
This comment was marked as outdated.
This comment was marked as outdated.
Sorry, something went wrong.
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.
Actually there is more stuff here than should get getters?
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.
I might be wrong, but then it becomes a case of consistency, if you change subtype then essence need to be automatically updated, which is why a getter is necessary either way.
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.
Actually there is more stuff here than should get getters?
You mean the PR itself, then yes? Otherwise, I might be missing something
I might be wrong, but then it becomes a case of consistency, if you change subtype then essence need to be automatically updated, which is why a getter is necessary either way
Yeah, once you said that about that must be a getter, that is what I started to see, as most likely the issue is not on the getter itself but the recomputation of the string
on every get
call.
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.
It seems is actually more about the string computation, as if I replace it with the following class that computes the essence
on instantiation, and on set/get (kinda caching), the results remains stable.
Class:
class MimeType {
#type = ''
#subtype = ''
#essence = ''
#parameters = new Map()
constructor ({ type, subtype }) {
this.#type = type
this.#subtype = subtype
this.#essence = `${type}/${subtype}`
}
set type (value) {
this.#type = value
this.#essence = `${type}/${subtype}`
}
get type () {
this.#type
}
set subtype (value) {
this.#subtype = value
this.#essence = `${type}/${subtype}`
}
get subtype () {
return this.#subtype
}
get essence () {
return this.#essence
}
get parameters () {
return this.#parameters
}
}
Scenario
const str = 'application/json; charset=utf-8'
suite
.add('util#MIMEType', function () {
new util.MIMEType(str)
})
.add('undici#parseMIMEType', function () {
parseMIMEType(str).essence
})
.add('undici#parseMIMEType(original)', function () {
parseMIMETypeOriginal(str).essence
})
.add('fast-content-type-parse#parse', function () {
fastContentType.parse(str)
})
.add('fast-content-type-parse#safeParse', function () {
fastContentType.safeParse(str)
})
.on('cycle', function (event) {
console.log(String(event.target))
})
.on('complete', function () {
console.log('Fastest is ' + this.filter('fastest').map('name'))
})
.run({ async: true })
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.
Results:
util#MIMEType x 1,309,932 ops/sec ±0.48% (94 runs sampled)
undici#parseMIMEType x 2,331,911 ops/sec ±0.32% (96 runs sampled)
undici#parseMIMEType(original) x 1,501,260 ops/sec ±0.50% (99 runs sampled)
I read the original function and these lines get my attention: Lines 325 to 331 in d7cd095
Also in this part: Lines 360 to 365 in d7cd095
I think that worth to put the validation of |
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
While working on Performance#38, I decided to use it as starting point Undici for experimenting with possible performance improvements, as Undici has a greatly clean implementation of the WHATWG for parsing a MIMEType.
While experimenting I was able to notice substantial performance improvements while changing smaller pieces, while trying to respect them as much as possible the spec.
Was able to obtain a substantial ~48% improvement compared to the current implementation.
The PR as always is just a suggestion as pointed out in the performance thread, in Undici this is not one of the biggest worries as it is used only while parsing
data:
strings 🙂Feel free to close it if there's no value added.
Benchmarks:
Machine
Results
Code