[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: optimization improves code readability
- From: William Bubel <inmatarian@...>
- Date: Thu, 22 Jan 2009 22:52:42 -0500
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.
> Message: 10
> Date: Thu, 22 Jan 2009 07:37:20 -0500 (EST)
> From: Leo Razoumov <email@example.com>
> Subject: optimization improves code readability
> To: Lua list <firstname.lastname@example.org>
> Message-ID: <alpine.DEB.email@example.com>
> 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
> local msg_type = packet.msg
> local dev_id = packet.msg..packet.msg
> local coorX = packet.msg
> local coorY = packet.msg
> 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.