Closed Sergio0694 closed 1 year ago
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis See info in area-owners.md if you want to be subscribed.
Author: | Sergio0694 |
---|---|
Assignees: | - |
Labels: | `area-System.Text.Json`, `untriaged` |
Milestone: | - |
Thinking about this more, and since @tannergooding mentioned that the previous use of Unsafe.As<TFrom, TTo>
had some concerns around additional generic instantiations, could we not just stop using pointers altogether and just box/unbox? The JIT/AOT compilers should recognize things anyway and elide the boxing anyway. That is, in both directions:
// Read
if (reader.TryGetInt32(out int int32))
{
return (T)(object)int32;
}
// Write
writer.WriteNumberValue((int)(object)value);
This would both solve the issue on .NET Native and also potentially gives even better codegen due to no address taken?
(TEnum)(object)integer
pattern won't work for .NET Framework and likely won't for Mono too, e.g.
CoreCLR learned to fold it fairly recently (in net7)
"won't work for .NET Framework"
Just to clarify, do you mean it'll work slowly, and not that it won't work at all? Because ai tried this in the NETFX C# interactive window and it seems to work fine on NETFX too.
If performance on NETFX and Mono is an issue, it seems we could:
Unsafe.As
use assuming that's fine with respect to additional generics.#ifdef
-s to keep raw pointers on .NET 8+, and revert only on downlevel targets? If it's just this couple methods it seems manageable?it'll work slowly
Yes, it boxes (allocates)
it's an intrinsic it wouldn't really have any performance impacts on .NET 8
The generic intrinsic references contribute to static footprint that is also a performance metric. Reducing the static footprint was my motivation for the change.
Revert the Unsafe.As use assuming that's fine with respect to additional generics
I think it is fine to revert the change for System.Text.Json. System.Text.Json EnumConverter has large static footprint (e.g. it instantiates ConcurrentDictionary<string, T>
- that is 10's kB of static footprint right there for each serialized enum type). A few extra generic instantiations from Unsafe.As
are not a big deal.
"The generic intrinsic references contribute to static footprint that is also a performance metric. Reducing the static footprint was my motivation for the change."
Yup, no that makes perfect sense. I should've clarified, I only meant to revert those changes in EnumConverter<T>
specifically, I do understand the motivation for the change in all other places in general to reduce generic instantiations 🙂
Was about to say feel free to assign me to this but I see you already have a PR up ahah awesome! 😄
Description
I'm dogfooding .NET 8 builds of System.Text.Json in the Microsoft Store, now that we've fully migrated to it, and noticed that since the .NET 8 Preview 1 release, .NET Native has starting failing to build. We've investigated this and @AntonLapounov managed to narrow this down to some uses of unconstrained raw pointers, which is a known issue with .NET Native.
Specifically, the build failure is caused by some changes in 4deccc02588990626180eaa812e63603d6c2790e:
https://github.com/dotnet/runtime/blob/8cb3bf89e4b28b66bf3b4e2957fd015bf925a787/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/EnumConverter.cs#L135-L182
https://github.com/dotnet/runtime/blob/8cb3bf89e4b28b66bf3b4e2957fd015bf925a787/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/EnumConverter.cs#L236-L262
This is a complete blocker for us, as we cannot update to System.Text.Json 8.0 at all.
Could we revert these changes in
EnumConverter<T>
and just keep usingUnsafe.As<TFrom, TTo>
? Since it's an intrinsic it wouldn't really have any performance impacts on .NET 8, and it'd at least fix the compile issues on .NET Native we're hitting.cc. @eiriktsarpalis, also could we mark this as partner impact? We're completely stuck on 7.0 as of right now 🥲
Reproduction Steps
Compile in Release.
Expected behavior
The app should build correctly.
Actual behavior
The build fails with an error like this:
Specifically, the real issue is here:
Regression?
Yes, it works just fine with System.Text.Json 7.0.2.