lua-users home
lua-l archive

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


> I want to keep them around in the source so that I can use them
> for debugging, but I'd also like to turn them off to make things as fast
> as possible (I am testing something for performance).

Preprocessing your scripts is one option, but have you considered the following (untested):

function assert(cond, ...)
   if not cond then error(string.format(...), 2) end
   return cond, ...
end

This should avoid most unnecessary string construction/concatenation. Are you worried this will still impact performance? As David suggested, another option is CPP

#define assert(cond, ...) if not cond then error(string.format(__VA_ARGS__), 2) end

The only pitfalls here (that i know of) is that CPP doesn't respect Lua's long strings (as Roberto mentioned) and outputs line directives, which you either need to suppress with a command-line option or strip using something like sed "/^#/d".

Yet another option, a lot safer than a generic preprocessor like CPP, is using an actual Lua lexer to strip the assert's. See for example ltokens[1] or my lexer[2]. Though this would require you to write a few lines of code :)

 - Peter Odding

[1] http://www.tecgraf.puc-rio.br/~lhf/ftp/lua/#ltokens
[2] http://xolox.ath.cx/lua/lexer.html

> The manpage for assert() here says that defining NDEBUG before
> #including assert.h will make assert()s vanish.  I believe this to be
> pretty standard.
That would never work. He's talking about Lua source-code. If you were to include assert.h in a Lua script using CPP, it would produce garbage output.