deploy: 098eebd412
This commit is contained in:
1 parent
70feeb496c
commit
8279f43a4b
471 files changed
+1737
-1335
No files matched your search
@@ -1,4 +1,4 @@
|
||||
<!doctype html><html lang=en class=no-js> <head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><meta name=description content="JSON for Modern C++ is a header-only C++11 library that makes JSON a first-class data type."><meta name=author content="Niels Lohmann"><link href="https://json.nlohmann.me/home/blog/how_3_13_0_got_faster/" rel="canonical"><link href=../../releases/v1.0.0/ rel=prev><link href=../../design_goals/ rel=next><link rel=icon href=../../../assets/images/favicon.png><meta name=generator content="mkdocs-1.6.1, mkdocs-material-9.8.0"><title>How 3.13.0 got faster - JSON for Modern C++</title><link rel=stylesheet href=../../../assets/stylesheets/main.57a0d79d.min.css><link rel=stylesheet href=../../../assets/stylesheets/palette.fb2fffe2.min.css><link rel=stylesheet href=../../../css/custom.css><link rel=stylesheet href=../../../css/brand.css><link rel=stylesheet href=../../../css/fig-sax.css><link rel=stylesheet href=../../../css/fig-jsonview.css><link rel=stylesheet href=../../../css/fig-exceptions.css><link rel=stylesheet href=../../../css/fig-pointer.css><link rel=stylesheet href=../../../css/fig-charts.css><link rel=stylesheet href=../../../css/fig-integration.css><link rel=stylesheet href=../../../css/fig-macros.css><script>__md_scope=new URL("../../..",location),__md_hash=e=>[...e].reduce(((e,_)=>(e<<5)-e+_.charCodeAt(0)),0),__md_get=(e,_=localStorage,t=__md_scope)=>JSON.parse(_.getItem(t.pathname+"."+e)),__md_set=(e,_,t=localStorage,a=__md_scope)=>{try{t.setItem(a.pathname+"."+e,JSON.stringify(_))}catch(e){}}</script></head> <body dir=ltr data-md-color-scheme=default data-md-color-primary=custom data-md-color-accent=custom> <input class=md-toggle data-md-toggle=drawer type=checkbox id=__drawer autocomplete=off> <input class=md-toggle data-md-toggle=search type=checkbox id=__search autocomplete=off> <label class=md-overlay for=__drawer></label> <div data-md-component=skip> <a href=#how-nlohmannjson-3130-got-3-faster class=md-skip> Skip to content </a> </div> <div data-md-component=announce> </div> <header class=md-header data-md-component=header> <nav class="md-header__inner md-grid" aria-label=Header> <a href=../../.. title="JSON for Modern C++" class="md-header__button md-logo" aria-label="JSON for Modern C++" data-md-component=logo> <img src=../../../images/wordmark-header.svg alt=logo> </a> <label class="md-header__button md-icon" for=__drawer> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M3 6h18v2H3zm0 5h18v2H3zm0 5h18v2H3z"/></svg> </label> <div class=md-header__title data-md-component=header-title> <div class=md-header__ellipsis> <div class=md-header__topic> <span class=md-ellipsis> JSON for Modern C++ </span> </div> <div class=md-header__topic data-md-component=header-topic> <span class=md-ellipsis> How 3.13.0 got faster </span> </div> </div> </div> <form class=md-header__option data-md-component=palette> <input class=md-option data-md-color-media="(prefers-color-scheme: light)" data-md-color-scheme=default data-md-color-primary=custom data-md-color-accent=custom aria-label="Switch to dark mode" type=radio name=__palette id=__palette_0> <label class="md-header__button md-icon" title="Switch to dark mode" for=__palette_1 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a4 4 0 0 0-4 4 4 4 0 0 0 4 4 4 4 0 0 0 4-4 4 4 0 0 0-4-4m0 10a6 6 0 0 1-6-6 6 6 0 0 1 6-6 6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> <input class=md-option data-md-color-media="(prefers-color-scheme: dark)" data-md-color-scheme=slate data-md-color-primary=custom data-md-color-accent=custom aria-label="Switch to light mode" type=radio name=__palette id=__palette_1> <label class="md-header__button md-icon" title="Switch to light mode" for=__palette_0 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 18c-.89 0-1.74-.2-2.5-.55C11.56 16.5 13 14.42 13 12s-1.44-4.5-3.5-5.45C10.26 6.2 11.11 6 12 6a6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> </form> <script>var palette=__md_get("__palette");if(palette&&palette.color){if("(prefers-color-scheme)"===palette.color.media){var media=matchMedia("(prefers-color-scheme: light)"),input=document.querySelector(media.matches?"[data-md-color-media='(prefers-color-scheme: light)']":"[data-md-color-media='(prefers-color-scheme: dark)']");palette.color.media=input.getAttribute("data-md-color-media"),palette.color.scheme=input.getAttribute("data-md-color-scheme"),palette.color.primary=input.getAttribute("data-md-color-primary"),palette.color.accent=input.getAttribute("data-md-color-accent")}for(var[key,value]of Object.entries(palette.color))document.body.setAttribute("data-md-color-"+key,value)}</script> <label class="md-header__button md-icon" for=__search> <svg xmlns=http://www.w3Line truncated
|
||||
<!doctype html><html lang=en class=no-js> <head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><meta name=description content="JSON for Modern C++ is a header-only C++11 library that makes JSON a first-class data type."><meta name=author content="Niels Lohmann"><link href="https://json.nlohmann.me/home/blog/how_3_13_0_got_faster/" rel="canonical"><link href=../../releases/v1.0.0/ rel=prev><link href=../../design_goals/ rel=next><link rel=icon href=../../../assets/images/favicon.png><meta name=generator content="mkdocs-1.6.1, mkdocs-material-9.8.1"><title>How 3.13.0 got faster - JSON for Modern C++</title><link rel=stylesheet href=../../../assets/stylesheets/main.57a0d79d.min.css><link rel=stylesheet href=../../../assets/stylesheets/palette.fb2fffe2.min.css><link rel=stylesheet href=../../../css/custom.css><link rel=stylesheet href=../../../css/brand.css><link rel=stylesheet href=../../../css/fig-sax.css><link rel=stylesheet href=../../../css/fig-jsonview.css><link rel=stylesheet href=../../../css/fig-exceptions.css><link rel=stylesheet href=../../../css/fig-pointer.css><link rel=stylesheet href=../../../css/fig-charts.css><link rel=stylesheet href=../../../css/fig-integration.css><link rel=stylesheet href=../../../css/fig-macros.css><script>__md_scope=new URL("../../..",location),__md_hash=e=>[...e].reduce(((e,_)=>(e<<5)-e+_.charCodeAt(0)),0),__md_get=(e,_=localStorage,t=__md_scope)=>JSON.parse(_.getItem(t.pathname+"."+e)),__md_set=(e,_,t=localStorage,a=__md_scope)=>{try{t.setItem(a.pathname+"."+e,JSON.stringify(_))}catch(e){}}</script></head> <body dir=ltr data-md-color-scheme=default data-md-color-primary=custom data-md-color-accent=custom> <input class=md-toggle data-md-toggle=drawer type=checkbox id=__drawer autocomplete=off> <input class=md-toggle data-md-toggle=search type=checkbox id=__search autocomplete=off> <label class=md-overlay for=__drawer></label> <div data-md-component=skip> <a href=#how-nlohmannjson-3130-got-3-faster class=md-skip> Skip to content </a> </div> <div data-md-component=announce> </div> <header class=md-header data-md-component=header> <nav class="md-header__inner md-grid" aria-label=Header> <a href=../../.. title="JSON for Modern C++" class="md-header__button md-logo" aria-label="JSON for Modern C++" data-md-component=logo> <img src=../../../images/wordmark-header.svg alt=logo> </a> <label class="md-header__button md-icon" for=__drawer> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M3 6h18v2H3zm0 5h18v2H3zm0 5h18v2H3z"/></svg> </label> <div class=md-header__title data-md-component=header-title> <div class=md-header__ellipsis> <div class=md-header__topic> <span class=md-ellipsis> JSON for Modern C++ </span> </div> <div class=md-header__topic data-md-component=header-topic> <span class=md-ellipsis> How 3.13.0 got faster </span> </div> </div> </div> <form class=md-header__option data-md-component=palette> <input class=md-option data-md-color-media="(prefers-color-scheme: light)" data-md-color-scheme=default data-md-color-primary=custom data-md-color-accent=custom aria-label="Switch to dark mode" type=radio name=__palette id=__palette_0> <label class="md-header__button md-icon" title="Switch to dark mode" for=__palette_1 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a4 4 0 0 0-4 4 4 4 0 0 0 4 4 4 4 0 0 0 4-4 4 4 0 0 0-4-4m0 10a6 6 0 0 1-6-6 6 6 0 0 1 6-6 6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> <input class=md-option data-md-color-media="(prefers-color-scheme: dark)" data-md-color-scheme=slate data-md-color-primary=custom data-md-color-accent=custom aria-label="Switch to light mode" type=radio name=__palette id=__palette_1> <label class="md-header__button md-icon" title="Switch to light mode" for=__palette_0 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 18c-.89 0-1.74-.2-2.5-.55C11.56 16.5 13 14.42 13 12s-1.44-4.5-3.5-5.45C10.26 6.2 11.11 6 12 6a6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> </form> <script>var palette=__md_get("__palette");if(palette&&palette.color){if("(prefers-color-scheme)"===palette.color.media){var media=matchMedia("(prefers-color-scheme: light)"),input=document.querySelector(media.matches?"[data-md-color-media='(prefers-color-scheme: light)']":"[data-md-color-media='(prefers-color-scheme: dark)']");palette.color.media=input.getAttribute("data-md-color-media"),palette.color.scheme=input.getAttribute("data-md-color-scheme"),palette.color.primary=input.getAttribute("data-md-color-primary"),palette.color.accent=input.getAttribute("data-md-color-accent")}for(var[key,value]of Object.entries(palette.color))document.body.setAttribute("data-md-color-"+key,value)}</script> <label class="md-header__button md-icon" for=__search> <svg xmlns=http://www.w3Line truncated
|
||||
v3.13.0 tag (Appendix G describes the setup; tests/benchmarks/json_view/others/README.md has the commands of the
|
||||
comparison), including the line counts in "The price". The numbers below are from develop 7fc8312c64 with the
|
||||
open pull requests #6029, #6031, #6033 and #6039 merged (local branch local/all-prs-1107 at fcae242ac8, plus
|
||||
@@ -2364,4 +2364,4 @@ g++-16<span class=w> </span>-std<span class=o>=</span>c++20<span class=w> </span
|
||||
<span class=n>take</span><span class=p>(</span><span class=n>prev</span><span class=p>,</span><span class=w> </span><span class=n>cur</span><span class=p>);</span><span class=w> </span><span class=c1>// cur becomes the parent</span>
|
||||
<span class=n>take</span><span class=p>(</span><span class=n>cur</span><span class=p>,</span><span class=w> </span><span class=n>tmp</span><span class=p>);</span><span class=w> </span><span class=c1>// the child becomes cur</span>
|
||||
<span class=c1>// on the way up: read the parent back out of prev's last slot, pop that slot, free cur</span>
|
||||
</code></pre></div> <p class=nj-code-caption>One step down in the destructor's walk: the emptied slot remembers the way back.</p> <p><code>take()</code> moves the 16 bytes of type and value and leaves a null behind. The way back needs no memory: the slot that holds the link is the one being emptied, it stays the last child until the walk comes back up, and only then, after the link is taken out, is it removed. A child is removed only when it is a scalar or an empty container, so each removal destroys one level at most.</p> <h3 id=j-validation>J. Validation<a class=headerlink href=#j-validation title="Permanent link">¶</a></h3> <p>A faster path is only useful if it gives the same result as the slow one, including the same error at the same byte. These are the checks behind the themes:</p> <ul> <li><strong>Error positions</strong> (theme <a class=nj-theme href=#theme-1 title="theme 1">1</a>). The contiguous fast paths defer to the byte path on anything malformed. On 481,000 documents, 298,333 of them with errors, the contiguous and the byte path reported the same exception, message, byte, line and column, except for the one deliberate correction listed in <a href=#f-behavior-changes>Appendix F</a>.</li> <li><strong>Stream windows</strong> (theme <a class=nj-theme href=#theme-1 title="theme 1">1</a>). A test <code>streambuf</code> hands out get areas of 1–4,096 bytes, so that every token crosses a window edge at every offset, and the result is compared with a <code>streambuf</code> without a get area, which takes the old path. The <code>FILE</code> window is checked the same way, with <code>FILE</code> buffers of 1 to 70 bytes, unbuffered files, and characters pushed back with <code>ungetc()</code> before the parse, on macOS and glibc.</li> <li><strong>The fused parser</strong> (theme <a class=nj-theme href=#theme-1 title="theme 1">1</a>). <code>contiguous_parser</code> is compared with the byte path on corner cases of literals, numbers, escapes, surrogates and <abbr title="Unicode Transformation Format">UTF</abbr>-8, on 1,500 generated documents, every truncation of the short ones and random mutations, and on nesting across the 64-level words of its stack: the same values with the same number types and float bits, the same exception messages, the same <code>accept()</code> verdicts and the same <code>sax_parse()</code> events, also for handlers that stop early (242,118 assertions). A fuzzer that compares the <code>sax_parse()</code> events of both paths ran 9 million inputs without a finding.</li> <li><strong>Differential fuzzing of the input kinds.</strong> Every fuzz input is also parsed from an <code>std::istringstream</code>, from <code>std::list</code> iterators (the byte path) and from a <code>FILE*</code>, and the value, the exception id, the message, and the byte offset must be identical (<a href="https://github.com/nlohmann/json/pull/5827">#5827</a>).</li> <li><strong>Guard pages.</strong> The input is copied so that its last byte is the last readable byte of a page, and the next page is unmapped. AddressSanitizer misses an over-read into the slack of the same heap allocation, and fuzzers rarely end an input exactly at a page boundary; this test crashes on any over-read by one byte (<a href="https://github.com/nlohmann/json/pull/5826">#5826</a>).</li> <li><strong>Number conversion</strong> (theme <a class=nj-theme href=#theme-3 title="theme 3">3</a>). 6.5 million float tokens were compared with <code>strtod</code>, among them 60,000 exact halfway cases, and 900,000 hard cases from other test suites: all bit-identical. <code>json_document</code>'s batched float output is byte-identical to the general writer for 10⁹ generated tokens.</li> <li><strong>Unchanged output.</strong> For this post, we checked that 3.13.0 writes the same bytes as 3.12.0 in all five binary formats for 39 test files, and the same <abbr title="JavaScript Object Notation">JSON</abbr> text for <code>twitter.json</code> and <code>citm_catalog.json</code>. The changes of float output are listed in <a href=#f-behavior-changes>Appendix F</a>.</li> <li><strong><code>json_document</code> against <code>json</code>.</strong> A fuzzer parses every input with both and requires the same verdict, the same value and the same error message, from a borrowed string and from an exact-size buffer without a closing <abbr title="Null character (U+0000, byte value 0)">NUL</abbr> (<a href="https://github.com/nlohmann/json/blob/develop/tests/src/fuzzer-parse_json_view.cpp"><code>fuzzer-parse_json_view.cpp</code></a>).</li> <li><strong>Comparison</strong> (theme <a class=nj-theme href=#theme-7 title="theme 7">7</a>). The single-pass <code>operator<</code> was compared with the old code on 68,121 pairs of values.</li> <li><strong>Mutation checks.</strong> Break a fast path on purpose (skip one byte too many, mark <code>"</code> as copyable) and make sure the tests notice. Marking the quote as copyable in the short-string table fails 4,551 asseLine truncated
|
||||
</code></pre></div> <p class=nj-code-caption>One step down in the destructor's walk: the emptied slot remembers the way back.</p> <p><code>take()</code> moves the 16 bytes of type and value and leaves a null behind. The way back needs no memory: the slot that holds the link is the one being emptied, it stays the last child until the walk comes back up, and only then, after the link is taken out, is it removed. A child is removed only when it is a scalar or an empty container, so each removal destroys one level at most.</p> <h3 id=j-validation>J. Validation<a class=headerlink href=#j-validation title="Permanent link">¶</a></h3> <p>A faster path is only useful if it gives the same result as the slow one, including the same error at the same byte. These are the checks behind the themes:</p> <ul> <li><strong>Error positions</strong> (theme <a class=nj-theme href=#theme-1 title="theme 1">1</a>). The contiguous fast paths defer to the byte path on anything malformed. On 481,000 documents, 298,333 of them with errors, the contiguous and the byte path reported the same exception, message, byte, line and column, except for the one deliberate correction listed in <a href=#f-behavior-changes>Appendix F</a>.</li> <li><strong>Stream windows</strong> (theme <a class=nj-theme href=#theme-1 title="theme 1">1</a>). A test <code>streambuf</code> hands out get areas of 1–4,096 bytes, so that every token crosses a window edge at every offset, and the result is compared with a <code>streambuf</code> without a get area, which takes the old path. The <code>FILE</code> window is checked the same way, with <code>FILE</code> buffers of 1 to 70 bytes, unbuffered files, and characters pushed back with <code>ungetc()</code> before the parse, on macOS and glibc.</li> <li><strong>The fused parser</strong> (theme <a class=nj-theme href=#theme-1 title="theme 1">1</a>). <code>contiguous_parser</code> is compared with the byte path on corner cases of literals, numbers, escapes, surrogates and <abbr title="Unicode Transformation Format">UTF</abbr>-8, on 1,500 generated documents, every truncation of the short ones and random mutations, and on nesting across the 64-level words of its stack: the same values with the same number types and float bits, the same exception messages, the same <code>accept()</code> verdicts and the same <code>sax_parse()</code> events, also for handlers that stop early (242,118 assertions). A fuzzer that compares the <code>sax_parse()</code> events of both paths ran 9 million inputs without a finding.</li> <li><strong>Differential fuzzing of the input kinds.</strong> Every fuzz input is also parsed from an <code>std::istringstream</code>, from <code>std::list</code> iterators (the byte path) and from a <code>FILE*</code>, and the value, the exception id, the message, and the byte offset must be identical (<a href="https://github.com/nlohmann/json/pull/5827">#5827</a>).</li> <li><strong>Guard pages.</strong> The input is copied so that its last byte is the last readable byte of a page, and the next page is unmapped. AddressSanitizer misses an over-read into the slack of the same heap allocation, and fuzzers rarely end an input exactly at a page boundary; this test crashes on any over-read by one byte (<a href="https://github.com/nlohmann/json/pull/5826">#5826</a>).</li> <li><strong>Number conversion</strong> (theme <a class=nj-theme href=#theme-3 title="theme 3">3</a>). 6.5 million float tokens were compared with <code>strtod</code>, among them 60,000 exact halfway cases, and 900,000 hard cases from other test suites: all bit-identical. <code>json_document</code>'s batched float output is byte-identical to the general writer for 10⁹ generated tokens.</li> <li><strong>Unchanged output.</strong> For this post, we checked that 3.13.0 writes the same bytes as 3.12.0 in all five binary formats for 39 test files, and the same <abbr title="JavaScript Object Notation">JSON</abbr> text for <code>twitter.json</code> and <code>citm_catalog.json</code>. The changes of float output are listed in <a href=#f-behavior-changes>Appendix F</a>.</li> <li><strong><code>json_document</code> against <code>json</code>.</strong> A fuzzer parses every input with both and requires the same verdict, the same value and the same error message, from a borrowed string and from an exact-size buffer without a closing <abbr title="Null character (U+0000, byte value 0)">NUL</abbr> (<a href="https://github.com/nlohmann/json/blob/develop/tests/src/fuzzer-parse_json_view.cpp"><code>fuzzer-parse_json_view.cpp</code></a>).</li> <li><strong>Comparison</strong> (theme <a class=nj-theme href=#theme-7 title="theme 7">7</a>). The single-pass <code>operator<</code> was compared with the old code on 68,121 pairs of values.</li> <li><strong>Mutation checks.</strong> Break a fast path on purpose (skip one byte too many, mark <code>"</code> as copyable) and make sure the tests notice. Marking the quote as copyable in the short-string table fails 4,551 asseLine truncated
|
||||
Reference in new issue
Block a user