<div dir="ltr">
<div dir="ltr"><div>Hi Valentin.</div><div><br></div><div>Yes there is a pretty simple answer :) Parsing is far harder than serialization ! for parsing, you have to read a bit of the binary stream, find what type it is, then translate the data to a data type, which means allocating memory. On top of that you need to validate that it is not a mangled binary stream. And you need to do it piecemeal, with a lot of information about current steps. This is far more complex than translating a particular memory setting that you know the size of into a binary stream.</div><div><br></div><div>Now is there space for speedup ? maybe. It is always an interesting area to explore. But fundamentally, parsing being slower than serialisation is not surprising,</div><div><br></div><div>Thomas Depierre<br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, 7 Jun 2021 at 14:25, Valentin Micic <<a href="mailto:v@micic.co.za">v@micic.co.za</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="overflow-wrap: break-word;">Hmmm… I realised that my previous email may be seen as a comment rather than a question, so let me ask the question clearly.<div><br></div><div>Given that binary_to_term/1 is about two orders of magnitude slower than term_to_binary/1, is there anyone out there that may have a reasonable explanation for that?</div><div><br></div><div>Kind regards</div><div><br></div><div>V/</div><div><br></div><div><div><blockquote type="cite"><div>On 06 Jun 2021, at 02:07, Valentin Micic <<a href="mailto:v@micic.co.za" target="_blank">v@micic.co.za</a>> wrote:</div><br><div><div style="overflow-wrap: break-word;">Hi all,<div><br></div><div>I did some performance measurement recently that included conversion of an arbitrary erlang term to its external binary representation via term_to_binary/1, as well as reversing the result using binary_to_term/1.</div><div><br></div><div>I’ve noticed that term_to_binary/1 is significantly faster than binary_to_term/1.</div><div><br></div><div>Also, I’ve observed that binary_to_term/1 performance gets considerably worse as complexity of specified term increases, whilst term_to_binary/1 maintains (more-less) steady performance.</div><div><br></div><div><div style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgba(227,227,227,0.89)"><div style="margin:0px;font-stretch:normal;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">(cig@MacBook-Pro)40> tconvert:run( a, 10000000 ).</span></div><div style="margin:0px;font-stretch:normal;line-height:normal;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures"></span><br></div><div style="margin:0px;font-stretch:normal;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">term_to_binary/1 RETURN VALUE:<<131,100,0,1,97>></span></div><div style="margin:0px;font-stretch:normal;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">REQUEST COUNT:10000000</span></div><div style="margin:0px;font-stretch:normal;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">ELAPSED TIME (usec):97070</span></div><div style="margin:0px;font-stretch:normal;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">TIME PER REQUEST (usec): 0.009707</span></div><div style="margin:0px;font-stretch:normal;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">PROJECTED RATE (req/sec): <b>103018440</b>.30081384</span></div><div style="margin:0px;font-stretch:normal;line-height:normal;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures"></span><br></div><div style="margin:0px;font-stretch:normal;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">binary_to_term/1 RETURN VALUE:a</span></div><div style="margin:0px;font-stretch:normal;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">REQUEST COUNT:10000000</span></div><div style="margin:0px;font-stretch:normal;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">ELAPSED TIME (usec):3383483</span></div><div style="margin:0px;font-stretch:normal;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">TIME PER REQUEST (usec): 0.3383483</span></div><div style="margin:0px;font-stretch:normal;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">PROJECTED RATE (req/sec): <b>2955534</b>.2822765773</span></div><div style="margin:0px;font-stretch:normal;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">ok</span></div><div><span style="font-variant-ligatures:no-common-ligatures"><br></span></div></div></div><div><div style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgba(227,227,227,0.89)"><div style="margin:0px;font-stretch:normal;line-height:normal"><div style="margin:0px;font-stretch:normal;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">(cig@MacBook-Pro)41> tconvert:run( {a,<<1,2,3>>, b, [1,2,3], c, {1,2,3}, d, #{a=>1, b=>2, c=>3}}, 10000000 ).</span></div><div style="margin:0px;font-stretch:normal;line-height:normal;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures"></span><br></div><div style="margin:0px;font-stretch:normal;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">term_to_binary/1 RETURN VALUE:<<131,104,8,100,0,1,97,109,0,0,0,3,1,2,3,100,0,1,</span></div><div style="margin:0px;font-stretch:normal;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">                                98,107,0,3,1,2,3,100,0,1,99,104,3,97,1,97,2,97,</span></div><div style="margin:0px;font-stretch:normal;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">                                3,100,0,1,100,116,0,0,0,3,100,0,1,97,97,1,100,</span></div><div style="margin:0px;font-stretch:normal;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">                                0,1,98,97,2,100,0,1,99,97,3>></span></div><div style="margin:0px;font-stretch:normal;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">REQUEST COUNT:10000000</span></div><div style="margin:0px;font-stretch:normal;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">ELAPSED TIME (usec):97307</span></div><div style="margin:0px;font-stretch:normal;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">TIME PER REQUEST (usec): 0.0097307</span></div><div style="margin:0px;font-stretch:normal;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">PROJECTED RATE (req/sec): <b>102767529</b>.57135664</span></div><div style="margin:0px;font-stretch:normal;line-height:normal;min-height:13px"><span style="font-variant-ligatures:no-common-ligatures"></span><br></div><div style="margin:0px;font-stretch:normal;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">binary_to_term/1 RETURN VALUE:{a,<<1,2,3>>,</span></div><div style="margin:0px;font-stretch:normal;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">                                 b,</span></div><div style="margin:0px;font-stretch:normal;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">                                 [1,2,3],</span></div><div style="margin:0px;font-stretch:normal;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">                                 c,</span></div><div style="margin:0px;font-stretch:normal;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">                                 {1,2,3},</span></div><div style="margin:0px;font-stretch:normal;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">                                 d, </span></div><div style="margin:0px;font-stretch:normal;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">                                 #{a => 1,b => 2,c => 3}}</span></div><div style="margin:0px;font-stretch:normal;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">REQUEST COUNT:10000000</span></div><div style="margin:0px;font-stretch:normal;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">ELAPSED TIME (usec):8747426</span></div><div style="margin:0px;font-stretch:normal;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">TIME PER REQUEST (usec): 0.8747426</span></div><div style="margin:0px;font-stretch:normal;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">PROJECTED RATE (req/sec): <b>1143193</b>.4377038456</span></div><div style="margin:0px;font-stretch:normal;line-height:normal"><span style="font-variant-ligatures:no-common-ligatures">ok</span></div><div><span style="font-variant-ligatures:no-common-ligatures"><br></span></div></div></div></div><div><br></div><div><span style="font-variant-ligatures:no-common-ligatures"><br></span></div><div><span style="font-variant-ligatures:no-common-ligatures">I’ve performed testing on R21.1.</span></div><div><span style="font-variant-ligatures:no-common-ligatures">Any thoughts?</span></div><div><span style="font-variant-ligatures:no-common-ligatures"><br></span></div><div><span style="font-variant-ligatures:no-common-ligatures">V/</span></div></div></div></blockquote></div><br></div></div></blockquote></div>

</div>