Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Perhaps this example is just meant to be illustrative, but if this is a real case, it seems easy to remove the branch:

    local counter = require("core.counter")

    local n = 1e9
    local c = counter.open("test")
    for i = 1,n do
       -- Add 9 for odd i, 0 for even.
       counter.add(c, 1 + (bit.band(i, 1) * 9))
    end


Yup. The link at the end goes to http://wiki.luajit.org/Numerical-Computing-Performance-Guide which mentions much the same: "Use bit.*, e.g. for conditional index computations."

It's certainly a real problem that can affect real code, this "issue" just falls a wee bit short demonstrating how to fix it.


Arguably it's an optimization that compiler could perform instead and a method JIT most likely would. However tracing JIT's optimizations are confined to a linear trace - which I guess exactly the limitation the author wanted to demonstrate.




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: