This is an archive of the discontinued LLVM Phabricator instance.

[COFF] Optimize range extension thunk insertion memory usage
ClosedPublic

Authored by rnk on Mar 27 2019, 1:40 PM.

Details

Summary

This avoids allocating O(#relocs) of intermediate data for each section
when range extension thunks aren't needed for that section. This also
removes a std::vector from SectionChunk, which further reduces its size.

Instead, this change adds the range extension thunk symbols to the
object files that contain sections that need extension thunks. By adding
them to the symbol table of the parent object, that means they now have
a symbol table index. Then we can then modify the original relocation,
after copying it to read-write memory, to use the new symbol table
index.

This makes linking browser_tests.exe with no PDB 10.46% faster, moving
it from 11.364s to 10.288s averaged over five runs.

Diff Detail

Repository
rL LLVM

Event Timeline

rnk created this revision.Mar 27 2019, 1:40 PM
Herald added a project: Restricted Project. · View Herald TranscriptMar 27 2019, 1:40 PM
mstorsjo accepted this revision.Mar 27 2019, 2:27 PM

This looks good from both a performance and memory usage perspective - I'll try to test run it on a largish executable that needs thunks. The code takes a little while to wrap the head around, but I think I understand it.

I tested it on a largish testcase that uses thunks, where linking takes around 24 seconds, where code layout was pretty consistently 2450 ms before this patch and 2500 ms after it. So to my eye, it's a very, very marginal slowdown for the actual thunk cases, but clearly outweighed by everything else.

lld/COFF/Writer.cpp
460 ↗(On Diff #192515)

Does the initial value ~0U actually matter here? If Insertion.second is true (if this was the first insertion), the value will be unconditionally overwritten anyway, right? (I guess it's a good sentinel to safeguard against not updating it though, so it's probably ok as such, just trying to grasp all aspects of this.)

This revision is now accepted and ready to land.Mar 27 2019, 2:27 PM

I forgot to say... Thanks for doing this! This eases my conscience a little, for bloating things for others due to this feature :-)

ruiu added a comment.Mar 27 2019, 5:22 PM

Nice!

lld/COFF/Writer.cpp
460 ↗(On Diff #192515)

Yeah, it looks to me that this can be a set instead of a map, as it doesn't seem like it is using map values.

mstorsjo added inline comments.Mar 27 2019, 10:09 PM
lld/COFF/Writer.cpp
460 ↗(On Diff #192515)

It does use them (that's ThunkSymbolIndex), but the initial value inserted into the map with the insert mrthod could be anything, as it is always overwritten if it actually was a new key.

This revision was automatically updated to reflect the committed changes.
rnk marked an inline comment as done.
rnk added a comment.Mar 28 2019, 11:39 AM

Thanks! Looks like I never sent my reply comment. Oops :(

lld/COFF/Writer.cpp
460 ↗(On Diff #192515)

Right, it's going to be overwritten after insertion, I was just trying to pick a sentinel that would be more likely to crash.