ThePhD / sol2

Sol3 (sol2 v3.0) - a C++ <-> Lua API wrapper with advanced features and top notch performance - is here, and it's great! Documentation:
http://sol2.rtfd.io/
MIT License
4.06k stars 493 forks source link

Provide struct for sol::automagic_flags::none to reduce generated code size #1510

Open Perl99 opened 1 year ago

Perl99 commented 1 year ago

Firstly, thank you for this great library.

I noticed a potential easy way to reduce size of the generated code in case when usertype does not need any automagical detection. I have some usertypes that are singletons or only expose a few methods. I used sol::no_constructor like this:

auto mySingleton = lua.new_usertype<MySingleton>("MySingleton", sol::no_constructor);
mySingleton["someFunc"] = &MySingleton::someFunc;
auto mySingletonInstance = MySingleton::Instance();

I did an experiment and I saved about 250 KB of object code per usertype (debug mode, MSVC x64) when registering them in this way:

typedef sol::constant_automagic_enrollments<sol::automagic_flags::none> no_automagic;
constexpr no_automagic no_automagic_enrollments { false, false, false, false, false, false, false, false, false };

auto mySingleton = lua.new_usertype<MySingleton>("MySingleton", no_automagic_enrollments);
mySingleton["someFunc"] = &MySingleton::someFunc;
auto mySingletonInstance = MySingleton::Instance();

I think it is worth mentioning in the documentation and providing something like no_automagic_enrollments in the headers so that everyone can benefit from smaller generated code size = faster compile time (even if it is optimized in release).