Repository navigation
ILC: use relative pointers in interface maps when optimizing for size - #138046
Sergio0694 wants to merge 1 commit into
Conversation
Each interface of a type takes a full pointer in its MethodTable, and each of them needs a base relocation in the image. When the compiler optimizes for size (--Os) and the image is not dehydrated, emit the interface map entries as 32-bit relative pointers instead, which saves about 6 bytes per entry. Dehydration encodes both kinds of pointers in the same space, so it doesn't gain anything there. The format is recorded per MethodTable by negating the interface count, so types created by the type loader keep full pointers and don't need to be told apart from the types of the image. Readers use MethodTable.GetInterface, and the casting helpers scan negative counts in a separate loop. The offsets of the fields after the interface map use the size of its entries. The code that handles relative interface maps is behind an intrinsic that the compiler folds to false unless the compilation can produce them (or is a multifile compilation), so other builds are not affected. Add a smoke test that runs the interface tests with relative interface maps. Co-authored-by: Copilot App <[email protected]>
|
Azure Pipelines: Successfully started running 3 pipeline(s). 12 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
|
Tagging subscribers to this area: @agocke, @dotnet/ilc-contrib |
|
It may be more interesting to look at using native size relative pointers for all pointers in MethodTable. It is likely to produce more interesting startup and size savings by avoiding relocs (vtable calls would be a tiny bit slower). I am not a fan of the 32-bit vs. native size special casing in the data structures. |
|
I prototyped it but it seems like it actually regresses size compared to this PR (and MSIX size in particular):
|
What are the code/data/reloc section sizes before/after in the prototype for this option? |
|
I got this:
|
|
Is the prototype turning all pointers in the MethodTable into relative pointers? The data suggests that it is only some subset. I mean all pointers - including vtable slots, etc. |
|
Yes I think that was from an earlier prototype not with all of them being relative. I changed all of them to be relative (except the ones for the generic dictionary, that's also doable but it needs a JIT/EE change, not sure if that'd be acceptable for this), I also run a comparison with that + the original 32-bit relative interface maps. It seems the two can stack up nicely together:
I haven't pushed the changes yet. Is combining both something you'd consider? They do seem both quite beneficial. |
That's not a problem.
As I have said above - I am not a fan of the 32-bit vs. native size special casing in the data structures. Can you check what percentage of interfaces is inherited from the base? If there is a lot of inheritance, the interface map can be compressed by taking advantage of the inheritance. Walking the interface would not be a simpler linear scan anymore, but that should not be a deal breaker. Similarly, if there is a lot of inheritance of vtable slots, they can be compressed by extra indirections as well (CoreCLR vtables are compressed like that for historic reasons). |
|
Here's the numbers for all experiments:
It seems even with all folding enabled for vtables, the 32-bit interface map still saves another ~720KB on top of that.
It seems that only had about 9% to 15% of overlap (using chunks of 8 or 4), so taking into account the pointers needed for each chunk it seems like it would've canceled out most of the gains. Interfaces instead had almost 40% overlap. |
This means that there are 720kB / 4 = 180,000 interface implementations. Do you have an idea where most of them come from? |
|
Wait I haven't pushed any of the new changes yet! 😅 Will check about the interfaces and report back (not home). |
|
I'm having mixed feeling about this because this doesn't produce any size savings in practice. It requires setting optimization preference to Size, which by default enables data dehydration. Then it requires turning off data dehydration to save anything. It's more like a working set optimization than size optimization? I don't know how to think about this. |
|
At minimum I'm concerned about test matrix expansion. There is only one test testing this mix (we have entire passes for sizeopt/speedopt). Triggering this requires flipping an unsupported switch. |
|
I was actually thinking of putting this also behind an Ilc feature switch (which would default to true when optimizing for size), so that each consumer would be able to evaluate whether they want the extra perf tradeoff in return for size, if this saves enough size for them. E.g. I'd imagine we'd use this for Windows components, and we'd likely use this for the Store, but it would seem nice to make this opt-in too? Especially give it also already kinda depends on disabling dehydration, which is another Ilc switch anyway, so that's also configurable already. What do you think? |
|
The problem is that this saves nothing in terms of size if you already specified OptimizationPreference=Size: that switch turns on dehydration and these pointers don't cost anything extra (they are already 32bit relative plus use a look up table to squeeze even more size out). See the first rt-sz table here: #308. Store asks to optimize for size and then goes "not like that" by specifying an unsupported switch (disable dehydration) and now we need a new feature that only really does something when dehydration is off. The savings in that combination are real (see the second table in rt-sz), but it's in a unsupported configuration. As it stands right now, this is a feature for Store (and Windows team maybe) only and makes me somewhat more anxious about recommending this untested combination for Windows and Store to use given we're going to start changing a lot more under it. |
|
Potential way out of the test matrix nightmare could be to use relative pointers in both the blended mode and optimize for size mode, and only use non-relative pointers when optimizing for speed. But we need to think this through. I'm not a fan of the current shape. |
I assume the store app is turning off dehydration since it is large one-time working set hit. Is that right? The relative pointers or 32-bit pointers are a simplistic on-the-fly decompression scheme. There are more aggressive decompression schemes possible with different trade-offs between binary size, steady state perf, startup perf (and runtime complexity). @Sergio0694 Where would you ultimately like to land with this tradeoff? |
Right, for the Store it permanently adds ~55 MB of working set use (and regresses startup perf by ~30ms). Similar situation when we first tried it in Windows, the startup re-hydration call was consistently showing up in traces and it was too expensive (especially because for Windows components we just merge everything into a single binary, so that startup overhead would also just keep growing as we add more code, even though that code might not all be needed). The general idea for all of these investigations was to identify ways to reduce the binary size without affecting perf in a noticeable way (which it seems for pretty much all of these changes it doesn't, also because the Store isn't too much CPU bound, or I mean it is in some cases but mostly due to interop/XAML layout work, not just because of some tight CPU loop in .NET). For working set the idea was that none of these size optimizations should make it worse (as in, worst case it should stay the same). |
Stores the interface map entries of a MethodTable as 32-bit relative pointers when ILC optimizes for size (
--Os) and the image is not dehydrated. A full pointer entry costs 8 bytes plus a base relocation in the image, a relative entry costs 4 bytes and no relocation.Design
_usNumInterfaces) for MethodTables with a relative interface map, so readers can tell the format apart without a new flag (there is no free flag bit that works for arrays and strings, and0x00200000is read by the GC asMTFlag_Collectible).OptimizationPreference=Sizeenables dehydration by default, so this currently takes effect withOptimizationPreference=SizeplusIlcDehydrate=false.MethodTable.MayHaveRelativeInterfaceMapsis an intrinsic folded by ILC, true only when relative maps are enabled (or in a multifile compilation, since modules are compiled separately). Other builds fold the relative handling away, so the default casting helpers are unchanged.MethodTable.GetInterface. The casting helpers scan negative counts with a separate loop.MethodTable.h/.inl) and the offsets of the fields after the interface map are updated.Size impact
Microsoft Store (CsWinRT 3.0), Release x64,
OptimizationPreference=SizewithIlcDehydrate=false, same inputs, ILC with and without this change. Measured on the equivalent change on top of the release/11.0 gate backport, as the Store can't be built with the current main compiler yet. The MSIX is the full published package.WinStore.App.dllThe image is smaller, but the package is larger: 32-bit relative offsets compress worse than the repetitive absolute pointers and the relocation entries that they remove (deflating the DLL alone gives the same direction: +372,372 B). This is an on-disk and in-memory saving, not a download size saving.
Performance
Casts in images built with
Sizeand relative maps are slower, as the map entries need to be decoded. A BenchmarkDotNet run (single round, noisy machine) of interfaceis/castclassover 1 to 16 interface maps showed 0% to about 19% higher cost inSizebuilds with relative maps, and +41% for a mixed-type cast loop.Speedbuilds were within noise of the baseline, since the relative handling is folded away.Testing
InterfacesSizeOptimized, which runs theInterfacessmoke test withOptimizationPreference=SizeandIlcDehydrate=false, and checks that the interface map of a type is encoded as relative.UnitTests/Interfaces.csgets a test for interface map lookups with 0 to 9 interfaces (hits, misses,castclass), generic instantiations created by the type loader at run time, and array covariance.nativeaottree): 29/29 on this branch, including the new test.Not verified: ARM64, Linux, macOS, wasm, multifile/shared library builds, startup time, and external readers of the NativeAOT interface map (the cDAC data descriptor still exports
NumInterfacesasT_UINT16andRuntimeTypeSystemstays atn1; nothing in this repo reads the map that way).Part of #137567.
Note
This PR description and the change were prepared with GitHub Copilot.