Mono does it again (Allocation)

I’m glad the foreach allocation problem was fixed, we still have the boxing problem with enums as generic argument. But I saw a new funny allocation bug on reddit

They point to the problem in the mono code, this is how mono 2 converts from float to decimal :smile:

Decimal d = Decimal.Parse (value.ToString (CultureInfo.InvariantCulture),
                    NumberStyles.Float, CultureInfo.InvariantCulture);
1 Like

When was the foreach allocation problem fixed, out of curiosity? I’ve entirely eliminated its usage in lieu of for loops, and I’d be interested in knowing when I can start using them again, assuming I’ve got the right patch or version.

It was done in 5.5, only the compiler not the CLR, but that was a compiler issue

1 Like

I never had to use the Decimal type in any game. Do you use it in a game and for what and why?

It was from a reddit post, they had a in game calculator

It’s odd that they choose decimal when your average operating system calculator just uses a double. That said if I had to track values greater than what double could handle I believe I’d choose a big math library before I choose decimal.

I think it was in currency calculator, let me see if I can find the reddit again. Anyway, on the .NET version strings are not used to convert floats to decimals… :smile:

Found it "Today I was profiling my economic simulator and I noticed that float-decimal conversion allocates about 160 bytes. ".

https://www.reddit.com/r/Unity3D/comments/8bsnxe/float_to_decimal_conversion_without_string_parsing/

edit: it was not a calculator but a simulator

Just spent some time digging around in code and discovered that Mono has been using Microsoft’s implementation for a couple years now. Switching their project to .NET 4.6 in Unity would likely eliminate the garbage altogether.

1 Like

Yepp, its coming to stable any year now :smile:

I wonder if the native code is the same though as the .NET equivalent

Found another strange one, Dictionary.Values allocates in 3.5

Source for 3.4 (Didnt find a 3.5 branch) (Unity 2017)

public ValueCollection Values {
            get { return new ValueCollection (this); }
        }

vs mono 4.5 (Unity 2018)

        public ValueCollection Values {
            get {
                Contract.Ensures(Contract.Result<ValueCollection>() != null);
                if (values == null) values = new ValueCollection(this);
                return values;
            }
        }

:0 – lol

1 Like