Live data from Hacker News

Performance Improvements in .NET 7

devblogs.microsoft.com

151–160 of 164 posts

Re: Performance Improvements in .NET 7

#151
post #146

Earlier quoted context omitted.

app.MapPut("ping/pong/yolo", ([FromBody] Payload request) => {...}); record Payload(Foo Foo); record Foo(string Bar); or app.MapPut("ping/pong/yolo", (JsonElement json) => {...}); Basically, just avoid outdated blog posts and articles :D

this looks like asp.net core

No one in their sane mind would use ASP.NET for new projects (it is legacy and a business risk) nowadays so it's reasonable to assume that everyone means ASP.NET Core given the .NET 7 context.

Re: Performance Improvements in .NET 7

#153

Earlier quoted context omitted.

Is this what you're talking about? > "Arguably the biggest improvement around UTF8 in .NET 7 is the new C# 11 support for UTF8 literals." > "UTF8 literals enables the compiler to perform the UTF8 encoding into bytes at compile-time. Rather than writing a normal string, e.g. "hello", a developer simply appends the new u8 suffix onto the string literal, e.g. "hello"u8. At that point, this is no longer a string. Rather,…

No, not unless you can pass a ReadOnlySpan into every API that expects a String. The change you referenced let's folks work around the fact that String is UTF16. It doesn't transparently handle ASCII with one byte like other languages. See the GitHub issue or https://openjdk.org/jeps/254 The .NET 7 change doesn't retain backwards compatibility

Doing magic with the internal string format was considered by the .net team and rejected.

There are multiple operations that right now are O(1) no-allocation, that would become sometimes O(n) allocating if they did that sort of magic.

This includes: PINVOKEing windows APIs that are expecting WCHARS, converting a string to a ReadOnlySpan, and using `unsafe` to pin a string and access its contents as a `char*`.

Making code that users may have been relying on being O(1) and non-allocating into possibly O(n) and allocating was deemed too disruptive.

Plus because strings store their contents within themselves, the on-demand conversion of a string to UTF-16 would require allocating a new object, and updating all pointers to the old object to refer to the new one. Which currently is an operation only done by the garbage collector. If expanding the string on-demand required running a full garbage collection to handle this... yeah, going from O(1) non-allocating, to o(n)+Full Garbage Collection is a total non-starter.

I think Java on the other hand did not expose very many places where a string could be observed to be a utf-16 character array under the hood (perhaps only as part of JNI marshaling?) making the ascii-only string optimization more feasible. Javascript never made the strings internal encoding visible, simply requiring that the indexer be able to return a UTF-16 value in O(1), so the ASCII optimization is simple there.

Re: Performance Improvements in .NET 7

#154

Earlier quoted context omitted.

I've taken to calling it "Legacy .NET".

Or my favourite "full framework"

Nuh-uh! I had to fight that term at Microsoft for a few years because it was obvious that .NET Framework was legacy but some people were afraid to say it for fear that some customers would leave (as if leaving is somehow easy to do).

Re: Performance Improvements in .NET 7

#155

Earlier quoted context omitted.

A polite way to describe most serialization tooling in the .NET ecosystem would be "bad". It's unfortunate, but it's good to see improvements happening. I still try to avoid JSON in general on .NET since I've had so many bad experiences with the various JSON libraries out there. Sadly if you want to ship well-performing software in .NET it's still mandatory to run it under a profiler on a regular basis, both to spot…

Most pitfalls in .NET are luckily along the lines of "punishing people who redo all the steps on each method call or write code Java style" which might even be a good thing. I wonder where does your bad experience with Json libraries come from? Even back in the days of .NET Framework, there was a variety of quality libraries such as Newtonsoft.JSON, Utf8Json or other more interesting but less feature-rich packages. N…

I had a terrible experience with Newtonsoft every time I used it:

* compatibility-breaking changes happening without proper versioning, which meant if someone happened to install their copy in the GAC my app would break. I had to start shipping custom builds of Newtonsoft with the version 9.9.9.9 to stop the GAC from breaking me and I never had this problem with any other managed library

* Bad performance

* The necessity to do awkward things to solve basic serialization problems, like write a custom contract resolver just so that it wouldn't try to write to read-only properties

The standard JSON libraries in the BCL at the time were much worse. The new STJ looks alright but after so many bad experiences I can't bring myself to trust it.

Re: Performance Improvements in .NET 7

#156
post #55

Earlier quoted context omitted.

Actually system text json does work with source generators and di can work with source Generators aswell.

Thanks for telling me :). On a serious note though, anything can work with source generators but it doesn't match the style of coding that we'd like (moving everything to be declarative isn't the path we want to go down for certain APIs). Also source generators don't compose, so any source generation that we would want to use would need to take advantage of the JSON source generator (if we wanted to keep things Nativ…

Well in system text json the api keeps the same and you „only“ need to pass an autogenerated meta object its basically the same api you just need to pass another object instead of an generic Parameter. But yeah its a change.

Re: Performance Improvements in .NET 7

#157

Earlier quoted context omitted.

Most pitfalls in .NET are luckily along the lines of "punishing people who redo all the steps on each method call or write code Java style" which might even be a good thing. I wonder where does your bad experience with Json libraries come from? Even back in the days of .NET Framework, there was a variety of quality libraries such as Newtonsoft.JSON, Utf8Json or other more interesting but less feature-rich packages. N…

I had a terrible experience with Newtonsoft every time I used it: * compatibility-breaking changes happening without proper versioning, which meant if someone happened to install their copy in the GAC my app would break. I had to start shipping custom builds of Newtonsoft with the version 9.9.9.9 to stop the GAC from breaking me and I never had this problem with any other managed library * Bad performance * The neces…

For read-only properties/fields, the useful pattern for Newtonsoft is to just provide a parametrized constructor. There are multiple ways to implements this and all of them are well documented.

When it comes to performance - are you sure your code doesn't do anything that pessimizes it? Regular JsonConvert.Serialize/.Deserialize work well and are reasonably fast. Obviously, if you use a lot of custom contract revolvers with heavy uncached reflection and large allocations, it will destroy the performance but almost no library can fix bad user-provided implementations, best case is to design API in a way that will nudge towards efficient patterns but that's it.

Keep in mind that assumptions, especially brought from different languages, may cause a user to unnecessarily shoot themselves in the foot, especially when they are solved by spending 15 minutes to go over suggested usage patterns in the documentation.

In addition, I think it is counter-productive to judge a completely unrelated Json library in BCL by your experience on a limited scenario with Newtonsoft where its defaults were simply different from expectations. The criticism listed is applicable to software that was released about a decade ago, on a platform with features (GAC) that have long become legacy. (e.g. GAC is a very old feature, .netfw 4.6.2 was released 8 years ago, Newtonsoft 9.* - 6 years ago, etc.)

I'm not saying all libraries are perfect, but experimenting with implementations in Rust, Java and Swift left me with impression that C# packages are solid and relatively easy to use even not the most popular ones, and DOM-like JObject/JToken were quite nice to use too for untyped payloads.

Last but not least, during the days of .NET Framework, the fastest implementation was Utf8Json losing only to optimized binary serializers which had to perform less work, it was nowhere near as popular or as feature-rich as Newtonsoft but in performance-sensitive scenarios it was very nice due to pooling internal buffers, emitting efficient IL stubs for serialization and using many other tricks to reduce overhead as much as possible.

Anyway, Utf8Json is sadly deprecated but STJ provides very good performance OOB and if you need even more, SpanJson and SimdJson can give you even better numbers, enjoying newer perf-focused features of the language and research done on JSON serialization. .NET today is much, much better and very different from .NET Framework of 10 years ago. Saying otherwise is not much different behavior from the people who keep yelling "msft bad, closed source windows only garbage!" for whatever reason.

Re: Performance Improvements in .NET 7

#158
post #146

Earlier quoted context omitted.

this looks like asp.net core

No one in their sane mind would use ASP.NET for new projects (it is legacy and a business risk) nowadays so it's reasonable to assume that everyone means ASP.NET Core given the .NET 7 context.

the context was asp.net mvc specifically

Re: Performance Improvements in .NET 7

#159
post #158

Earlier quoted context omitted.

No one in their sane mind would use ASP.NET for new projects (it is legacy and a business risk) nowadays so it's reasonable to assume that everyone means ASP.NET Core given the .NET 7 context.

the context was asp.net mvc specifically

ASP.NET MVC is dead. It has been over 6 years; you need to update yourself.

Re: Performance Improvements in .NET 7

#160

Earlier quoted context omitted.

Or my favourite "full framework"

Nuh-uh! I had to fight that term at Microsoft for a few years because it was obvious that .NET Framework was legacy but some people were afraid to say it for fear that some customers would leave (as if leaving is somehow easy to do).

I was ironically saying it was my favorite. It's the worst because it implied the "Core" in use at the time was somehow a "lite" version.
Post reply on HN