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

InvalidProgramException: locals size too big #1016

Closed
ppereira-serviceonsites opened this issue Jan 24, 2022 · 3 comments · Fixed by #1018
Closed

InvalidProgramException: locals size too big #1016

ppereira-serviceonsites opened this issue Jan 24, 2022 · 3 comments · Fixed by #1018

Comments

@ppereira-serviceonsites

I am trying to use the UnitsNet library in my .NET 6 Blazor WebAssembly project. Included it through Nuget package V4.113.0

It works great except for the UnitConverter. Whenever I try one of the examples:
double d1 = UnitConverter.Convert(1, LengthUnit.Centimeter, LengthUnit.Millimeter); // 10 mm
double d2 = UnitConverter.ConvertByName(1, "Length", "Centimeter", "Millimeter"); // 10 mm
double d3 = UnitConverter.ConvertByAbbreviation(1, "Length", "cm", "mm"); // 10 mm

I get the following error:
blazor.webassembly.js:1 crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
Unhandled exception rendering component: The type initializer for 'UnitsNet.UnitConverter' threw an exception.
System.TypeInitializationException: The type initializer for 'UnitsNet.UnitConverter' threw an exception.
---> System.InvalidProgramException: Unable to run method 'void UnitsNet.UnitConverter:RegisterDefaultConversions (UnitsNet.UnitConverter)': locals size too big.
at UnitsNet.UnitConverter..cctor()
--- End of inner exception stack trace ---

Has anyone else had this issue?

@angularsen
Copy link
Owner

I have no experience with Units.NET in Blazor WebAssembly, but my hunch is that either AOT strips it out, or webassembly stumbles on our large number of types and methods/properties in the library. IL2CPP in Unity3D apparently had similar issues.

#1009

Since it seems to crash on UnitConverter ctor, which is default, I suspect it is really having trouble with the static ctor:
https://github.com/angularsen/UnitsNet/blob/master/UnitsNet/UnitConverter.cs#L45-L49

We load tons of things there. Maybe it is too slow? Maybe it uses too much memory?

The only relevant hit on Google was this, which seems to deal with large arrays. We have large dictionaries. Nothing crazy large, but definitely large.
dotnet/runtime#46622

@angularsen
Copy link
Owner

If you are able to debug with source code, it would be helpful to try to comment out the static ctor or reduce the amount of data it loads to see if it helps.

@tmilnthorp
Copy link
Collaborator

The only thing I can think of is just the sheer size of this method. I created PR #1018 to see if this would help. Unfortunately I have no way to test if this fixes it or not, so please give it a go.

angularsen pushed a commit that referenced this issue Jan 26, 2022
Ref #1016 

Instead of a huge single method in UnitConverter, it is split up into a static method per quanitity type.
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 a pull request may close this issue.

3 participants