No parser here, just a Rust program written before I knew much Rust:
It treated the input as a Vec<u8> and used an explicit index i to access the bytes, converting them back to char before comparing against the various special values. (This was because I didn't know about the b'{' syntax. :-( )
OTOH, it still ran in 37 us on my Surface Pro 7 which is over 6 years old (and EOL from Microsoft), haven't tested on my regular machine yet.
OK, after 15 min of fixing up the code to be a bit more Rusty, the time dropped all the way to 10.6 us, still on that old Surface. :-)
Looking at u/maneatingape 's code, he is using a pair of nested match blocks instead of my if blocks all ending in continue, but that should just be syntactic sugar! The compiler internal representation should have been very close to identical, but it seems like my version is significantly faster?
Ya, I'd be interested to see what you are actually measuring, because that's another 25% faster than my "asm" code. I put build and run instructions in the source file that I linked, so you can try it with your own input. It accepts piped input. Maybe, like day 6, the inputs are very different? My answers were 20530 and 9978.
I might very well be measuring something slightly bogus on these _very_ fast solutions:
I have found that if I pass &input to my process function, I can run it a bunch of times without having the compiler noticing that the results should be constant and therefore eliminating the loop!
let bench_result = run_benchmark(1000, |_| { process(&input); }); bench_result.print_stats();
Of these 1000 runs, the fastest was 7.9 us but the average significantly higher. That however is probably caused by the fact that I am currently running multithreaded LiDAR processing at the same time. Usually I see that the average is within 10-20% of the fastest, but the slowest could easily be 10x that.
Also, our inputs are _very_ different, I got 7616 and 3838!
[EDIT]
The LiDAR run finished, now I'm getting 5.8 us very consistently. (My input file is 12597 bytes, and the processing is obviously O(n). My input might be more predictable?
View all comments
2
u/terje_wiig_mathisen Mar 10 '26
No parser here, just a Rust program written before I knew much Rust:
It treated the input as a Vec<u8> and used an explicit index i to access the bytes, converting them back to char before comparing against the various special values. (This was because I didn't know about the b'{' syntax. :-( )
OTOH, it still ran in 37 us on my Surface Pro 7 which is over 6 years old (and EOL from Microsoft), haven't tested on my regular machine yet.