Native performance extremely slow Hi everyone, I h...
# kotlin-native
s
Native performance extremely slow Hi everyone, I have created the fleeksoft/Ksoup library, a port of Jsoup. It works very well, but I noticed that the performance on native platforms is significantly slower. On the JVM, it parses a 450KB file in 7ms, but on macOS and iOS native platforms, the same file takes 220ms, which is a huge difference. No platform-specific code is being used, and I tested with Ksoup-lite, which only has a dependency on Stately. However, the library code itself is written entirely in pure Kotlin. Are there any recommendations for optimizing and improving performance on native platforms?
m
How you've measured these results?
It's expected to have better results in Kotlin/JVM but the difference is huge. JVM is highly optimized for object creation. I guess that the performance issues are coming from the number of instances that you are creating since every node in the HTML tree is an object. It has nothing to do with external dependencies.
e
It's expected to have better results in Kotlin/JVM but the difference is huge.
It's somewhat expected. See https://github.com/Strumenta/antlr-kotlin/issues/165#issuecomment-1914879063
Perf will hopefully get better once LLVM 16 is optimized. You can already see a difference between 1.9.24 and 2.0.20, where 2.0.20 has marginally better perf on LLVM 11.
m
I already have a similar library, and JVM is only 2 times faster than macOS for large files. https://github.com/MohamedRejeb/Ksoup
e
macOS is generally better optimized because of mobile.
Worst is currently mingw, which doesn't seem to get a lot of attention.
s
@mohamed rejebI just checked the parsing with your library and noticed the same issue. The parsing time with your library is 4ms on the JVM and 212ms in native. I’m going to open an issue for it.
🤔 1
m
Are you testing with macOs arm or x64?
s
ARM macbook pro M2 MAX
m
Hmmm weird, can you share the file that you are testing with.
s
@mohamed rejeb select test empty for your Ksoup as I can’t figure out how to do that with it. It’s possible that your library only allows traversal. Also, I think I may have mixed up some code while making changes, but you can get the idea of how I’m testing.
m
This is the result I'm getting the way you are testing Jvm: 15.47ms macOS: 162.74ms I'm also testing with kotlinx-benchmark, which should be more accurate, but the results are totally different: Jvm: 7.88ms macOS: 15.27ms
s
@mohamed rejeb then it may be issue with measure function i will check it with kotlinx benchmark
o
Just in case: on K/N tests are run by default in DEBUG mode, while kotlinx.benchmark runs in RELEASE mode The difference between those modes could be huge
s
@mohamed rejeb You were right, it was an issue with the measure function. However, after checking with the kotlinx benchmark, KSoup took 4 ms on JVM and 7 ms on Native. Thanks for your time.
a
Try profiling the code to see exactly which function is slowing down the process
e
Is there a proper way to profile K/N binaries?