Live data from Hacker News

Parsing JSON in 500 lines of Rust

krish.gg

41–50 of 59 posts

Re: Parsing JSON in 500 lines of Rust

#42

I have to commend on the simplicity and clarity of thought in the write up. I could see what you were up to just by a skim through it (also thanks for the ready reference)! I'm pretty sure you'd already know of this, but once you've written your own version, it might help to compare and take notes from a popular, well-established, benchmarked library: https://github.com/serde-rs/json .

And on a related note, this project was for educational purposes, but if the author wants to do more parsing in Rust, there's the excellent `nom` crate [1], which provides a JSON parser as an example [2].

It uses a very similar paradigm to what the author used in the article, and provides a lot of helper utilities. I used it to parse a (very, very small) subset of Markdown recently [3] and enjoyed the experience.

[1] https://github.com/rust-bakery/nom

[2] https://github.com/rust-bakery/nom/blob/main/examples/json.r...

[3] https://git.sr.ht/~bsprague/logseq-to-linkwarden

Re: Parsing JSON in 500 lines of Rust

#43
post #11
post #4

Here is one in 73 lines of F# by Jon Harrop: https://gist.github.com/isaksky/6681cfad8ced1708a04b2eca92fc...

Meta: github is now requiring a login to see gists?

I got log in page on first click but it went away after closing it and opening again.

Re: Parsing JSON in 500 lines of Rust

#44
I have no rust knowledge let me ask

- is this hard to do in 500 lines of rust? - is there a catch why a implementation in rust worth mentioning on HN?

don't get me wrong im realy intested in rust.

Re: Parsing JSON in 500 lines of Rust

#46
post #27

Earlier quoted context omitted.

Another article on the front page is discussing the capabilities of AI for coding. I wonder, given a 500 line problem, can any of the current cutting edge AIs make the code obviously, dramatically better? How far can they go?

https://minimaxir.com/2025/01/write-better-code/

So, no?

Re: Parsing JSON in 500 lines of Rust

#47
post #2

If OP is running this on MacOS it could be due to macOS' code signing that runs the first time you run an executable.

Or more likely, OP has multiple versions of rust installed.

I wouldn't think the code signing phase is run while the benchmark is running and thus affecting those numbers.

Re: Parsing JSON in 500 lines of Rust

#48
post #44

I have no rust knowledge let me ask - is this hard to do in 500 lines of rust? - is there a catch why a implementation in rust worth mentioning on HN? don't get me wrong im realy intested in rust.

> - is this hard to do in 500 lines of rust?

No, not that much. User posted mostly because he/she is learning the language.

> is there a catch why a implementation in rust worth mentioning on HN?

HN loves Rust and it is an fun opportunity to explore Rust semantics in use without going to the more complex part of the language. It belongs to the category of "cool first project", like a Sudoku solver, etc.

Re: Parsing JSON in 500 lines of Rust

#49
post #25

I guess it's faster with sudo because the regular account has some resource limits, or something? Didn't mention which system is being used (Linux? macOS? Something else?) but I can't repro on my Linux system, where performance is identical: % cargo run --release [..] Parsing speed: 103761177.00 Bytes/s Parsing speed: 103.76 MB/s Parsing speed: 0.10 GB/s % doas cargo run --release [..] Parsing speed: 105401032.21 Byt…

Wild guess, but different versions of Rust for the users?

Too fast of a compile time, different versions would recompile the whole dep tree. And even just the end code would take more than .02s

Re: Parsing JSON in 500 lines of Rust

#50
post #44

I have no rust knowledge let me ask - is this hard to do in 500 lines of rust? - is there a catch why a implementation in rust worth mentioning on HN? don't get me wrong im realy intested in rust.

500 lines struck me as particularly long, but I would use a combinator library like nom, and it looks like they are trying to do it from scratch.
Post reply on HN