{"id":18282,"date":"2017-06-30T19:54:22","date_gmt":"2017-06-30T23:54:22","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=18282"},"modified":"2017-06-30T19:54:22","modified_gmt":"2017-06-30T23:54:22","slug":"writing-a-really-really-fast-json-parser","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2017\/06\/30\/writing-a-really-really-fast-json-parser\/","title":{"rendered":"Writing a Really, Really Fast JSON Parser"},"content":{"rendered":"<p><a href=\"https:\/\/chadaustin.me\/2017\/05\/writing-a-really-really-fast-json-parser\/\">Chad Austin<\/a> (via <a href=\"https:\/\/news.ycombinator.com\/item?id=14421215\">Hacker News<\/a>):<\/p>\n<blockquote cite=\"https:\/\/chadaustin.me\/2017\/05\/writing-a-really-really-fast-json-parser\/\">\n<p>Here are the optimizations that mattered:<\/p>\n<ul>\n<li>Moved the input and output pointers into locals instead of members, which helps VC++ and Clang understand that they can be placed in registers.  (gcc was already performing that optimization.) <a href=\"https:\/\/github.com\/chadaustin\/sajson\/commit\/71078d31e2248beb03ddd82de6826d5b2b8cd671\">71078d3<\/a> <a href=\"https:\/\/github.com\/chadaustin\/sajson\/commit\/4a07c770c2a1a7900879eec8511e0f429fe41dfa\">4a07c77<\/a><\/li>\n<li>Replaced the entire parse loop with a goto state machine.  Surprisingly, not only was this a performance win, but <a href=\"https:\/\/github.com\/chadaustin\/sajson\/blob\/b6a332f565d1717c4d27b8b85eaa3de94ac36764\/include\/sajson.h#L1037\">it actually made the code clearer<\/a>.   <a href=\"https:\/\/github.com\/chadaustin\/sajson\/commit\/3828769413bd83ccab42a686b1493b700d0a8f2e\">3828769<\/a> <a href=\"https:\/\/github.com\/chadaustin\/sajson\/commit\/05b3ec8840961076c89d43465eac6099e7547b64\">05b3ec8<\/a> <a href=\"https:\/\/github.com\/chadaustin\/sajson\/commit\/c02cb31f6492336501d613b142bf927475172d69\">c02cb31<\/a><\/li>\n<li>Change an 8-entry enum to a <code>uint8_t<\/code> instead of the pointer-sized value it was defaulting to.  <a href=\"https:\/\/github.com\/chadaustin\/sajson\/commit\/799c55fcd3b582b31df44caa9ccc249bc5393210\">799c55f<\/a><\/li>\n<li>Duplicated a bit of code to avoid needing to call a function pointer.  <a href=\"https:\/\/github.com\/chadaustin\/sajson\/commit\/44ec0dff687f502759cf8272373ce8aacc2b7921\">44ec0df<\/a><\/li>\n<li>Tiny microoptimizations like avoiding branches and unnecessary data flow.  <a href=\"https:\/\/github.com\/chadaustin\/sajson\/commit\/235d3309f94543523f52ebf477f91dc5f8c59473\">235d330<\/a> <a href=\"https:\/\/github.com\/chadaustin\/sajson\/commit\/002ba3108380b6764a29fd8279468f846f0cbc4e\">002ba31<\/a> <a href=\"https:\/\/github.com\/chadaustin\/sajson\/commit\/193b1839c6a0b99924ee7d25ca583e943b72708c\">193b183<\/a> <a href=\"https:\/\/github.com\/chadaustin\/sajson\/commit\/c23fa23927f903a382bd9dc4cbdf9a89c1257a94\">c23fa23<\/a><\/li>\n<li>Store the tag bits at the bottom of the element index instead of the top, which avoids a shift on 64-bit.  <a href=\"https:\/\/github.com\/chadaustin\/sajson\/commit\/e7f2351f197ffc418d1290c476661fb4f4aed01d\">e7f2351<\/a><\/li>\n<\/ul>\n<p>[&#8230;]<\/p>\n<p>An iOS team at Dropbox replaced JSONSerialization with sajson and cut their initial load times by two thirds!<\/p>\n<\/blockquote>\n\n<p>Previously: <a href=\"https:\/\/mjtsai.com\/blog\/2016\/10\/27\/parsing-json-is-a-minefield\/\">Parsing JSON Is a Minefield<\/a>.<\/p>","protected":false},"excerpt":{"rendered":"<p>Chad Austin (via Hacker News): Here are the optimizations that mattered: Moved the input and output pointers into locals instead of members, which helps VC++ and Clang understand that they can be placed in registers. (gcc was already performing that optimization.) 71078d3 4a07c77 Replaced the entire parse loop with a goto state machine. Surprisingly, not [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"","apple_news_api_id":"","apple_news_api_modified_at":"","apple_news_api_revision":"","apple_news_api_share_url":"","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[326,31,1380,507,138,270,71],"class_list":["post-18282","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-c-plus-plus","tag-ios","tag-ios-10","tag-json","tag-optimization","tag-parser","tag-programming"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/18282","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=18282"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/18282\/revisions"}],"predecessor-version":[{"id":18283,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/18282\/revisions\/18283"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=18282"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=18282"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=18282"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}