lua-users home
lua-l archive

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


----- Original Message -----
From: Ian Millington
Date: 6/26/2011 5:04 PM
A while back I posted a commercial job offer for someone to help add
in-place mutation operators to Lua (+=, -=, *=, /=, %= and ^=). My
domain is in a scripting language for games, and a good deal of the
lua scripts consist of changing variable values, such as:

target.health -= 2
player.ammo.shells -= 1

so this saves a *lot* of typing.
This just made my day.

It isn't just about typing. It's also about performance. When written like:

        player.ammo.shells = player.ammo.shells - 1

Lua must:

* Look up player.
* Look up ammo.
* Look up shells.
* Subtract 1.
* Look up player.
* Look up ammo.
* Set via hash the shells key to be the result of the subtraction.

When written as "player.ammo.shells -= 1", Lua must:

* Look up player.
* Look up ammo.
* Look up shells.
* Subtract 1.
* Directly store the result.

Hooray!  (I hope this is how the patch works, anyway...)

I have just pushed this to the LuaPlus repository on GitHub, #ifdef'ed as LUA_MUTATION_OPERATORS.

Josh