Last month, Microsoft released the fifth preview of .NET 9. The new version of the framework, expected to be released later this year, is a Standard Term Support (STS) release and will be supported on multiple operating systems for 18 months, from November 12th, 2024 to May 12th, 2026. This preview brings performance improvements and features such as enhanced AI capabilities, prioritized unbounded channel, substring searching with SearchValues
, and more flexible active linking in OpenTelemetry.
One of the most important features of this release is the expansion of its AI capabilities with updated versions of TensorPrimitives
and the Tensor<T>
type. The TensorPrimitive
class provides static methods for performing numerical operations on spans of values. With the update, the scope of methods exposed by the class has been significantly expanded - from 40 (.NET 8) to approximately 200 overloads, The expansion includes overloads for any T that implements a certain interface. In terms of performance,many operations have been accelerated via SIMD-optimized implementations.
The new Tensor<T>
type, introduced in the previous release, aims to provide efficient interop with AI libraries (like ML.NET). It also enables data manipulation by providing indexing and slicing operations, and since it builds on top of TensorPrimitives
for math operations the new type also benefits from the TensorPrimitives
updates.
Another interesting update in this release is the prioritized unbounded channel. An unbounded channel refers to a thread channel that has no limit on the number of items that may be stored. The new prioritized channel is provided with the addition of the method CreateUnboundedPrioritized<T>
to the System.Threading.Channels
library. The channel created by the new method orders its elements according to either Comparer<T>.Default
or a custom IComparer<T>
supplied to the factory method. The following example outputs the numbers 1 through 5 in order, even though they were written to the channel in a different order:
using System.Threading.Channels;
Channel<int> c = Channel.CreateUnboundedPrioritized<int>();
await c.Writer.WriteAsync(1);
await c.Writer.WriteAsync(5);
await c.Writer.WriteAsync(2);
await c.Writer.WriteAsync(4);
await c.Writer.WriteAsync(3);
c.Writer.Complete();
while (await c.Reader.WaitToReadAsync())
{
while (c.Reader.TryRead(out int item))
{
Console.WriteLine(item);
}
}
Source: Microsoft
The SearchValues
type, introduced in .NET 8, provides an optimized solution for searching within spans. In .Net 9, the type has been extended to support searching for substrings within a larger string. This is another implementation optimized to take advantage of the SIMD support in the underlying platform. As a result, higher-level types that use this functionality as part of its implementation (like Regex
) are also optimized.
OpenTelemetry activity linking is now more flexible with the addition of the System.Diagnostics.Activity.AddLink
method. It enables linking an Activity
object to other tracing contexts after the creation of the Activity
object, which better aligns with the OpenTelemetry specifications.
Other updates in this release include new APIs for working with Task
- in particular, the new Task.WhenEach
method, which allows for using an await foreach
to iterate through tasks as they complete. It also includes trimming support for the TypeDescriptor
class, a new TypeName
class for parsing ECMA-335 type names, and better params
performance with Span
overloads.
You can find more information about this release here. .NET 9 Preview 5 is available for Linux, macOS, and Windows.