json-is-slow.md (1792B)
1 # JSON Is Slow 2 3 I've been going through "Computer, Enhance!", a worthwhile course for anyone who wants to build a better understanding of x86_64 assembly and performance broadly. Early on, it was shown how ILP can improve basic arithemetic performance 1000x in C relative to pure Python, which was rather obvious to me, having worked on similar problems in the past (TODO: LINK). 4 5 After creating an 8086 simulator, we started looking at Haversine distance calculation as an applied example for evaluating performance. Haversine distance doesn't really matter, but the process is interesting because it's relatively applied. 6 7 1. Load JSON file from disk 8 2. Parse JSON 9 3. Compute haversine distances for JSON embedded coordinates 10 11 Haversine distance calculation generally requires some trigonometric computations (sin, cos, atan2), along with a few other arithemetic operations. Things like atan2 are slow, but overall this can be completed quickly. Reading from disk can be slow, but it's inconsequential relative to parsing JSON. Even my limited JSON parser that was designed to only parse the coordinates was taking up ~80% of the computation time with small numbers of samples (~30,000 samples, json file not cached), degrading to ~85% at 300,000 samples, evaluated using the rdtsc intrinsic (a precise wall-clock timer on my system as rdtsc is shared and boost agnostic on my architecture). 12 13 You could say this is contrived, but I put almost no effort into optimizing any of this, and this is what most people would implement in C, using heap allocated arrays. Additionally, full fledged JSON parsers, like nlohmann's C++ parser, are actually slower than mine, primarily because they implement the full specification, instead of mine which is just a simple character-based state machine.