Skip to content

ILC: use relative pointers in interface maps when optimizing for size - #138046

Open
Sergio0694 wants to merge 1 commit into
dotnet:mainfrom
Sergio0694:user/sergiopedri/nativeaot-relative-interface-maps
Open

Sergio0694 wants to merge 1 commit into
dotnet:mainfrom
Sergio0694:user/sergiopedri/nativeaot-relative-interface-maps

Conversation

@Sergio0694

Copy link
Copy Markdown
Contributor

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

  • The count is stored negated (_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, and 0x00200000 is read by the GC as MTFlag_Collectible).
  • Types created by the type loader keep a positive count and full pointers, so they don't need to be told apart from the types of the image.
  • Relative maps are only emitted for 64-bit targets with relative pointer support, and not when the data is dehydrated (dehydration encodes both kinds of pointers in the same space). Note that OptimizationPreference=Size enables dehydration by default, so this currently takes effect with OptimizationPreference=Size plus IlcDehydrate=false.
  • MethodTable.MayHaveRelativeInterfaceMaps is 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.
  • Readers use MethodTable.GetInterface. The casting helpers scan negative counts with a separate loop.
  • The native runtime (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=Size with IlcDehydrate=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.dll MSIX
Before 84,872,704 36,900,929
After 83,160,576 (-1,712,128, -2.02%) 37,250,781 (+349,852, +0.95%)

The 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 Size and relative maps are slower, as the map entries need to be decoded. A BenchmarkDotNet run (single round, noisy machine) of interface is/castclass over 1 to 16 interface maps showed 0% to about 19% higher cost in Size builds with relative maps, and +41% for a mixed-type cast loop. Speed builds were within noise of the baseline, since the relative handling is folded away.

Testing

  • Added InterfacesSizeOptimized, which runs the Interfaces smoke test with OptimizationPreference=Size and IlcDehydrate=false, and checks that the interface map of a type is encoded as relative. UnitTests/Interfaces.cs gets 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.
  • NativeAOT smoke tests (nativeaot tree): 29/29 on this branch, including the new test.
  • The Store built with the same change (release/11.0 based) was smoke tested in Windows Sandbox (home, apps, gaming and library tabs), no crashes. That was an earlier revision of this change; the final build was not smoke tested.

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 NumInterfaces as T_UINT16 and RuntimeTypeSystem stays at n1; nothing in this repo reads the map that way).

Part of #137567.

Note

This PR description and the change were prepared with GitHub Copilot.

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

Copy link
Copy Markdown
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.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @agocke, @dotnet/ilc-contrib
See info in area-owners.md if you want to be subscribed.

@jkotas

jkotas commented Nov 21, 2026

Copy link
Copy Markdown
Member

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.

@Sergio0694

Copy link
Copy Markdown
Contributor Author

I prototyped it but it seems like it actually regresses size compared to this PR (and MSIX size in particular):

Configuration Variant WinStore.App.dll Δ vs gate MSIX Δ vs gate
Not dehydrated (IlcDehydrate=false) Gate only 84,872,704 n/a 36,900,929 n/a
Not dehydrated (IlcDehydrate=false) 32-bit relative interface maps 83,160,576 -1,712,128 (-2.02%) 37,250,781 +349,852 (+0.95%)
Not dehydrated (IlcDehydrate=false) Native-size (8 B) relative pointers 84,030,464 -842,240 (-0.99%) 37,524,899 +624,970 (+1.69%)
Dehydrated (IlcDehydrate=true) Gate only 68,738,560 n/a 35,591,352 n/a
Dehydrated (IlcDehydrate=true) Native-size (8 B) relative pointers 68,741,632 +3,072 (+0.00%) 35,593,024 +1,672 (+0.00%)

@jkotas

jkotas commented Nov 21, 2026

Copy link
Copy Markdown
Member

Not dehydrated (IlcDehydrate=false) Native-size (8 B) relative pointers

What are the code/data/reloc section sizes before/after in the prototype for this option?

@Sergio0694

Copy link
Copy Markdown
Contributor Author

I got this:

Section Gate only Native-size (8 B) relative pointers Δ 32-bit relative interface maps (PR #138046) Δ
.text (code) 34,145,280 34,147,328 +2,048 34,146,816 +1,536
.rdata (read-only data) 40,948,736 40,949,248 +512 39,843,328 -1,105,408
.data 3,941,376 3,941,376 0 3,941,376 0
.pdata 2,308,608 2,308,608 0 2,308,608 0
.rsrc 2,048 2,048 0 2,048 0
.reloc 3,525,632 2,680,832 -844,800 2,917,376 -608,256
File 84,872,704 84,030,464 -842,240 83,160,576 -1,712,128

@jkotas

jkotas commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

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.

@Sergio0694

Copy link
Copy Markdown
Contributor Author

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:

Variant WinStore.App.dll Δ vs gate .reloc .text .rdata
Gate only 84,872,704 n/a 3,525,632 34,145,280 40,948,736
All pointers (current prototype) 82,934,272 -1,938,432 (-2.28%) 1,326,080 34,394,624 40,960,512
All pointers + 32-bit relative interface maps 81,828,864 -3,043,840 (-3.59%) 1,324,032 34,396,160 39,855,104

I haven't pushed the changes yet. Is combining both something you'd consider? They do seem both quite beneficial.

@jkotas

jkotas commented Nov 23, 2026

Copy link
Copy Markdown
Member

it needs a JIT/EE change,

That's not a problem.

Is combining both something you'd consider?

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).

@Sergio0694

Copy link
Copy Markdown
Contributor Author

Here's the numbers for all experiments:

Variant DLL DLL vs gate MSIX MSIX vs gate .text .rdata .reloc
Gate 84,872,704 n/a 36,900,929 n/a 34,145,280 40,948,736 3,525,632
All pointers (12r): related type, interface map entries, vtable code slots 82,934,272 -2.28% 38,773,198 +5.07% 34,394,624 40,960,512 1,326,080
All pointers + 32-bit interface map entries (13r) 81,828,864 -3.59% 38,765,101 +5.05% 34,396,160 39,855,104 1,324,032
All pointers + interface map inheritance (15r) 82,141,184 -3.22% 38,445,919 +4.19% 34,395,136 40,167,936 1,324,544
15r + relative dictionary slots (16r) 82,052,608 -3.32% 38,431,628 +4.15% 34,493,952 40,169,984 1,135,104
16r + 32-bit interface map entries (17r) 81,314,816 -4.19% 38,433,535 +4.15% 34,494,976 39,431,168 1,135,104

It seems even with all folding enabled for vtables, the 32-bit interface map still saves another ~720KB on top of that.

"if there is a lot of inheritance of vtable slots, they can be compressed by extra indirections as well"

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.

@jkotas

jkotas commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

the 32-bit interface map still saves another ~720KB on top of that.

This means that there are 720kB / 4 = 180,000 interface implementations. Do you have an idea where most of them come from?

@jkotas
jkotas marked this pull request as ready for review November 24, 2026 02:52
@Sergio0694

Copy link
Copy Markdown
Contributor Author

Wait I haven't pushed any of the new changes yet! 😅

Will check about the interfaces and report back (not home).

@MichalStrehovsky

Copy link
Copy Markdown
Member

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.

@MichalStrehovsky

Copy link
Copy Markdown
Member

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.

@Sergio0694

Copy link
Copy Markdown
Contributor Author

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?

@MichalStrehovsky

Copy link
Copy Markdown
Member

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.

@MichalStrehovsky

Copy link
Copy Markdown
Member

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.

@jkotas

jkotas commented Nov 24, 2026

Copy link
Copy Markdown
Member

dehydration is off

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?

@Sergio0694

Copy link
Copy Markdown
Contributor Author

"I assume the store app is turning off dehydration since it is large one-time working set hit. Is that right?"

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).

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-NativeAOT-coreclr community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants