8 comments

  • U1F9842 days ago
    From the article: Lookup tables are always faster than calculation - is that true? I'd think that while in the distant past maybe today due to memory being much slower than CPU the picture is different nowadays. If you're calculating a very expensive function over a small domain so the lookup fits in L1 Cache then I can see it would be faster, but you can do a lot of calculating in the time needed for a single main memory access.
    • ay2 days ago
      You will need to first sit and ballpark, and then sit and benchmark, and discover your ballpark was probably wrong anyhow:-)<p>Some (for me) useful pointers to that regard for both:<p>1. <a href="https:&#x2F;&#x2F;www.agner.org&#x2F;optimize&#x2F;instruction_tables.pdf" rel="nofollow">https:&#x2F;&#x2F;www.agner.org&#x2F;optimize&#x2F;instruction_tables.pdf</a> - an extremely nice resource on micro architectural impacts of instructions<p>2. <a href="https:&#x2F;&#x2F;llvm.org&#x2F;docs&#x2F;CommandGuide&#x2F;llvm-mca.html" rel="nofollow">https:&#x2F;&#x2F;llvm.org&#x2F;docs&#x2F;CommandGuide&#x2F;llvm-mca.html</a> - tooling from Intel that allows to see some of these in real machine code<p>3. <a href="https:&#x2F;&#x2F;www.intel.com&#x2F;content&#x2F;www&#x2F;us&#x2F;en&#x2F;developer&#x2F;articles&#x2F;tool&#x2F;performance-counter-monitor.html" rel="nofollow">https:&#x2F;&#x2F;www.intel.com&#x2F;content&#x2F;www&#x2F;us&#x2F;en&#x2F;developer&#x2F;articles&#x2F;t...</a> - shows you whether the above is matching the reality (besides the CPU alone, more often than not your bottleneck <i>is</i> actually memory accesses; at least on the first access which wasn’t triggered by a hardware prefetcher or a hint to it. On Linux it would be staring at “perf top” results.<p>So, the answer is as is very often - “it depends”.
      • rft3 hours ago
        A few more links for low level CPU benchmarking<p>1 - <a href="https:&#x2F;&#x2F;www.uops.info&#x2F;index.html" rel="nofollow">https:&#x2F;&#x2F;www.uops.info&#x2F;index.html</a> similar content to Anger&#x27;s tables<p>2 - <a href="https:&#x2F;&#x2F;reflexive.space&#x2F;zen2-ibs&#x2F;" rel="nofollow">https:&#x2F;&#x2F;reflexive.space&#x2F;zen2-ibs&#x2F;</a> how to capture per micro op data on AMD &gt;= Zen 1 CPUs<p>I agree on &quot;it depends&quot;. And usually not only on your actual code and data, but also how you arrange it over cache lines, what other code on the same core&#x2F;complex&#x2F;system is doing to your view of the cache and some other internal CPU features like prefetchers or branch predictors.
      • bayindirh2 days ago
        ...and we always circle back to &quot;premature optimization is the root of all evil&quot;, since processors are a wee bit more intelligent with our instructions than we thought. :)
        • mananaysiempre2 days ago
          Not <i>that</i> intelligent. If you have two loads and one store per cycle, then that’s it. (Recall that we have SSDs with 14 GB&#x2F;s sequential reads now, yet CPU clocks are below 6 GHz.) Most of the computational power of a high-performance CPU is in the vector part; still the CPU won’t try to exploit it if you don’t, and the compiler will try but outside of the simplest cases won’t succeed. (Most of the computational power of a high-performance <i>computer</i> is in the GPU, but I haven’t gotten that deep yet.)<p>I don’t mean to say that inefficient solutions are unacceptable; they have their place. I do mean to say that, for example, for software running on end-users’ computers (including phones), the programmer is hardly entitled to judge the efficiency on the scale of the entire machine—the entire machine does not belong to them.<p>&gt; We <i>should</i> forget about small inefficiences, say 97% of the time; premature optimization is the root of all evil. Yet we should not pass up our opportunities in that critical 3%. A good programmer will not be lulled into complacency by such reasoning, he will be wise to look carefully at the critical code; but only <i>after</i> that code has been identified.<p>D. E. Knuth (1974), “Structured Programming with go to Statements”, <i>ACM Comput. Surv.</i> 6(4).
          • bayindirh1 day ago
            You are right, but with a good optimizing compiler and out of order execution, your code will not work the way you guess most of the time, even though it accomplishes what you want.<p>On the other hand, while doing high performance compute, the processor will try to act smart to keep everything saturated. As a result, you still need to look at cache trash ratio, IPC, retirement ratio, etc. to see whether you are using the system at its peak performance, and again CPU is doing its thing to keep the numbers high, but that&#x27;s not enough of course. You have to do your own part and write good code.<p>In these cases where you share the machine (which can be a cluster node or a mobile phone), maximizing this performance is again beneficial since it allows smoother operation both for your and other users&#x27; code in general. Trying to saturate the system with your process is a completely different thing, but you don&#x27;t have to do that to have nice and performant code.<p>GPU computation is nice, and you can do big things fast, but it&#x27;s not suitable for optimizing and offloading every kind of task, and even if though the task is suitable for the GPU, the scale of the computation still matters, because a competent programmer can fit billions of computations until a GPU starts running your kernel. The overhead is just too big.<p>Knuth&#x27;s full quote doesn&#x27;t actually invalidate me, because that&#x27;s how I operate while writing code, designed for high performance or not.
          • sitkack3 hours ago
            And so the line continues<p><a href="https:&#x2F;&#x2F;hn.algolia.com&#x2F;?dateRange=all&amp;page=0&amp;prefix=false&amp;query=%22Yet%20we%20should%20not%20pass%20up%20our%20opportunities%20in%20that%20critical%22&amp;sort=byDate&amp;type=comment" rel="nofollow">https:&#x2F;&#x2F;hn.algolia.com&#x2F;?dateRange=all&amp;page=0&amp;prefix=false&amp;qu...</a>
    • mananaysiempre2 days ago
      &gt; Lookup tables are always faster than calculation - is that true?<p>Maybe on the Z80. Contemporary RAM was quite fast compared to it, by our sad standards.<p>A table lookup per byte will see you hit a pretty hard limit of about 1 cycle per byte on all x86 CPUs of the last decade. If you’re doing a state machine or a multistage table[1] where the next table index depends on both the next byte and the previous table value, you’ll be lucky to see half that. Outracing your SSD[2] you’re not, with this approach.<p>If instead you can load a 64-bit chunk (or several!) at a time, you’ll have quite a bit of leeway to do some computation to it before you’re losing to the lookup table, especially considering you’ve got fast shifts and even multiplies (another difference from the Z80). And if you’re doing 128- or 256-bit vectors, you’ve got even more compute budget—but you’re also going to spend a good portion of it just shuffling the right bytes into the right positions. Ultimately, though, one of your best tools there is going to be ... an instruction that does 16 resp. 32 lookups in a 16-entry table at a time[3].<p>So no, if you want to be fast on longer pieces of data, in-memory tables are not your friend. On smaller data, with just a couple of lookups, they could be[4]. In any case, you need to be thinking about your code’s performance in detail for these things to matter—I can’t think of a situation where “prefer a lookup table” is a useful heuristic. “Consider a lookup table” (then measure), maybe.<p>[1] <a href="https:&#x2F;&#x2F;www.unicode.org&#x2F;versions&#x2F;latest&#x2F;ch05.pdf" rel="nofollow">https:&#x2F;&#x2F;www.unicode.org&#x2F;versions&#x2F;latest&#x2F;ch05.pdf</a><p>[2] <a href="https:&#x2F;&#x2F;lemire.me&#x2F;en&#x2F;talk&#x2F;perfsummit2020&#x2F;" rel="nofollow">https:&#x2F;&#x2F;lemire.me&#x2F;en&#x2F;talk&#x2F;perfsummit2020&#x2F;</a><p>[3] <a href="http:&#x2F;&#x2F;0x80.pl&#x2F;notesen&#x2F;2008-05-24-sse-popcount.html" rel="nofollow">http:&#x2F;&#x2F;0x80.pl&#x2F;notesen&#x2F;2008-05-24-sse-popcount.html</a><p>[4] <a href="https:&#x2F;&#x2F;tia.mat.br&#x2F;posts&#x2F;2014&#x2F;06&#x2F;23&#x2F;integer_to_string_conversion.html" rel="nofollow">https:&#x2F;&#x2F;tia.mat.br&#x2F;posts&#x2F;2014&#x2F;06&#x2F;23&#x2F;integer_to_string_conver...</a>
    • flohofwoe2 days ago
      On the Z80 any memory access had a fixed cost of 3 clock cycles (in reality the memory system could inject wait cycles, but that was an esoteric case). Together with the instruction fetch of 4 clock cycles the fastest instruction to load an 8-bit value from an address that&#x27;s already in a 16-bit register (like LD A,(HL)) takes 7 clock cycles.<p>The fastest instructions that didn&#x27;t access memory (like adding two 8-bit registers) were 4 clock cycles, so there&#x27;s really not much room to beat a memory access with computation.<p>Today &quot;it depends&quot;, I still use lookup tables in some places in my home computer emulators, but only after benchmarking showed that the table is actually slightly faster.
    • bayindirh2 days ago
      Depends on the hardware and what you are making with that hardware. Some processors can do complicated things stupidly fast (e.g. when SIMD done right), and for some hardware platforms, a mundane operation can be very costly since they are designed for other things primarily.<p>My favorite story is an embedded processor which I forgot its ISA. The gist was, there was a time budget, and doing a normal memory access would consume 90% of that budget alone. The trick was to use the obscure DMA engine to pump data into the processor caches asynchronously. This way, moving data was only ~4% of the same budget, and they have beaten their performance targets by a large margin.
      • bobmcnamara3 hours ago
        I&#x27;ve run into this on PowerQuicc network processors. It was so handy having the packets(or at least header) dropped straight into the cache
    • whizzter2 days ago
      The article does mention cache friendly access patterns in the same context.<p>But yes, you&#x27;re right. Back when I started with optimizations in the mid 90s memory _latencies_ were fairly minor compared to complex instructions so most things that wasn&#x27;t additions (and multiplications on the Pentium) would be faster from a lookup table, over time memory latencies grew and grew as clock speeds and other improvements made the distance to the physical memory an actual factor and lookup tables less useful compared to recomputing things.<p>Still today there are things that are expensive enough that can be fit in a lookup table that is small enough that it doesn&#x27;t get evicted from cache during computation, but they&#x27;re few.
    • yorwba2 days ago
      In this case, the lookup table is used for popcount, and there&#x27;s a comment in the Z80 assembly that says &quot;One lookup vs eight bit tests.&quot; If the code could make use of a hardware popcount instruction, the lookup table would lose, but if that isn&#x27;t available, a 256-byte lookup table could be faster. So it&#x27;s less &quot;lookup tables are always faster&quot; and more &quot;lookup tables can be faster, this is one such case.&quot;
      • ooisee2 days ago
        probably fastest popcount in z80 that does not use aligned table, would be shift A through flag, then conditional INC C, unrolled, still slower than ld l,a: ld b,(hl).
    • haiku20772 days ago
      &gt; Lookup tables are always faster than calculation - is that true?<p>I know it&#x27;s not always true on the Nintendo 64, because it shared a single bus between the RAM and &quot;GPU&quot;: <a href="https:&#x2F;&#x2F;youtu.be&#x2F;t_rzYnXEQlE?t=94" rel="nofollow">https:&#x2F;&#x2F;youtu.be&#x2F;t_rzYnXEQlE?t=94</a>, <a href="https:&#x2F;&#x2F;youtu.be&#x2F;Ca1hHC2EctY?t=827" rel="nofollow">https:&#x2F;&#x2F;youtu.be&#x2F;Ca1hHC2EctY?t=827</a>
      • sumtechguy3 hours ago
        Basically if the thing you are doing can be precomputed. Then you can use a lookup table. Then at that point do you have the space for it? Is the time to get it out of memory less than the operation you are caching in the lookup table. Then it could be a good candidate for a lookup table. Many times that can be true. But not always. Even if that is all true you can end up hurting something else because your lookup table evicted something else important from the l1&#x2F;l2 cache. So you also have to test it in context.
    • devnullbrain2 days ago
      You are correct and I&#x27;ve even ran into a situation where build-time evaluation was slower than runtime calculation, thanks to code size.
    • Taniwha2 days ago
      I&#x27;m a sometimes CPU architect and came here to argue just this - modern CPUs have far far slower memory access (in clocks) than z80 memory access. To be fair you can probably fit any z80 table you&#x27;re using into modern L1 cache, but even so you&#x27;re looking at multiple clocks rather than 1.
  • _notreallyme_2 days ago
    Optimizing code on MMU-less processor versus MMU and even NUMA capable processor is vastly different.<p>The fact that the author achieves only a 3 to 6 times speedup on a processor running at a frequency 857 faster should have led to the conclusion that old optimizations tricks are awfully slow on modern architecture.<p>To be fair, execution pipeline optimization still works the same, but not taking into account the different layers of cache, the way the memory management works and even how and when actual RAM is queried will only lead to suboptimal code.
    • ooisee2 days ago
      Seems like, You&#x27;ve got it backwards — and that makes it so much worse. ^_^<p>I ported from ABAP to Z80. Modern enterprise SAP system → 1976 processor. The Z80 version is almost as fast as the &quot;enterprise-grade&quot; ABAP original. On my 7MHz ZX Spectrum clone, it&#x27;s neck-and-neck. On the Agon Light 2, it&#x27;ll probably win. Think about that: 45-year-old hardware competing with modern SAP infrastructure on computational tasks. This isn&#x27;t &quot;old tricks don&#x27;t work on new hardware.&quot; This is &quot;new software is so bloated that Paleolithic hardware can keep up.&quot; (but even this is nonsense - ABAP is not designed for this task =)<p>The story has no moral, it is just for fun.
    • guenthert1 hour ago
      Are we intentionally ignoring that ABAP is byte code interpreted?
  • martinald1 hour ago
    It&#x27;s interesting how this stuff works. I started learning &quot;tech&quot; as a millenial in the late 90s when the internet and web was relatively new.<p>As such I picked up an awful lot of networking&#x2F;windows&#x2F;linux &quot;fundamentals&quot; simply because you had to know it to fix anything (truing - and failing - to get my crappy 28k winmodem working on Linux probably taught me many &quot;months&quot; worth of fundamentals alone!).<p>The other thing is when you are younger learning this stuff, most of us had pretty hard financial constraints on what we were doing. I couldn&#x27;t persuade my parents to replace that winmodem with a proper modem (which I would do without thinking now), so you really had to make do with what you had.
  • orbifold2 days ago
    Writing style and length of paragraphs strongly suggest that this is AI generated in full.
    • ooisee2 days ago
      Not yet in full, it is ~45% generated.<p>Two reasons: 1) English is not my native tongue, 2) I hate LinkedIn article style -&gt; let LLM convert my hadrcore-oldschool style into something like &quot;You won&#x27;t believe what my grandmother&#x27;s cat taught me about ...&quot;
      • layer81 hour ago
        What’s wrong with hardcore-oldschool style? Why on earth would you want LinkedIn style?
      • bee_rider2 hours ago
        Some folks get very annoyed by the whiff of LLMs, and will complain loudly. I guess people who aren’t annoyed have no reason to post about it, though.<p>Perhaps if you post your oldschool notes and their LLMified version and see which rises to the top, haha.
      • AnimalMuppet3 hours ago
        I would rather read a non-native speaker write something hardcore-oldschool than read LLM-generated &quot;You won&#x27;t believe what my grandmother&#x27;s cat taught me about...&quot; You may get corrections about your English. You may even get complaints (not everyone on HN is nice all the time). But my impression is that you will get fewer complaints than you will from something that &quot;feels&quot; LLM-generated. (Or maybe that&#x27;s just my personal taste.)
        • bee_rider2 hours ago
          It is considered socially acceptable to just straightforwardly complain about an LLM-written post, while complaints about somebody’s English are usually phrased as constructive feedback. The latter takes more effort and is not as effective as a launching point for a rant, so the former might just be easier to detect based on lines-of-text.
    • bapak1 hour ago
      Absolutely right. Kinda sad that this format is frowned upon now. I like data-rich articles without SEO fluff.
    • edg50003 hours ago
      It was fun to read though, it flows oddly but it works. Great case study of appropriate AI use!
  • bravesoul22 days ago
    It&#x27;s amusing that the writing style is akin to a LinkedIn what XYZ taught me about B2B sales.
    • ooisee2 days ago
      Guilty as charged—and totally intentional.<p>Turns out &quot;I want a burger&quot; beats &quot;we have equivalent burger at home&quot; every time—even when the home solution is objectively better.<p>So yes, this reads like &quot;What my goldfish taught me about microservices.&quot; But unlike those posts, this story has no moral—just nerdy fun with enterprise software roasting.<p>Sometimes you gotta speak the language they actually read there +)).
  • mdaniel2 hours ago
    Please don&#x27;t forget to put a license in your repo. Based on the zvdb linked to in the readme I&#x27;m guessing you prefer MIT but explicit is always better than leaving the audience wondering
  • IceDane2 hours ago
    Saying you ported SAP makes no sense. You ported a program you wrote for the SAP platform.<p>Porting all of SAP would be equivalent to porting all known human cancers from the human genome to a different newly discovered alien genome, both in terms of scale as well as sheer evil.
    • mdaniel2 hours ago
      It really makes me want to flag this submission because of how grossly misleading such a title is, despite it being correctly copied from the upstream source
  • peteforde2 days ago
    I have only one question: does the author know anything about coding ABAP like it&#x27;s a Z80? I wish that they&#x27;d addressed this.
    • ooisee2 days ago
      yes, it is addressed in another repo <a href="https:&#x2F;&#x2F;github.com&#x2F;oisee&#x2F;zvdb">https:&#x2F;&#x2F;github.com&#x2F;oisee&#x2F;zvdb</a><p>and another article about zvdb-abap from ~1.5 years ago.
      • peteforde2 days ago
        My comment was a reaction to the fact that you called out your experience&#x2F;wisdom on the topic no less than five distinct times in a short (otherwise great!) post.<p>I would argue that by doing so, you made it the unintended central theme of the post.<p>I&#x27;m not saying this to hurt your feelings. That you didn&#x27;t perceive the obvious sarcasm in my late-night comment suggests that you might not want to be perceived as pompous in your writing style.<p>My suggestion is that one explicit &quot;I&#x27;m an expert&quot; reminder per post is a perfectly good number.
        • ooisee2 days ago
          oh!, now I got it =) will increase &quot;Shy&quot; parameter and decrease &quot;Braggart&quot; parameters in my &quot;Normal speech&quot; to &quot;LinkedInese&quot; translator +)<p>Thank you for feedback =)<p>(I was thinking that it is not MY wisdom i am talking, but about &quot;Z80&quot; wisdom - all the code I have seen on demoscene and e-zines.)
          • peteforde1 day ago
            I am (definitely) not telling you to act shy or not be proud of your accomplishments.<p>Ultimately, it&#x27;s up to you if you want to redeclare your bona fides in almost every paragraph. It doesn&#x27;t make for great reading, but it&#x27;s your call either way.<p>If anything, I&#x27;m encouraging you to let your narrative and actions speak for your skills.
        • ooisee2 days ago
          Nice recursive joke!