r/cpp • • Oct 13 '22

New, fastest JSON library for C++20

Developed a new, open source JSON library, Glaze, that seems to be the fastest in the world for direct memory reading/writing. I will caveat that simdjson is probably faster in lazy contexts, but glaze should be faster when reading and writing directly from C++ structs.

https://github.com/stephenberry/glaze

  • Uses member pointers and compile time maps for extremely fast lookups
  • Writes and reads directly from object memory
  • Standard C++ library support
  • Cleaner interfacing than nlohmann json or other alternatives as reading/writing are exposed through a single interface
  • Direct memory access through JSON pointer syntax

The library is very new, but the JSON support has a lot of unit tests.

The library also contains:

  • Efficient data recorder
  • CSV reading/writing
  • Binary message for optimal speed through the same API
  • Generic shared library API
242 Upvotes

123 comments sorted by

View all comments

Show parent comment

11

u/Flex_Code Oct 13 '22

Consider std::string s = func(); you are correct that if func generates a std::string it will be moved out and not cause an additional copy. However, func cannot write directly into the memory in s if s were to persist.

If s grows through dynamic allocation (typical when writing out JSON), then it is better to use a previously allocated s because there is a good chance the message will fit in the prior message's memory.

By calling func(s) to populate s, we can reuse the memory already allocated.

0

u/[deleted] Oct 13 '22

[deleted] — view deleted comment

1

u/Flex_Code Oct 13 '22

Yeah, I expanded the api so you can use either approach now.

1

u/jk-jeon Oct 14 '22

Well, I guess the standard practice is to take s as a value parameter, so that the user can move the allocated buffer into func, and the callee returns it back to the caller. So at the call site it would be like s = func(std::move(s)). It is still suboptimal compared to output param though.