Skip to content

Optimize Array.CreateFille when value is null - #136695

Merged
jkotas merged 1 commit into
dotnet:mainfrom
lilinus:opt-Array.CreateFilled
Oct 30, 2026
Merged

jkotas merged 1 commit into
dotnet:mainfrom
lilinus:opt-Array.CreateFilled

Conversation

@lilinus

@lilinus lilinus commented Oct 30, 2026

Copy link
Copy Markdown
Contributor

Also added some test cases for Array.CreateFilled<int?> and when valueis default(T).

Benchmarks:

| Method              | Categories      | N     | Mean      | Error     | StdDev    | Ratio |
|-------------------- |---------------- |------ |----------:|----------:|----------:|------:|
| NullableWrapperMain | NullableWrapper | 10000 | 43.380 us | 0.1265 us | 0.1121 us |  1.00 |
| NullableWrapperPr   | NullableWrapper | 10000 |  6.480 us | 0.0452 us | 0.0423 us |  0.15 |
|                     |                 |       |           |           |           |       |
| StringMain          | string          | 10000 |  3.205 us | 0.0281 us | 0.0263 us |  1.00 |
| StringPr            | string          | 10000 |  1.789 us | 0.0122 us | 0.0114 us |  0.56 |
Benchmark source
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Running;

BenchmarkRunner.Run<Bench>();

[GroupBenchmarksBy(BenchmarkLogicalGroupRule.ByCategory), CategoriesColumn]
public class Bench
{
    [Params(10000)]
    public int N;

    private static T[] CreateFilledMain<T>(int length, T value)
    {
        ArgumentOutOfRangeException.ThrowIfNegative(length);

        T[] array = GC.AllocateUninitializedArray<T>(length);
        Array.Fill(array, value);
        return array;
    }

    private static T[] CreateFilledPr<T>(int length, T value)
    {
        ArgumentOutOfRangeException.ThrowIfNegative(length);

        if (value is null)
        {
            return new T[length];
        }

        T[] array = GC.AllocateUninitializedArray<T>(length);
        Array.Fill(array, value);
        return array;
    }

    [BenchmarkCategory("string"), Benchmark(Baseline = true)]
    public string?[] StringMain() => CreateFilledMain(N, default(string));
    [BenchmarkCategory("string"), Benchmark]
    public string?[] StringPr() => CreateFilledPr(N, default(string));

    [BenchmarkCategory("NullableWrapper"), Benchmark(Baseline = true)]
    public Wrapper?[] NullableWrapperMain() => CreateFilledMain<Wrapper?>(N, null);
    [BenchmarkCategory("NullableWrapper"), Benchmark]
    public Wrapper?[] NullableWrapperPr() => CreateFilledPr<Wrapper?>(N, null);
}

public record struct Wrapper(string Value);

@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Oct 30, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
13 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/area-system-runtime
See info in area-owners.md if you want to be subscribed.

@jkotas jkotas left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

@jkotas
jkotas merged commit 3eb929b into dotnet:main Oct 30, 2026
144 of 146 checks passed
@MichalPetryka

Copy link
Copy Markdown
Contributor

when valueis default(T).

I don't see such check here? Why do we even check for null and not default?

@jkotas

jkotas commented Oct 30, 2026

Copy link
Copy Markdown
Member

There is no good way to check for default

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-System.Runtime community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants