lua-users home
lua-l archive

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


Hi all,

Thanks for your suggestions! I think for now I will go with the script
preprocessing using string.gsub in the interest of expediency at the
cost of elegance. However at some point I would like to use a proper
lexer; thanks for the pointer, Peter, I didn't know about your code.

Another option would be token filtering, but that is something of an
arcane art to me at this point. :-)

For the curious the application here is a constraint satisfaction
solver, and these asserts are littered all over the inner loops. So any
unnecessary overhead, even a function call that would avoid constructing
the strings, is undesirable. I'm not trying to directly compete with C
here, but any performance I can squeeze out is most welcome...

Thanks again for your suggestions everybody.
Cheers,
- David

On this day of 05/10/07 06:08, xolox@home.nl saw fit to scribe:
>> 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.
> 


-- 
~David-Haley
http://david.the-haleys.org