Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable .NET IL trimming to reduce app size #54

Merged
merged 5 commits into from
Mar 19, 2023

Conversation

JasonWei512
Copy link
Owner

@JasonWei512 JasonWei512 commented Mar 15, 2023

Solve #14 .

Currently the TrimMode is set to partial, since full is too aggressive.
Need some time to test and make sure it doesn't break app's functionality.

Size comparision

App size is reduced by 50%.

Before (MB) After (MB)
Unpackaged 146 65
MSIX 57 29

Note

TaskScheduler is removed from dependencies because it uses COM to interact with Windows Task Scheduler and is incompatible with IL trimming.
Instead I use schtasks get the status of the admin startup schedule task.

@JasonWei512
Copy link
Owner Author

Error when clicking "Contact the developer" button in settings page:

2023/03/15 21:13:42.991 | ERROR | Unhandled exception|System.TypeInitializationException: The type initializer for 'System.Management.WmiNetUtilsHelper' threw an exception.
 ---> System.TypeLoadException: Could not load type 'System.StubHelpers.InterfaceMarshaler' from assembly 'System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e'.
   at System.Runtime.InteropServices.Marshal.GetDelegateForFunctionPointerInternal(IntPtr, Type)
   at System.Runtime.InteropServices.Marshal.GetDelegateForFunctionPointer[TDelegate](IntPtr)
   at System.Management.WmiNetUtilsHelper.LoadDelegate[TDelegate](TDelegate&, IntPtr, String)
   at System.Management.WmiNetUtilsHelper..cctor()
   --- End of inner exception stack trace ---
   at System.Management.MTAHelper.IsNoContextMTA()
   at System.Management.MTAHelper.CreateInMTA(Type)
   at System.Management.ManagementPath.CreateWbemPath(String)
   at System.Management.ManagementObjectSearcher..ctor(String, String, EnumerationOptions)
   at Hardware.Info.Windows.HardwareInfoRetrieval.GetOs()
   at Hardware.Info.HardwareInfo..ctor(Boolean useAsteriskInWMI, Nullable`1 timeoutInWMI)
   at EnergyStarX.ViewModels.SettingsViewModel.<>c.<.ctor>b__40_0()
   at System.Lazy`1.ViaFactory(LazyThreadSafetyMode)
   at System.Lazy`1.ExecutionAndPublication(LazyHelper, Boolean)
   at System.Lazy`1.CreateValue()
   at System.Threading.Tasks.Task`1.InnerInvoke()
   at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread, ExecutionContext, ContextCallback, Object)
--- End of stack trace from previous location ---
   at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread, ExecutionContext, ContextCallback, Object)
   at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task&, Thread )
--- End of stack trace from previous location ---
   at EnergyStarX.ViewModels.SettingsViewModel.ContactTheDeveloper()
   at CommunityToolkit.Mvvm.Input.AsyncRelayCommand.AwaitAndThrowIfFailed(Task)
   at System.Threading.Tasks.Task.<>c.<ThrowAsync>b__128_0(Object)
   at Microsoft.UI.Dispatching.DispatcherQueueSynchronizationContext.<>c__DisplayClass2_0.<Post>b__0()
--- End of stack trace from previous location ---
   at WinRT.ExceptionHelpers.<ThrowExceptionForHR>g__Throw|20_0(Int32)
   at ABI.Windows.ApplicationModel.Core.IUnhandledErrorMethods.Propagate(IObjectReference)
   at Microsoft.AppCenter.Utils.ApplicationLifecycleHelperWinUI.<.ctor>b__0_3(Object sender, UnhandledErrorDetectedEventArgs eventArgs)

@JasonWei512
Copy link
Owner Author

JasonWei512 commented Mar 15, 2023

Hardware.Info uses System.Management, which is incompatible with trimming.
Related: dotnet/runtime#61960

@JasonWei512
Copy link
Owner Author

Replaced Hardware.Info with WmiLight for fetching hardware info.

@JasonWei512
Copy link
Owner Author

JasonWei512 commented Mar 17, 2023

Visual Studio App Center SDK uses Newtonsoft.Json, which is not trimming friendly.
Not sure if telemetry will be affected.

@JasonWei512 JasonWei512 force-pushed the optimize/IL-trimming branch from 0801221 to 486b80e Compare March 18, 2023 03:31
@JasonWei512 JasonWei512 merged commit 050fe83 into develop Mar 19, 2023
@JasonWei512
Copy link
Owner Author

Reverted because it breaks App Center telemetry.
#14 (comment)

@JasonWei512 JasonWei512 deleted the optimize/IL-trimming branch April 16, 2023 02:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant