Eliminate redundant copies of EMPTY_UNIT object, saving 2.5 KB.

There are currently 11 copies of the `EMPTY_UNIT` object in the
`Calculator.exe` binary, which not only wastes space/footprint in the
binary itself, but also means that each copy must be separately
initialized, which effects performance.

The reason for this is that the object is defined in a shared header
file, which then is included by multiple .cpp files, causing each
translation unit (.obj) to get a full complete copy of the object.

By marking the object as `extern __declspec(selectany)` we can
instruct the linker to pick any of the global objects, as we do
not need to have 11 unique versions of the EMPTY_UNIT object,
we only need 1.

The net result is that the `Calculator.exe` binary size is reduced by
2,560 bytes (or 2.5 KB) with no change in behavior, other than
the small performance benefit of not initializing 10 redundant copies
of the object.
This commit is contained in:
Jeff Genovy 2019-03-09 14:03:17 -08:00
commit 8983a1cb46

View file

@ -51,7 +51,7 @@ namespace UnitConversionManager
// null checks.
//
// unitId, name, abbreviation, isConversionSource, isConversionTarget, isWhimsical
const Unit EMPTY_UNIT = Unit{ -1, L"", L"", true, true, false };
extern __declspec(selectany) const Unit EMPTY_UNIT = Unit{ -1, L"", L"", true, true, false };
struct Category
{