Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Precompiled JSON library? #1842

Closed
tawmoto opened this issue Nov 17, 2019 · 12 comments
Closed

Precompiled JSON library? #1842

tawmoto opened this issue Nov 17, 2019 · 12 comments
Labels
kind: question state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated

Comments

@tawmoto
Copy link

tawmoto commented Nov 17, 2019

Hi,
Is there a way to precompile the JSON library, instead of including a 22k LOC header file?
The compilation times increased dramatically when trying to switch from cJSOn (300 LOC heeader file) to nlohmann JSON. If we never modify that code, I see no point of recompiling everytime 22k LOC if we can have it precompiled.
We love the library but the single-include header is too big, I know that Catch2 wants to drop also the single-header because of this issue.
There are multiple "solutions" to this, but none of them is elegant enough (frw declarations, etc).
Thanks! 👍

@nlohmann
Copy link
Owner

The library heavily relies on templates, so I am not sure how to achieve this. Any ideas?

Apart from this, the multi-header variant contains a header with forward declarations - maybe this can help in the meantime.

@t-b
Copy link
Contributor

t-b commented Nov 18, 2019

@tawmoto Does your compiler support precompiled headers? I've made good experience with that for getting the compilation times down. I have not used it with this library though.

@tawmoto
Copy link
Author

tawmoto commented Nov 18, 2019

@nlohmann I will try the forward declarations. It's very hard for me to convince my colleagues to switch from cJSON to nlohmann json for this reason, although I love your library, it's fantastic. We use a lot precompiled libraries (openssl, curl, json, etc) with great results in compilation time. Because of this problem (big header), we switched from Catch2 to google test, which we precompile.
Here is the link regarding Catch2 decision:
https://codingnest.com/the-future-of-catch2/
https://twitter.com/horenmar_ctu/status/1093219300410880000

I really think of a solution to make it work in both situations. :-)
I see that there is already an old request for this: #84

@t-b Yes, that is my idea also, I will try this. The problem is that we compile with 4 compilers (gcc 5.5, 8.3, clang and VS2019) and I have to implement PCH for all of them in order to convince my colleagues.

Thank you both for your answers! 👍

@gregmarr
Copy link
Contributor

I've used this library precompiled under MSVC, Clang, Objective C++.

@nlohmann
Copy link
Owner

I've used this library precompiled under MSVC, Clang, Objective C++.

Could you share some experiences regarding compile times?

@gregmarr
Copy link
Contributor

I don't have any data, but the compile times are very reasonable.

@tawmoto
Copy link
Author

tawmoto commented Nov 19, 2019

I generated a PCH but unfortunetaly it's huge, 61MB!

g++ -c json.hpp -o json.hpp.gch

-rw-rw-r-- 1 taw taw 791K nov 19 12:11 json.hpp
-rw-rw-r-- 1 taw taw  61M nov 19 12:46 json.hpp.gch

Perhaps the library is better suited for big projects, I have multiple small projects: 8 sloc, 15 sloc, 50 sloc. The json.hpp itself is 20 sloc.

Thank you guys for all your help.:)

@gregmarr
Copy link
Contributor

If you don't already have a PCH for , , , and the like, then you're going to get those included in this PCH. What effect did it have on your compile time? Are you comparing your compile time to something including all the standard headers with and without a PCH?

@tawmoto
Copy link
Author

tawmoto commented Nov 19, 2019

@gregmarr I could not make the PCH to work, but a 61MB PCH inside a 10MB project is not possible for my team, unfortunetaly.
Thanks again for the help, I will stick with cJSON at the moment.

@tlee-vertiv
Copy link

We tried to use nlohmann::json across different executable and libraries in embedded system. It would save a lot of flash space if we could compile nlohmann::json into a shared library.

@nlohmann
Copy link
Owner

nlohmann commented Dec 2, 2019

The library is heavily using templates, both for the internal types and for features like creating an array from an iterator range of any STL containers. I have no idea how to make a shared library out of this.

However, though I could reproduce the size of the PCH, the binaries are usually much smaller binaries. For instance, a small pretty printer

#include <nlohmann/json.hpp>
#include <iostream>

int main()
{
	nlohmann::json j;
	std::cin >> j;
	std::cout << j.dump(2) << std::endl;
}

compiles to a 68 KB binary (-O3 -flto -DNDEBUG) on my machine using Clang.

@stale
Copy link

stale bot commented Jan 1, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated label Jan 1, 2020
@stale stale bot closed this as completed Jan 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: question state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated
Projects
None yet
Development

No branches or pull requests

5 participants