getml / reflect-cpp

A C++20 library for fast serialization, deserialization and validation using reflection. Supports JSON, BSON, CBOR, flexbuffers, msgpack, TOML, XML, YAML / msgpack.org[C++20]
https://getml.github.io/reflect-cpp/
MIT License
901 stars 76 forks source link

Compile time regex (related to issue #37) #42

Closed ChemiCalChems closed 8 months ago

ChemiCalChems commented 8 months ago

As discussed in #37, it would be beneficial to move over to compile-time regex. Hanickadot's CTRE allows for this. Here is the (hopefully) much awaited PR.

On my machine, tests now compile quicker and into smaller binaries. They also run marginally quicker, though I didn't test this with a big enough sample size to conclude anything other than anecdotally quicker runs.

~/src/reflect-cpp/build $ make clean > /dev/null && time make -j4 > /dev/null && du -b tests/json/reflect-cpp-json-tests 

real    1m30.108s
user    5m13.096s
sys 0m19.556s
31075952    tests/json/reflect-cpp-json-tests
~/src/reflect-cpp/build $ git switch main
Switched to branch 'main'
Your branch is up to date with 'origin/main'.
~/src/reflect-cpp/build $ make clean > /dev/null && time make -j4 > /dev/null && du -b tests/json/reflect-cpp-json-tests 

real    1m42.362s
user    6m0.853s
sys 0m21.433s
37749584    tests/json/reflect-cpp-json-tests 

With this we reap some of the benefits of compile-time regex, but it would be cool to move forward and be able to do compile-time parsing too. For that, we are going to need to make a lot of our internals to be constexpr (I think most of them can be constexpr as it stands). There is no reason not to do this, and would allow nice stuff to happen.

Once this PR is merged, #37 can be closed.

liuzicheng1987 commented 8 months ago

I'm amazed that things compile quicker, even though more is happening at compile time.

ChemiCalChems commented 8 months ago

@liuzicheng1987 std::regex is notorious for being incredibly slow both at compile-time and at run-time. Hana showed some data regarding this in her CTRE talks. Quite astounding it's so bad.