3 ms·
C++ function to compare two enums: bool operator ==(SomeEnum left, SomeEnum right) { return left == right; } In my bindings (in C++): LM_BIND_NONMEMBER_OP(Some
by phaedrus 15y ago
C++ function to compare two enums:
bool operator ==(SomeEnum left, SomeEnum right) { return left == right; }
In my bindings (in C++):
LM_BIND_NONMEMBER_OP(SomeEnum, (==))
In Io script:
if( obj property == SomeEnumInstance, doStuff())
The == in the script calls the overloaded == operator defined in C++, via the binding. Turns out, you do a lot of this in game programming, at least with Irrlicht (or maybe it's a quirk of what I'm doing in my game scripts). Testing an enum for equality is cheap in C++. Calling == from script via a binding adds an order magnitude (or two!) more overhead. I could improve this by optimizing for this case.
(You may wonder - why I don't just convert enums to script numbers, which surely are faster. Mainly because I wrote my Io to C++ binding library to be strongly typed; it does not allow mixing a number with an enum and if you auto-demoted enums to numbers in script that type information would be lost. And I don't think Io's interpreter goes to the same lengths as, say, a Smalltalk does to optimize for numbers; Io takes an even more purist approach to treating everything as an object.)
- justincormack 15y agoLuaJIT's ffi lets you use native C types directly in Lua, which fixes that issue and keeps the type info.