KEMBAR78
[.NET 6]A derived Random class still allocates possibly unnecessary memory · Issue #60549 · dotnet/runtime · GitHub
Skip to content

[.NET 6]A derived Random class still allocates possibly unnecessary memory #60549

@koszeggy

Description

@koszeggy

This is a follow up after fixing #57272.

Thanks to pull request #57530 a new Net5CompatDerivedImpl class has been introduced so it can be specialized for derived Random implementations.

Now I could test the latest release and unfortunately this implementation still initializes the _seedArray even if no base member will be called ever. Now that the possible scenarios have been separated the new Net5CompatDerivedImpl could initialize the _seedArray only on demand, when it is actually needed for the first time. I mentioned it in #57272 but the original reason of that issue was different. So now I created this one.

Of course, this would mean a minimal overhead but only when a derived instance calls some base implementation. In return the construction of the derived instance will be much faster and less memory consuming.


Update: API Proposal

To preserve the performance of the current behavior and fix the mentioned issue I propose the following new API

public class Random
{
    // Determines the behavior of the base members for a derived Random class
    // when using the new protected constructor.
    protected enum BaseBehavior
    {
        // Base members use the original Random implementation.
        // Instantiation generates a seed that allocates an array consuming 224 bytes
        Compatible,

        // Base members use the xoshiro** algorithm.
        // Instantiation generates a seed consuming up to 32 bytes (on a 64 bit system)
        // Can be useful if a derived class overrides some members and the rest should work
        // the same way as the static Shared instance
        Xoshiro,

        // Base members rely on the protected Sample method
        // Instantiation does not generate any seed in the base Random class
        // Can be useful if a derived class overrides all members and does not call any base methods.
        // Can be useful also when you want to override only the Sample method to provide
        // a fully functional new Random implementation. This is useful also for tests that should
        // produce prepared results as you don't need to override anything but the Sample method.
        SampleBased
    }

    // protected constructor for derived classes
    protected Random(BaseBehavior baseBehavior)
    {
        _impl = baseBehavior switch
        {
            BaseBehavior.Compatible => new Net5CompatDerivedImpl(this),
            BaseBehavior.Xoshiro => new XoshiroImpl(),
            BaseBehavior.SampleBased => new SampleBasedImpl(this), // TODO: new implementation
            _ => throw new ArgumentOutOfRangeException(nameof(baseBehavior))
        };
    }
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions