www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 3383] New: newVoid

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3383

           Summary: newVoid
           Product: D
           Version: 2.033
          Platform: Other
        OS/Version: Windows
            Status: NEW
          Keywords: patch, performance
          Severity: enhancement
          Priority: P2
         Component: Phobos
        AssignedTo: nobody puremagic.com
        ReportedBy: dsimcha yahoo.com



D's new keyword for allocating dynamic arrays initializes the data to T.init. 
This is a perfectly reasonable safe default.  However, there should be an
obvious way to optimize this out if one is sure one doesn't need it, as there
is for static arrays.  Below is a proposed function, newVoid(), that should go
in std.array to allow such a thing.

import core.memory;

/**Gives the block attribute that a block containing type T should have,
 * i.e. scan or NO_SCAN.*/
GC.BlkAttr blockAttribute(T)() {
    if(typeid(T).flags & 1) {
        return cast(GC.BlkAttr) 0;
    } else {
        return GC.BlkAttr.NO_SCAN;
    }
}

unittest {
    assert(blockAttribute!(uint)() == GC.BlkAttr.NO_SCAN);
    assert(blockAttribute!(void*)() == cast(GC.BlkAttr) 0);
}

/**Returns a new array of type T w/o initializing elements.
 *
 * Examples:
 * ---
 * auto foo = newVoid!uint(5);
 * foreach(i; 0..5) {
 *     foo[i] = i;
 * }
 * ---
 */
T[] newVoid(T)(size_t length) {
    T* ptr = cast(T*) GC.malloc(length * T.sizeof, blockAttribute!(T)());
    return ptr[0..length];
}

unittest {
    // Mostly just see if this instantiates.
    auto foo = newVoid!uint(5);
    foreach(i; 0..5) {
        foo[i] = i;
    }

    foreach(i; 0..5) {
        assert(foo[i] == i);
    }
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 09 2009
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3383


Andrei Alexandrescu <andrei metalanguage.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED
                 CC|                            |andrei metalanguage.com
         AssignedTo|nobody puremagic.com        |andrei metalanguage.com


-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 11 2009
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3383


Andrei Alexandrescu <andrei metalanguage.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         AssignedTo|andrei metalanguage.com     |dsimcha yahoo.com



08:18:18 PDT ---
Reassigning this to David. David, this is a sensible primitive. You may want to
make it into a pull request, probably in std.array or even in druntime. A few
notes:

1. newVoid is not very indicative, I'd suggest something long and
obvious-looking (as this can be an unsafe function), e.g.
makeUninitializedArray!int(100).

2. Since 0 seems to be an actually used constant of GC.BlkAttr, you may as well
give it a name, e.g. NONE.

Thanks!

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 05 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3383


bearophile_hugs eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bearophile_hugs eml.cc



Nicer and cleaner:

// doesn't initialize foo1
auto foo1 = new uint[5] = void;

// initializes the dynamic array to 5, avoiding a double initialization
auto foo2 = new uint[5] = 5;

That syntax is inspired by static array syntax:
uint[5] a1 = void;
uint[5] a2 = 5;

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 05 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3383




One case worth considering:

auto mat = new int[5][5];
foreach (row; mat)
    row[] = 10;

The way used to initialize a matrix to void is useful to initialize all of it
to a defined value too.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 06 2011
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3383


David Simcha <dsimcha yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
         Resolution|                            |FIXED



This evolved into std.array.uninitializedArray.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Aug 12 2011