News

.NET 7 Preview 5 Boosts Generic Math, Observability, More

Microsoft shipped the fifth preview of .NET 7 en route to a November debut, with improvements concerning generic math, observability and much more.

Generic Math is so important it warranted its own blog posts last year as a preview feature and last week. It helps .NET developers take advantage of static APIs, including operators, from within their generic code.

Or, as last week's post says: "Generic Math combines the power of generics and a new feature known as static virtuals in interfaces to allow .NET developers to take advantage of static APIs, including operators, from generic code. This means that you get all the power of generics, but now with the ability to constrain the input to number like types, so you no longer need to write or maintain many near identical implementations just to support multiple types. It also means that you get access to all your favorite operators and can use them from generic contexts. That is, you can now have static T Add<T>(T left, T right) where T : INumber<T> => left + right; whereas previously it would have been impossible to define."

Generic Math especially benefits API authors, helping them to simplify their codebase. Other developers will also benefit, though more indirectly, because consumed APIs will start to support more types without requiring all numeric types to be explicitly supported.

"In .NET 7 we have made improvements to the implementation and responded to feedback from the community," said Angelos Petropoulos, product manager for the .NET team, in a June 14 announcement post. He pointed to last week's post (linked above) for more information.

The goal of observability, meanwhile, is to help developers better understand the state of their applications as scale and technical complexity increases, said Petropoulos. The preceding link points to a 2021 post about OpenTelemetry .NET reaching version 1.0. OpenTelemetry is described as "a collection of tools, APIs, and SDKs. Use it to instrument, generate, collect, and export telemetry data (metrics, logs, and traces) to help you analyze your software's performance and behavior." It encompasses:

  • A vendor-neutral specification for observability telemetry (distributed tracing, metrics, etc.)
  • API packages that implement the public interfaces used for instrumentation
  • SDKs used by applications to configure instrumentation and interfaces for plugin authors to write exporters
  • Exporters that enable you to send your data to the telemetry backend of your choice

The improvement in .NET 7 Preview 5 is to expose performant ActivityEvent and ActivityLink tags enumerator methods, which is explained here. The goal is to eliminate the need for reflection or casting and improve performance when accessing tags associated with an event or link.

"The exposed methods can be used in performance critical scenarios to enumerate the Tag objects without any extra allocations and with fast items access," Petropoulos said.

Regular expression functionality has also been improved, specifically concerning a new RegexGenerator source generator, which helps developers to statically generate regular expressions at compile time, thus improving performance, as explained in this post from last month. To help, in Preview 5 the team added a Roslyn analyzer and fixer:

  • Analyzer: This will search for uses of Regex that could instead be converted to use the RegexGenerator source generator. The analyzer can detect uses of the Regex constructors and Regex static methods that meet the following criteria:
    • Parameters supplied have a known value at compile time. The source generator's output depends on these values, so they must be known at compile time.
    • They are part of an app that targets .NET 7. The new analyzer ships inside the .NET 7 targeting pack and only apps targeting .NET 7 are eligible for this analyzer.
    • The LangVersion (learn more) is higher than 10. For the time being the regex source generator requires LangVersion to be set to preview.
    The New Analyzer in Action in Visual Studio
    [Click on image for larger view.] The New Analyzer in Action in Visual Studio (source: Microsoft).
  • Code fixer: This suggests a RegexGenerator source generator method and gives developers the option to override the default name, after which it replaces the original code with a call to the new method.
    The New Code Fixer in Action in Visual Studio
    [Click on image for larger view.] The New Code Fixer in Action in Visual Studio (source: Microsoft).

Other highlights of Preview 5 include:

  • System.Text.Json: This now supports serializing and deserializing polymorphic type hierarchies using attribute annotations.
  • ML.NET Text Classification API: This is the process of applying labels or categories to text in machine learning projects. "We are excited to introduce the ML.NET Text Classification API, an API that makes it easier for you to train custom text classification models and brings the latest state-of-the-art deep learning techniques for natural language processing to ML.NET," Petropoulos said.
  • System.Reflection performance improvements when invoking members: The overhead of using reflection to invoke a member (whether a method, constructor or a property gettersetter) has been substantially reduced when the invoke is done several times on the same member. The team achieved typical 3-4x faster performance gains by reducing the overhead of using reflection to invoke a member, such as a method, constructor or a property getter/setter.
  • System.IO.Stream: The team added new methods to the base System.IO.Stream class -- ReadExactly and ReadAtLeast -- to address common mistakes when using Stream.Read(). The specific problem is that Read() may return less data than what is available in the Stream and less data than the passed-in buffer. "And even for programmers who are aware of this, having to write the same loop every single time they want to read from a Stream is annoying," Petropoulos said.

All of the above and much more is explained in greater detail in the announcement post, along with a bunch of other fixes, optimizations and other tweaks.

About the Author

David Ramel is an editor and writer at Converge 360.

comments powered by Disqus

Featured

Subscribe on YouTube