question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Ktor performance improvements

See original GitHub issue

Out 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:

  1. 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.)

  1. Most of lazy properties shouldn’t be thread-safe (SynchronizedLazyImpl -> UnsafeLazyImpl). It’s specific for TestApplicationRequest and TestApplicationResponse, but I’ve seen this in real request/response classes (e.g. in netty)

  2. Headers, probably, shouldn’t be lazy at all neither in request nor in response, because they are always required and read.

  3. 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

  1. ValuesMap#ensureListForKey (~15% of CPU) may be implemented via computeIfAbsent, though it’s should be measured first.

If you think these issues are valid I don’t mind to fix them

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:10 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
orangycommented, Dec 17, 2017

Thanks. Small note, benchmarks infra can do better:

    benchmark(args) {
        threads = 1
        iterations = 10000
        run<FullBenchmark>("sayOKLongParams")
    }

Instead of manually commenting annotations and modifying constants.

0reactions
qwwdfsadcommented, Dec 28, 2017

Thanks, I will close this issue. FYI this session revealed KT-22042

Read more comments on GitHub >

github_iconTop Results From Across the Web

What I Learnt from Benchmarking Http4k, Ktor (Kotlin) and ...
A couple of initial 1-connection warm-up steps show how JVM-based Kotlin frameworks gradually gain performance as they are being JIT-optimised ( ...
Read more >
Performance for my Ktor web service (Profiling?) : r/Kotlin
I've been working on a (now) Ktor web service for the past few years ... without using CPU, and what to work on...
Read more >
Engines | Ktor
The Ktor HTTP client can be used on different platforms, including JVM, Android, JavaScript, and Native. A specific platform may require a ...
Read more >
Performance Issue / Ktor & Netty #1163 - GitHub
Doesn't work, utilization of CPU is pretty low with it and I face above issue, only when I manually increase at least callGroupSize...
Read more >
Kotlin Asynchronous Framework, Ktor 2.0, Released with New ...
Other improvements include random port support, improved testing API, type-safe routing, XML Serialization and subroutes for plugins. On the ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found