|
{ |
|
"number": 100000, |
|
"title": "Trim unused interfaces", |
|
"body": "So far, the compiler could only trim unused interface __methods__; the interface __types__ themselves were left alone. There is not a huge cost associated with having these extra `MethodTable`s, but it sill impacts size/working set/startup. Initial estimate showed this could actually be up to 2% for BasicMinimalApi so I decided to investigate a fix.\r\n\r\nThis is an attempt to start trimming them. I chose a relatively conservative approach:\r\n\r\n* Stop unconditionally rooting the interface `MethodTable` in the implementing class `MethodTable` InterfaceList. Instead check whether someone else marked it first.\r\n* Track whether an interface type definition is used in any way. This avoids problem with variance, etc. If an interface definition is used, all instantiations that we were trying to optimize away get the `MethodTable` and won't be optimized. We should be able to do better than this, maybe, at some point.\r\n* Classes that implement generic interfaces with default methods need special treatment because we index into interface list at runtime and we might not know the correct index yet. So just forget the optimization in that case.\r\n\r\nFixes #66716.", |
|
"createdAt": "2024-03-20T08:55:34Z", |
|
"closedAt": "2024-03-23T22:28:34Z", |
|
"mergedAt": "2024-03-23T22:28:34Z", |
|
"state": "MERGED", |
|
"author": { |
|
"login": "MichalStrehovsky" |
|
}, |
|
"labels": { |
|
"nodes": [ |
|
{ |
|
"name": "area-NativeAOT-coreclr" |
|
} |
|
] |
|
}, |
|
"headRefName": "intfstrip", |
|
"additions": 224, |
|
"deletions": 57, |
|
"changedFiles": 21, |
|
"comments": { |
|
"totalCount": 13, |
|
"nodes": [ |
|
{ |
|
"body": "I vaguely remember that some part of the iOS managed static registrar or the linker pipeline may depend on the current behavior. So, cc @ivanpovazan, @simonrozsival in case they do remember the details or have some general concerns.", |
|
"createdAt": "2024-03-20T09:12:35Z" |
|
}, |
|
{ |
|
"body": "@filipnavara If I understand this change correctly, I don't think this should break iOS. There are cases when we explicitly cast objects to an interface they implement and call a method on the interface. I think that falls under the second bullet point and in that case the optimization won't be applied.", |
|
"createdAt": "2024-03-20T10:43:39Z" |
|
}, |
|
{ |
|
"body": "> I think that falls under the second bullet point and in that case the optimization won't be applied.\r\n\r\nThanks for checking!\r\n", |
|
"createdAt": "2024-03-20T10:48:50Z" |
|
}, |
|
{ |
|
"body": "/azp run runtime-nativeaot-outerloop", |
|
"createdAt": "2024-03-20T11:36:21Z" |
|
}, |
|
{ |
|
"body": "<samp>\nAzure Pipelines successfully started running 1 pipeline(s).<br>\r\n\n</samp>", |
|
"createdAt": "2024-03-20T11:36:34Z" |
|
}, |
|
{ |
|
"body": "ID: 100000 🎉 ", |
|
"createdAt": "2024-03-20T15:22:08Z" |
|
}, |
|
{ |
|
"body": "So this won't be quite the promised 2% size saving on WebapiAot template because once I started to worry about generic interfaces and the suppressions that assume IL level trimming we have within the libraries, it became 0.7%.\r\n\r\nIn libraries, we have several places where we call `GetInterfaces()` without a properly annotated/statically known type and assume that if we grab the type definition of the interface we got from `GetInterfaces()` and compare with `typeof(OpenVersion<>)`, it's going to be reliable and trim safe way to detect if the type implemented say `OpenVersion<int>`. It's safe in IL sense. In AOT sense, we could have stripped `OpenVersion<int>` if it was never used and then this trim-unsafe piece of code won't find it. So we can't do it. Oh well.\r\n\r\nBut the good news is that `dotnet new console --aot` followed by `dotnet publish /p:OptimizationPreference=Size /p:StackTraceSupport=false /p:UseSystemResourceKeys=true` is now ***877 kB***.\r\n\r\n# Size statistics\r\n\r\nPull request dotnet/runtime#100000\r\n\r\n| Project | Size before | Size after | Difference |\r\n|---------|-------------|------------|------------|\r\n| avalonia.app-linux | [ 24681968 ](https://github.com/MichalStrehovsky/rt-sz/actions/runs/8371281835/artifacts/1345532638) | [ 24615120 ](https://github.com/MichalStrehovsky/rt-sz/actions/runs/8371281835/artifacts/1345532361) | -66848 |\r\n| avalonia.app-windows | [ 22321152 ](https://github.com/MichalStrehovsky/rt-sz/actions/runs/8371281835/artifacts/1345533992) | [ 22253568 ](https://github.com/MichalStrehovsky/rt-sz/actions/runs/8371281835/artifacts/1345534230) | -67584 |\r\n| hello-linux | [ 1316304 ](https://github.com/MichalStrehovsky/rt-sz/actions/runs/8371281835/artifacts/1345530706) | [ 1266592 ](https://github.com/MichalStrehovsky/rt-sz/actions/runs/8371281835/artifacts/1345530587) | -49712 |\r\n| hello-minimal-linux | [ 1172808 ](https://github.com/MichalStrehovsky/rt-sz/actions/runs/8371281835/artifacts/1345530970) | [ 1119000 ](https://github.com/MichalStrehovsky/rt-sz/actions/runs/8371281835/artifacts/1345530503) | -53808 |\r\n| hello-minimal-windows | [ 947200 ](https://github.com/MichalStrehovsky/rt-sz/actions/runs/8371281835/artifacts/1345531948) | [ 898560 ](https://github.com/MichalStrehovsky/rt-sz/actions/runs/8371281835/artifacts/1345532788) | -48640 |\r\n| hello-windows | [ 1200640 ](https://github.com/MichalStrehovsky/rt-sz/actions/runs/8371281835/artifacts/1345532280) | [ 1149952 ](https://github.com/MichalStrehovsky/rt-sz/actions/runs/8371281835/artifacts/1345532013) | -50688 |\r\n| webapiaot-linux | [ 10363808 ](https://github.com/MichalStrehovsky/rt-sz/actions/runs/8371281835/artifacts/1345531354) | [ 10301344 ](https://github.com/MichalStrehovsky/rt-sz/actions/runs/8371281835/artifacts/1345531459) | -62464 |\r\n| webapiaot-windows | [ 9438720 ](https://github.com/MichalStrehovsky/rt-sz/actions/runs/8371281835/artifacts/1345532871) | [ 9377280 ](https://github.com/MichalStrehovsky/rt-sz/actions/runs/8371281835/artifacts/1345532767) | -61440 |\r\n", |
|
"createdAt": "2024-03-21T09:09:34Z" |
|
}, |
|
{ |
|
"body": "@dotnet/ilc-contrib this is ready for review", |
|
"createdAt": "2024-03-21T11:08:03Z" |
|
}, |
|
{ |
|
"body": "/azp run runtime-nativeaot-outerloop", |
|
"createdAt": "2024-03-21T11:24:25Z" |
|
}, |
|
{ |
|
"body": "<samp>\nAzure Pipelines successfully started running 1 pipeline(s).<br>\r\n\n</samp>", |
|
"createdAt": "2024-03-21T11:24:40Z" |
|
}, |
|
{ |
|
"body": "Hmm, the linux-arm failure is interesting but unrelated:\r\n\r\n```\r\n(lldb) bt\r\nThis version of LLDB has no plugin for the language \"assembler\". Inspection of frame variables will be limited.\r\n* thread #1, name = 'Microsoft.Exten', stop reason = signal SIGSEGV\r\n * frame #0: 0x00eab31e Microsoft.Extensions.Logging.Console.Tests`RhpCheckedAssignRefr1 at WriteBarriers.S:251\r\n frame #1: 0x011ec304 Microsoft.Extensions.Logging.Console.Tests`System.Runtime.TypeCast___cctor at TypeCast.cs:36\r\n frame #2: 0x0121c2b8 Microsoft.Extensions.Logging.Console.Tests`Internal.Runtime.CompilerHelpers.StartupCodeHelpers__RunInitializers(typeManager=<unavailable>, section=<unavailable>) at StartupCodeHelpers.cs:181\r\n frame #3: 0x0121be8e Microsoft.Extensions.Logging.Console.Tests`Internal.Runtime.CompilerHelpers.StartupCodeHelpers__InitializeModules(osModule=<unavailable>, pModuleHeaders=<unavailable>, count=<unavailable>, pClasslibFunctions=<unavailable>, nClasslibFunctions=<unavailable>) at StartupCodeHelpers.cs:53\r\n frame #4: 0x00e6f4d4 Microsoft.Extensions.Logging.Console.Tests`main [inlined] InitializeRuntime() at main.cpp:203:5 [opt]\r\n frame #5: 0x00e6f470 Microsoft.Extensions.Logging.Console.Tests`main(argc=9, argv=0xffca6804) at main.cpp:221:19 [opt]\r\n(lldb) dis -b -A thumbv7\r\nMicrosoft.Extensions.Logging.Console.Tests`RhpCheckedAssignRef:\r\n 0xeab2be <+0>: 0xf3bf8f5f dmb sy\r\n\r\nMicrosoft.Extensions.Logging.Console.Tests`RhpCheckedAssignRefr1:\r\n 0xeab2c2 <+4>: 0x6001 str r1, [r0]\r\n 0xeab2c4 <+6>: 0xf2400c4c movw r12, #0x4c\r\n 0xeab2c8 <+10>: 0xf2c00ca0 movt r12, #0xa0\r\n 0xeab2cc <+14>: 0x44fc add r12, pc\r\n 0xeab2ce <+16>: 0xf8dcc000 ldr.w r12, [r12]\r\n 0xeab2d2 <+20>: 0x4560 cmp r0, r12\r\n 0xeab2d4 <+22>: 0xd328 blo 0x89b328 ; <+106>\r\n 0xeab2d6 <+24>: 0xf2400c3e movw r12, #0x3e\r\n 0xeab2da <+28>: 0xf2c00ca0 movt r12, #0xa0\r\n 0xeab2de <+32>: 0x44fc add r12, pc\r\n 0xeab2e0 <+34>: 0xf8dcc000 ldr.w r12, [r12]\r\n 0xeab2e4 <+38>: 0x4560 cmp r0, r12\r\n 0xeab2e6 <+40>: 0xd21f bhs 0x89b328 ; <+106>\r\n 0xeab2e8 <+42>: 0xf6470ca0 movw r12, #0x78a0\r\n 0xeab2ec <+46>: 0xf2c00c9e movt r12, #0x9e\r\n 0xeab2f0 <+50>: 0x44fc add r12, pc\r\n 0xeab2f2 <+52>: 0xf8dcc000 ldr.w r12, [r12]\r\n 0xeab2f6 <+56>: 0x4561 cmp r1, r12\r\n 0xeab2f8 <+58>: 0xd31b blo 0x89b332 ; <+116>\r\n 0xeab2fa <+60>: 0xf6470c92 movw r12, #0x7892\r\n 0xeab2fe <+64>: 0xf2c00c9e movt r12, #0x9e\r\n 0xeab302 <+68>: 0x44fc add r12, pc\r\n 0xeab304 <+70>: 0xf8dcc000 ldr.w r12, [r12]\r\n 0xeab308 <+74>: 0x4561 cmp r1, r12\r\n 0xeab30a <+76>: 0xd212 bhs 0x89b332 ; <+116>\r\n 0xeab30c <+78>: 0xf2400c00 movw r12, #0x0\r\n 0xeab310 <+82>: 0xf2c00c9f movt r12, #0x9f\r\n 0xeab314 <+86>: 0x44fc add r12, pc\r\n 0xeab316 <+88>: 0xf8dcc000 ldr.w r12, [r12]\r\n 0xeab31a <+92>: 0xeb0c2090 add.w r0, r12, r0, lsr #10\r\n-> 0xeab31e <+96>: 0xf890c000 ldrb.w r12, [r0]\r\n 0xeab322 <+100>: 0xf1bc0fff cmp.w r12, #0xff\r\n 0xeab326 <+104>: 0xd100 bne 0x89b32a ; <+108>\r\n 0xeab328 <+106>: 0xe003 b 0x89b332 ; <+116>\r\n 0xeab32a <+108>: 0xf04f0cff mov.w r12, #0xff\r\n 0xeab32e <+112>: 0xf880c000 strb.w r12, [r0]\r\n 0xeab332 <+116>: 0x4770 bx lr\r\n(lldb)\r\n```\r\n\r\nCc @filipnavara \r\n\r\nRunfo:\r\n\r\n```\r\nrunfo get-helix-payload -j 320338f9-9d8c-4deb-9eeb-884cfb26892e -w Microsoft.Extensions.Logging.Console.Tests -o c:\\helix_payload\\Microsoft.Extensions.Logging.Console.Tests\r\n```", |
|
"createdAt": "2024-03-21T17:24:13Z" |
|
}, |
|
{ |
|
"body": "I'll wait with merging this until after #98712. I don't want to mask off how much of a regression that PR is.", |
|
"createdAt": "2024-03-21T17:26:19Z" |
|
}, |
|
{ |
|
"body": "> the linux-arm failure is interesting\r\n\r\nOpened https://github.com/dotnet/runtime/issues/100112", |
|
"createdAt": "2024-03-22T01:50:53Z" |
|
} |
|
] |
|
}, |
|
"reviewThreads": { |
|
"totalCount": 0, |
|
"nodes": [] |
|
}, |
|
"commits": { |
|
"totalCount": 3, |
|
"nodes": [ |
|
{ |
|
"commit": { |
|
"oid": "13e62212b9b6de6ff0b198207fcbe5531bf6b442", |
|
"message": "Trim unused interfaces\n\nSo far, the compiler could only trim unused interface __methods__; the interface __types__ themselves were left alone. There is not a huge cost associated with having these extra `MethodTable`s, but it sill impacts size/working set/startup. Initial estimate showed this could actually be up to 2% for BasicMinimalApi so I decided to investigate a fix.\n\nThis is an attempt to start trimming them. I chose a relatively conservative approach:\n\n* Stop unconditionally rooting the interface `MethodTable` in the implementing class `MethodTable` InterfaceList. Instead check whether someone else marked it first.\n* Track whether an interface type definition is used in any way. This avoids problem with variance, etc. If an interface definition is used, all instantiations that we were trying to optimize away get the `MethodTable` and won't be optimized. We should be able to do better than this, maybe, at some point.\n* Classes that implement generic interfaces with default methods need special treatment because we index into interface list at runtime and we might not know the correct index yet. So just forget the optimization in that case.\n\nFixes #66716.", |
|
"committedDate": "2024-03-20T08:49:22Z" |
|
} |
|
}, |
|
{ |
|
"commit": { |
|
"oid": "008f01ec8042cfa23abe4dff91bc07cde848b0c6", |
|
"message": "Also remove from reflection metadata", |
|
"committedDate": "2024-03-21T06:52:52Z" |
|
} |
|
}, |
|
{ |
|
"commit": { |
|
"oid": "7a4a308fa7351ff447cffc1f465af4db98d0e411", |
|
"message": "Fix tests", |
|
"committedDate": "2024-03-21T11:07:28Z" |
|
} |
|
} |
|
] |
|
} |
|
} |