Just introducing ktrs, a bit exact rust port of kt...
# feed
h
Just introducing ktrs, a bit exact rust port of ktlint and ktfmt! github.com/Hexay/ktrs
๐Ÿ”ฅ 2
m
Nice! But did you try a GraalVM native image of the Java version first? As kotlinc can now also start and run faster with this tech, maybe it can work for ktlink and ktfmt too
a
I am not sure that replacing 10 k lines in kotlin with 300 k lines of code in Rust (including unsafe memory access) is a good thing. Also it is not clear how you measured the performance. Did you include VM warmup? If you a concerned with warmup time, maybe it is better to use Kotlin/Native instead? It would better contribute to the ecosystem.
i
@altavir I don't think it's fare to exclude warmup for cli tools that often used on CI and even local I'll prefer not having any extra daemons
๐ŸŽ‰ 1
a
We had this discussion ๐Ÿ™‚ I can see the reason, but then why not GraalVM-native or Kotlin/Native? If the difference in performance is so large, than it could make sense to use Rust. But I am skeptical. JVM could have 1-2 seconds warm up disadvantage, but not 12 seconds.
i
youtrack.jetbrains.com/issue/KT-82373/Meta-Distribute-a-GraalVM-compiled-Native-Image-of-kotlinc-or-the-Kotlin-daemon Cause waiting for all JIT optimizations to kick in is really long and painful. That's why I believe that JVM shouldn't be used generally for CLI and Desktop tools
It's interesting experiment overall, could you also compare: - with native image ktfmt - with ktlint 2 - include binary sizes in comparison (i expect rust to be much better here compared to native image) - i wonder if rust version passes all tests from ktfmt and ktlint
๐Ÿ‘ 1
m
I asked an agent to do a port to native image. It says (unverified): ktfmt: for a small input, 67x faster (704msec to 11msec). For a 50 file check, ~2 seconds to 256msec, 7.5x faster ktlint: for a small input 50x faster, for a 50 file input 4.6x faster. That's without PGO. I'm asking it to try with PGO now.
For ktfmt, peak RSS drops from 884 MB to 91 MB. However, for a large job, it does take 35% longer due to lack of profiling/JIT.
(for ktfmt there was no porting work to do because Sam already did it)
PGO nearly halves binary size and improves performance by 15%-30% and the dense file slowdown is gone, so the native image is faster, smaller and starts instantly. It doesn't get to 192x faster, that is a truly crazy speedup, but on the other hand, it doesn't require maintaining a large second codebase. I guess people can use what they want! Lots of options! ๐Ÿ™‚
๐Ÿ‘ 3
๐Ÿ’ฏ 1
h
@altavir I'm not remotely sure where 300k lines comes from? The project is ~86k lines of non test code compared to 13k and 32k for ktfmt/ktlint (non test code) that implement less. The 3 small areas of unsafe rust are perfectly fine and are done for performance. I've made some of the numbers clearer ๐Ÿ‘
@irus I've added some more comparisons. Both test suites fully pass, I actually need to make a few upstream prs for bugs found from fuzzing!
๐Ÿ‘ 1
i
Yep, LGTM, thanks
e
Are there plans to propose integrating this into the Kotlin Toolchain as a
kotlin format
command? It would be great to avoid having two parallel efforts developing similar tools? https://kotlinlang.slack.com/archives/C062WG3A7T8/p1791658913235909?thread_ts=1791653984.272239&cid=C062WG3A7T8
i
Ported rust version to common Kotlin. Review sees few inficiencies related to UTF-8 <-> UTF-16 conversions and more objects allocated, I'm interesting in pushing K/N even harder, so I'll continue this investigation
Tuning K/N GC (by default it's using 10Mb heap target and 0.9 collection trigger coefficient, which likely great for phones, but not so much for our use-case) I've got around 2x improvement by using more memory)