<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">Hi Thomas, <div class=""><br class=""></div><div class="">Thanks for your input. </div><div class=""><br class=""></div><div class="">Indeed, parsing being slower that serialisation is not necessarily surprising. However, when a difference in performance is, well, two orders of magnitude, that it may be reasonable to ask if term_to_binary/1 does any actual work. Put differently, is there any difference between internal and external term presentation?</div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">V/</div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><blockquote type="cite" class=""><div dir="ltr" class=""><div dir="ltr" class=""><div class="">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></div></blockquote><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><div><br class=""><blockquote type="cite" class=""><div class="">On 07 Jun 2021, at 14:38, Thomas Depierre <<a href="mailto:depierre.thomas@gmail.com" class="">depierre.thomas@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">
<div dir="ltr" class=""><div class="">Hi Valentin.</div><div class=""><br class=""></div><div class="">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 class=""><br class=""></div><div class="">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 class=""><br class=""></div><div class="">Thomas Depierre<br class=""></div></div><br class=""><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" class="">v@micic.co.za</a>> wrote:<br class=""></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;" class="">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 class=""><br class=""></div><div class="">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 class=""><br class=""></div><div class="">Kind regards</div><div class=""><br class=""></div><div class="">V/</div><div class=""><br class=""></div><div class=""><div class=""><blockquote type="cite" class=""><div class="">On 06 Jun 2021, at 02:07, Valentin Micic <<a href="mailto:v@micic.co.za" target="_blank" class="">v@micic.co.za</a>> wrote:</div><br class=""><div class=""><div style="overflow-wrap: break-word;" class="">Hi all,<div class=""><br class=""></div><div class="">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 class=""><br class=""></div><div class="">I’ve noticed that term_to_binary/1 is significantly faster than binary_to_term/1.</div><div class=""><br class=""></div><div class="">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 class=""><br class=""></div><div class=""><div style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgba(227,227,227,0.89)" class=""><div style="margin:0px;font-stretch:normal;line-height:normal" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">(cig@MacBook-Pro)40> tconvert:run( a, 10000000 ).</span></div><div style="margin:0px;font-stretch:normal;line-height:normal;min-height:13px" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""></span><br class=""></div><div style="margin:0px;font-stretch:normal;line-height:normal" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">term_to_binary/1 RETURN VALUE:<<131,100,0,1,97>></span></div><div style="margin:0px;font-stretch:normal;line-height:normal" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">REQUEST COUNT:10000000</span></div><div style="margin:0px;font-stretch:normal;line-height:normal" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">ELAPSED TIME (usec):97070</span></div><div style="margin:0px;font-stretch:normal;line-height:normal" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">TIME PER REQUEST (usec): 0.009707</span></div><div style="margin:0px;font-stretch:normal;line-height:normal" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">PROJECTED RATE (req/sec): <b class="">103018440</b>.30081384</span></div><div style="margin:0px;font-stretch:normal;line-height:normal;min-height:13px" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""></span><br class=""></div><div style="margin:0px;font-stretch:normal;line-height:normal" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">binary_to_term/1 RETURN VALUE:a</span></div><div style="margin:0px;font-stretch:normal;line-height:normal" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">REQUEST COUNT:10000000</span></div><div style="margin:0px;font-stretch:normal;line-height:normal" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">ELAPSED TIME (usec):3383483</span></div><div style="margin:0px;font-stretch:normal;line-height:normal" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">TIME PER REQUEST (usec): 0.3383483</span></div><div style="margin:0px;font-stretch:normal;line-height:normal" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">PROJECTED RATE (req/sec): <b class="">2955534</b>.2822765773</span></div><div style="margin:0px;font-stretch:normal;line-height:normal" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">ok</span></div><div class=""><span style="font-variant-ligatures:no-common-ligatures" class=""><br class=""></span></div></div></div><div class=""><div style="margin:0px;font-stretch:normal;font-size:11px;line-height:normal;font-family:Menlo;background-color:rgba(227,227,227,0.89)" class=""><div style="margin:0px;font-stretch:normal;line-height:normal" class=""><div style="margin:0px;font-stretch:normal;line-height:normal" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">(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" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""></span><br class=""></div><div style="margin:0px;font-stretch:normal;line-height:normal" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">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" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">                                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" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">                                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" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">                                0,1,98,97,2,100,0,1,99,97,3>></span></div><div style="margin:0px;font-stretch:normal;line-height:normal" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">REQUEST COUNT:10000000</span></div><div style="margin:0px;font-stretch:normal;line-height:normal" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">ELAPSED TIME (usec):97307</span></div><div style="margin:0px;font-stretch:normal;line-height:normal" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">TIME PER REQUEST (usec): 0.0097307</span></div><div style="margin:0px;font-stretch:normal;line-height:normal" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">PROJECTED RATE (req/sec): <b class="">102767529</b>.57135664</span></div><div style="margin:0px;font-stretch:normal;line-height:normal;min-height:13px" class=""><span style="font-variant-ligatures:no-common-ligatures" class=""></span><br class=""></div><div style="margin:0px;font-stretch:normal;line-height:normal" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">binary_to_term/1 RETURN VALUE:{a,<<1,2,3>>,</span></div><div style="margin:0px;font-stretch:normal;line-height:normal" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">                                 b,</span></div><div style="margin:0px;font-stretch:normal;line-height:normal" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">                                 [1,2,3],</span></div><div style="margin:0px;font-stretch:normal;line-height:normal" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">                                 c,</span></div><div style="margin:0px;font-stretch:normal;line-height:normal" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">                                 {1,2,3},</span></div><div style="margin:0px;font-stretch:normal;line-height:normal" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">                                 d, </span></div><div style="margin:0px;font-stretch:normal;line-height:normal" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">                                 #{a => 1,b => 2,c => 3}}</span></div><div style="margin:0px;font-stretch:normal;line-height:normal" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">REQUEST COUNT:10000000</span></div><div style="margin:0px;font-stretch:normal;line-height:normal" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">ELAPSED TIME (usec):8747426</span></div><div style="margin:0px;font-stretch:normal;line-height:normal" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">TIME PER REQUEST (usec): 0.8747426</span></div><div style="margin:0px;font-stretch:normal;line-height:normal" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">PROJECTED RATE (req/sec): <b class="">1143193</b>.4377038456</span></div><div style="margin:0px;font-stretch:normal;line-height:normal" class=""><span style="font-variant-ligatures:no-common-ligatures" class="">ok</span></div><div class=""><span style="font-variant-ligatures:no-common-ligatures" class=""><br class=""></span></div></div></div></div><div class=""><br class=""></div><div class=""><span style="font-variant-ligatures:no-common-ligatures" class=""><br class=""></span></div><div class=""><span style="font-variant-ligatures:no-common-ligatures" class="">I’ve performed testing on R21.1.</span></div><div class=""><span style="font-variant-ligatures:no-common-ligatures" class="">Any thoughts?</span></div><div class=""><span style="font-variant-ligatures:no-common-ligatures" class=""><br class=""></span></div><div class=""><span style="font-variant-ligatures:no-common-ligatures" class="">V/</span></div></div></div></blockquote></div><br class=""></div></div></blockquote></div>

</div>
</div></blockquote></div><br class=""></div></body></html>