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

Why the compiled object file is so huge? #1677

Closed
xchern opened this issue Jul 19, 2019 · 4 comments
Closed

Why the compiled object file is so huge? #1677

xchern opened this issue Jul 19, 2019 · 4 comments
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@xchern
Copy link

xchern commented Jul 19, 2019

Just tried to use some very basic features with following source code:

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

using json = nlohmann::json;

int main() {
    json j;
    std::cin >> j;
    std::cout << j["field1"];
}

The executable size is 3.05MB compiled with g++ (gcc version 9.1.0 (Rev3, Built by MSYS2 project)). Such a overhead is insane for a simple program. It is even bigger than the "json.hpp" header file (711KB).

I roughly viewed through the source code. Class methods are mostly defined in class body, which will be inlined automatically. And a lot of macros and templates are used. All these facts probably cause the bloat of binary size.

Is there any advice for reduce object file size?

@nlohmann
Copy link
Owner

Which compiler flags are you using?

@xchern
Copy link
Author

xchern commented Jul 19, 2019

I've just checked "-g" is used for including debug symbols, this truely contributes to the file size...

Removing that flag results in a smaller output (1.64M), it's still very big compared to 314KB for a empty main() function.

With "-O2" optimization flag, the size is reduced very much, 771KB compared to 313KB for empty main. This seems good enough. But I'm still in doubt whether file size will increase if more classes are compiled separately with serialization implementations in each class.

@nlohmann
Copy link
Owner

On macOS, I can get the binary down to 80 KB with Clang 8:

clang++ size.cpp -std=c++11 -I single_include -O3 -flto -DNDEBUG

@xchern
Copy link
Author

xchern commented Jul 20, 2019

I tried your flags with both clang 8 on linux and mingw g++ 9 on windows, result in 113KB and 728KB respectively. "-O3" is possibly too much, it's known to have problem for certain cases.

Any way, hundreds of KB is good enough for most cases.

And I tried to compile more separate files, the size is not accumulated, some commonly used code might be reused.

Thanks a lot for your help!

@nlohmann nlohmann added kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation labels Jul 21, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

No branches or pull requests

2 participants