Hacker News new | ask | show | jobs
by jjmarr 722 days ago
magic_enum is killing my build time with endless template instantiations. Is this going to be faster?
3 comments

magic_enum works by walking all possible enumeration values from one-by-one in a wide range at compile time, instantiating a function template for each one so it can extract the __PRETTY_FUNCTION__ name, which is very slow. The C++26 feature just directly returns the vector of the named enumerators in one go, so it should be way faster.

They have a reference implementation on godbolt under clang, so you can play around with that. I did not try it yet.

Wow. I'm trying to make some of these template instantiations explicit on a large project I'm on as magic_enum is one of the largest contributors to our build-time.

It's nice to know I can just transition to C++26 to fix this.

Do you think you could try my library [1] and let me know how it performs in comparison? I've been curious about its compile-time performance, but I've never tried to compare its performance against that of magic_enum.

[1] https://news.ycombinator.com/item?id=32236447

If you're using C++20 or above, you could try conjure_enum (https://github.com/fix8mt/conjure_enum) It's based on magic_enum but optimized for C++20. Not sure about compile times although in our testing and with our test users it hasn't been reported as an issue.

Yes, there is magic_enum already - and we based this implementation on the core of magic_enum, but refocused for C++20, using some of the key features of this language version such constexpr algorithms, std::source_location and concepts; we also improved on and expanded the API.

I'm trying it, and this library doesn't work on clang 14. Do you have any insight as to why?
Not supported. Minimum 15. See "8. Compiler support"