r/androiddev • • 3d ago • Bravo 1

Yalın — a Rust-based JSON parser optimized for Android ARM devices

Post image

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.

https://github.com/0xberkay/yalin

44 Upvotes

21 comments sorted by

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.

2

u/kral_katili 3d ago

Thanks for the feedback! Fair point, selective reads are where it really makes a difference. I'm still experimenting to see how far I can push the performance.

2

u/wasimaster 2d ago

This reply is also just AI Slop

-1

u/kral_katili 2d ago

Lib is about 580 kb and it could be worth for apps with huge code bases and if JSON is big performance difference is big to

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.

2

u/kral_katili 3d ago

Thanks for the feedback! There's already a demo app in the repo that makes network calls, but I agree that testing in larger, real-world apps would be more useful. I'm planning to work on that next.

8

u/Ayano-Keiko 3d ago

Rust🦀🦀Saved. When I have android project, I will try it.

2

u/kral_katili 3d ago

Thanks! 🦀

1

u/Elgas_Clondo 3d ago

yeah definitely bookmarking this one for later too

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.

1

u/kral_katili 3d ago

Thanks for the feedback! That's a good point about Moshi. I'll look into running some security checks and fuzz testing as well. Yalın is still experimental, so there's definitely more work to do on both safety and usability.

1

u/NewSubWhoDis 2d ago

I think its worth the effort fwiw. It doesn't seem like the phones are getting that much faster day to day so having a library that does really fast processing is effective. Also with AI it might be easier to wire up JNI and the speed might be worth the unreadable code (cus I'm not reading that code lol)

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.

2

u/kral_katili 3d ago

Good point, thanks! I checked the release build: the arm64 native library is about 568 KiB, including a 387 KiB .text section.

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.

1

u/kral_katili 3d ago

Oh okay I can give you whenever you want you can copy :D ıııııİİİ btw it's yalin in library and api so you don't need to worry about

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