lua-users home
lua-l archive

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

For the most part, I really agree. I find often that scanning a table
for a string is a waste, when I could easily make a set out of the
strings, and do a straight lookup on the table with the string I'm
looking for as the key. What we have here really is that Lua has
normalized the overhead of most data structures, so that we're more free
to choose the best one to solve the task. That is where our readability
comes from, as the best data structure for a task usually has the most
logical code to go with it.

William Bubel

> Message: 10
> Date: Thu, 22 Jan 2009 07:37:20 -0500 (EST)
> From: Leo Razoumov <>
> Subject: optimization improves code readability
> To: Lua list <>
> Message-ID: <alpine.DEB.2.00.0901220724040.10603@meson.home>
> Content-Type: TEXT/PLAIN; format=flowed; charset=US-ASCII
> This message is about Lua advocacy.
> It just crossed my mind that in most programming languages
> optimizations make code _less_ readable. In Lua, on the other hand, some
> optimizations make code *MORE* readable.
> For examples, we all know that access to local variables is
> substantially faster that access to globals or table hashes. 
> If I have to work with packet that contains a message represented by
> an array of bytes an optimized way to use its content would be
> something like this
> local function parse(packet)
>      local header   = packet.header
>      local msg_begin= packet.msg[1]
>      local msg_type = packet.msg[2]
>      local dev_id   = packet.msg[3]..packet.msg[4]
>      local coorX    = packet.msg[5]
>      local coorY    = packet.msg[6]
>      ....
> end
> Not only using local bindings to msg elements make the code run faster
> (avoids repetitive packet.msg hash lookup) but, more importantly, such
> code provides an explicit message format documentation. When the
> message format changes, the code breaks. Fixing the broken code will
> simultaneously update the documentation! How convenient.
> --Leo--