Ktor performance improvements
See original GitHub issueOut of curiosity I’ve made small profiling session of ktor via FullBenchmark.kt
, but added query string and its parsing (to emulate more realistic workload).
CPU profile: http://svgshare.com/i/4XN.svg Allocation profile: http://svgshare.com/i/4X_.svg (note that this one may be inaccurate due to profile method skew)
Observations:
- Query string parsing is dominating CPU consumer. E.g. for comparison two OK methods, one without query string and one with medium-size one (~80 characters):
Benchmark Mode Cnt Score Error Units
FullBenchmark.sayOK avgt 10 3.507 ± 0.062 us/op
FullBenchmark.sayOKLongParams avgt 10 5.783 ± 1.286 us/op
It can be simply optimized (pre-size ValuesMap
, don’t use lazy filtering sequences for splitting, etc.)
-
Most of lazy properties shouldn’t be thread-safe (
SynchronizedLazyImpl
->UnsafeLazyImpl
). It’s specific forTestApplicationRequest
andTestApplicationResponse
, but I’ve seen this in real request/response classes (e.g. in netty) -
Headers, probably, shouldn’t be lazy at all neither in request nor in response, because they are always required and read.
-
Pipeline#merge
is implemented in non-trivial (for me) way:
val interceptors = ArrayList<PipelineInterceptor<TSubject, TContext>>(fromContent.interceptors.size)
interceptors.fastAddAll(fromContent.interceptors)
where fastAddAll
ensures capacity and adds element one by one, where each add
ensures capacity on its own.
Why is it better than
val interceptors = ArrayList<PipelineInterceptor<TSubject, TContext>>(fromContent.interceptors)
? It’s rather about code style, than performance though
ValuesMap#ensureListForKey
(~15% of CPU) may be implemented viacomputeIfAbsent
, though it’s should be measured first.
If you think these issues are valid I don’t mind to fix them
Issue Analytics
- State:
- Created 6 years ago
- Comments:10 (10 by maintainers)
Top GitHub Comments
Thanks. Small note, benchmarks infra can do better:
Instead of manually commenting annotations and modifying constants.
Thanks, I will close this issue. FYI this session revealed KT-22042