Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vector<T>(Rengine engine, T[] vector) constructor waste memory and copy data unprotected #136

Open
hrogithub opened this issue Dec 15, 2020 · 0 comments

Comments

@hrogithub
Copy link

Why this constructor stuff was implemented in this matter?

First: this is OK, but expensive, both .Count() and .ToArray() do copy twice ore once, if vector not inherited from ICollection
///


/// Creates a new vector with the specified values.
///

/// The handling this instance.
/// The element type.
/// The elements of vector.
protected Vector(REngine engine, SymbolicExpressionType type, IEnumerable vector)
: base(engine, engine.GetFunction<Rf_allocVector>()(type, vector.Count()))
{
SetVector(vector.ToArray());
}

But here: the base constructor is called and this creates an empty double array of length and why the SetVector is not used? instead the array is copied unprotected? (see next protected base constructor)
///


/// Creates a new NumericVector with the specified values.
///

/// The handling this instance.
/// The values.
///
public NumericVector(REngine engine, double[] vector)
: base(engine, SymbolicExpressionType.NumericVector, vector.Length)
{
Marshal.Copy(vector, 0, DataPointer, vector.Length);
}

    /// <summary>
    /// Creates a new vector with the specified size.
    /// </summary>
    /// <param name="engine">The <see cref="REngine"/> handling this instance.</param>
    /// <param name="type">The element type.</param>
    /// <param name="length">The length of vector.</param>
    protected Vector(REngine engine, SymbolicExpressionType type, int length)
        : base(engine, engine.GetFunction<Rf_allocVector>()(type, length))
    {
        if (length <= 0)
        {
            throw new ArgumentOutOfRangeException("length");
        }
        var empty = new byte[length * DataSize];
        Marshal.Copy(empty, 0, DataPointer, empty.Length);
    }

Same "problem" for all Vector subclasses

Roman

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant