r/androiddev
•
u/kral_katili
•
3d ago
•
1
Yalın — a Rust-based JSON parser optimized for Android ARM devices
I've been working on Yalın, a JSON parser written in Rust for Android.
It uses ARM NEON SIMD and tries to keep JNI calls to a minimum. The idea was to see how much performance I could get out of native parsing compared to existing Java libraries.
In benchmarks against fastjson2, selective field reads were up to 4.6x faster, while parsing a full 1 MB JSON tree was around 1.3x faster.
Small payloads are a different story. JNI overhead is still a bottleneck, and there's room for improvement.
Curious what other Android developers think about using Rust for this kind of thing, especially whether the performance gains are worth the native dependency.
18
u/shurco 3d ago
a 1.3x gain on a full 1 MB tree isn't worth a native library in the apk. only the 4.6x selective reads pay for the rust dependency.
10
u/tadfisher 3d ago
In general we are amortizing JSON deserialization over network calls, so benchmarks that show improvement for 1MB payloads are kind of pointless. If you have a production app it would be useful to compare with kotlinx-serialization for real-world calls, and maybe even provide this as an alternative backend.
IMO, developer ergonomics is going to win over performance any day, and kotlinx-serialization kind of already won.
8
u/Ayano-Keiko 3d ago
Rust🦀🦀Saved. When I have android project, I will try it.
3
u/NewSubWhoDis 3d ago
This is great! I'd run it though some AI vulnerability scanners to make sure there isn't any nasty overflows.
I think the benefit of the other parsers like Moshi is that is breaks everything into heap objects that we can pass around to different views and parts of the stack. So while its slow, its a run once cry once kinda of parser that makes everything else much easier.
3
u/ComfortablyBalanced You will pry XML Views from my cold dead hands 3d ago
What is the size of the library? Rust binaries are notorious for their sizes.
3
u/shlusiak 3d ago
I have a few rules, and being able to at least type the name of the library to use on my keyboard is a pretty low bar to miss. Sorry.
3
u/Yikings-654points 3d ago
-Yalin
-I'm in
1
u/davidbalbino 2d ago
nice work. for the small payload case imo the trick is to not cross JNI per call at all. pass the raw bytes as a direct ByteBuffer (or byte[] with GetPrimitiveArrayCritical) instead of a jstring, because the String -> modified UTF-8 conversion alone kinda eats the gain on tiny jsons. and if the app parses lots of small responses, batching them in one native call helps more than any SIMD tbh. I think is also worth having a size threshold and just fall back to fastjson2 below some KB, depends of where your crossover is in the benchmark
1
u/Genuine_Giraffe 1d ago
Write my life in Rust , make no mistakes . maybe it will be better with no errors since everyone vibe codes it lol