Ref C++ template code size (a)
We want to rely on shared libraries present at runtime so the binary we ship can have less size. This is undermined by the use of template classes in standard library.
As a simple example, consider that we use std::vector<int>
in our code. The functions used will be instantiated and stored in our app binary.
arpit@alice:~$ bloaty -d symbols ./a --debug-file=a.debug --source-filter=std::
FILE SIZE VM SIZE
-------------- --------------
8.4% 58 27.3% 346 std::cout@GLIBCXX_3.4
8.3% 57 27.3% 345 std::cin@GLIBCXX_3.4
34.2% 236 18.6% 236 std::vector<>::push_back()
26.2% 181 14.3% 181 std::__copy_move<>::__copy_m<>()
15.4% 106 8.4% 106 std::vector<>::_M_check_len()
7.5% 52 4.1% 52 std::_Vector_base<>::~_Vector_base()
100.0% 690 100.0% 1.24Ki TOTAL
arpit@alice:~$ bloaty -d symbols ./a --debug-file=a.debug -n 2
FILE SIZE VM SIZE
-------------- --------------
48.2% 6.84Ki 0.0% 0 [Unmapped]
39.1% 5.55Ki 100.0% 5.85Ki [43 Others]
12.8% 1.81Ki 0.0% 0 [ELF Section Headers]
100.0% 14.2Ki 100.0% 5.85Ki TOTAL
In our code of 14.2Ki
, we have 690 bytes
being occupied by std::vector
and other related things.
We can see that libstdc++.so
doesn’t contain std::vector<int>
by using nm -C /usr/lib/libstdc++.so | grep -i "vector"
.