3 ms·
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.counte
by haberman 11y ago
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
- tedunangst 11y agoYup. The link at the end goes to http://wiki.luajit.org/Numerical-Computing-Performance-Guide 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.
- mraleph 11y agoArguably 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.