This is an archive of the discontinued LLVM Phabricator instance.

[ELF] Make InputSection smaller
ClosedPublic

Authored by MaskRay on Nov 7 2020, 4:33 PM.

Details

Summary

On LP64/Windows platforms, this decreases sizeof(InputSection) from 208 (larger
on Windows) to 184.

For a large executable (7.6GiB, inputSections.size()=5105122,
make<InputSection> called 4835760 times), this decreases cgroup
memory.max_usage_in_bytes by 0.6%

Diff Detail

Event Timeline

MaskRay created this revision.Nov 7 2020, 4:33 PM
MaskRay requested review of this revision.Nov 7 2020, 4:33 PM
MaskRay updated this revision to Diff 303683.Nov 7 2020, 4:37 PM

Improve a basic block sections comment

grimar accepted this revision.Nov 9 2020, 12:01 AM

LGTM

This revision is now accepted and ready to land.Nov 9 2020, 12:01 AM
MaskRay updated this revision to Diff 303906.Nov 9 2020, 9:40 AM
MaskRay edited the summary of this revision. (Show Details)

Fix Windows

This revision was landed with ongoing or failed builds.Nov 9 2020, 9:55 AM
This revision was automatically updated to reflect the committed changes.
lxfind added a subscriber: lxfind.Nov 10 2020, 5:21 PM
lxfind added inline comments.
lld/ELF/InputSection.h
393

Curious, what's the purpose of static_assert like this? Will the program behave incorrectly if the size of InputSection is larger than 184?
Asking because with this assertion whoever adding or removing fields from this class or the base class will need to remember updating this static_assert accordingly, which I am not sure is scalable. That doesn't seem to serve the typical purpose of static_assert (which should be that the program behavior depends on it to be true) I think?

MaskRay marked an inline comment as done.Nov 10 2020, 5:58 PM
MaskRay added inline comments.
lld/ELF/InputSection.h
393

There are a few other static_assert for objects which are allocated a lot. This is important to prevent accidental regression or memory usage and performance.

lxfind added inline comments.Nov 10 2020, 8:25 PM
lld/ELF/InputSection.h
393

There is a static_assert for SectionPiece, which makes sense since it's small and simple, and it makes sense that we want to make sure the bit trick makes it fall within 16 bytes.
But I don't think a class as complicated as InputSection falls into that category. InputSection has 3 levels of inheritance hierarchy, with a ton of members. 184 is kind of like a magic number already (you probably have the compiler printed out the size instead of eye-balled it). This doesn't seem to be the right way to catch memory increases of classes.
If we really want to have some guarantee here, it might make sense to add a static_assert for SectionBase since it's extended by everything else, but imho not a class so far down the inheritance structure.
Finally, I'm not sure it would serve a real purpose. Whenever anyone is touching these classes, they would just go ahead and modify the static_assert to make it pass since it''s so complicated anyway, which is pure trouble than catching anything because there isn't really a way to "accidentally" regress it. A regression would come from adding fields on purpose.
Maybe I am missing something here. I see that @espindola added the other assert, what do you think?

MaskRay marked 2 inline comments as done.Nov 10 2020, 10:19 PM
MaskRay added inline comments.
lld/ELF/InputSection.h
393

More than 90% sections are InputSection and not other SectionBase derivatives. We even have bits in SectionBase which are only used by InputSection. The type we want to optimize for is InputSection, not SectionBase (which is not allocated independently), and InputSection is an apparent memory usage/performance bottleneck so I think static_assert is on topic.

If we really want to have some guarantee here, it might make sense to add a static_assert for SectionBase since it's extended by everything else, but imho not a class so far down the inheritance structure.

This static_assert is very useful and I felt bad that we had not added it earlier so that D68065 regressed the memory usage by more than 1%.

This is of course not pure trouble - the fields are very stable. You can search for other logs about my optimization on the structure members.