lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Fri, Sep 21, 2012 at 06:11:02PM -0400, Jay Carlson wrote:
> On Sep 10, 2012, at 1:45 PM, Roberto Ierusalimschy wrote:
> 
> >> i'm studying lua's implement now, i'm wondering why lua's table need a
> >> dummy node, why not just set node to NULL if table's hash part is empty?
> >> could someone tell me the reason?
> > 
> > It avoids an extra test (t->node == NULL) when querying tables.
> 
> My instinct is the test itself should be somewhere between cheap and free
> these days. For non-empty tables there would be an additional
> predicted-not-taken branch, taking up icache and some vague
> non-architected resources. For empty tables, a NULL check would avoid the
> latency of a memory access and the trivial cache pressure of having the
> dummy plug--er, dummy node sitting around.

Conditional branching kills pipelining, particularly when it's so data
dependent--you couldn't make a branch predictor good enough to claw back the
cost. So, on the face of it, it seems like a sane optimization to me.

But more importantly...
 
<snip> 
> If performance is close, clarity and simplicity wins. The time everyone
> spent reading this message is probably greater than the potential combined
> execution time savings on all Lua instances.

... fewer conditions to check for usually makes for saner code, IMO. I often
add dummy values like this just to reduce the number of conditional cases.
The performance gain is just a bonus.