Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
October 09, 2009 [Issue 3383] New: newVoid | ||||
---|---|---|---|---|
| ||||
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 --- Comment #0 from David Simcha <dsimcha@yahoo.com> 2009-10-09 10:08:58 PDT --- 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: ------- |
October 11, 2009 [Issue 3383] newVoid | ||||
---|---|---|---|---|
| ||||
Posted in reply to David Simcha | 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: ------- |
June 05, 2011 [Issue 3383] newVoid | ||||
---|---|---|---|---|
| ||||
Posted in reply to David Simcha | 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 --- Comment #1 from Andrei Alexandrescu <andrei@metalanguage.com> 2011-06-05 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: ------- |
June 05, 2011 [Issue 3383] newVoid | ||||
---|---|---|---|---|
| ||||
Posted in reply to David Simcha | http://d.puremagic.com/issues/show_bug.cgi?id=3383 bearophile_hugs@eml.cc changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |bearophile_hugs@eml.cc --- Comment #2 from bearophile_hugs@eml.cc 2011-06-05 12:29:45 PDT --- 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: ------- |
June 06, 2011 [Issue 3383] newVoid | ||||
---|---|---|---|---|
| ||||
Posted in reply to David Simcha | http://d.puremagic.com/issues/show_bug.cgi?id=3383 --- Comment #3 from bearophile_hugs@eml.cc 2011-06-06 03:26:11 PDT --- 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: ------- |
August 13, 2011 [Issue 3383] newVoid | ||||
---|---|---|---|---|
| ||||
Posted in reply to David Simcha | http://d.puremagic.com/issues/show_bug.cgi?id=3383 David Simcha <dsimcha@yahoo.com> changed: What |Removed |Added ---------------------------------------------------------------------------- Status|ASSIGNED |RESOLVED Resolution| |FIXED --- Comment #4 from David Simcha <dsimcha@yahoo.com> 2011-08-12 20:43:48 PDT --- This evolved into std.array.uninitializedArray. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- |
Copyright © 1999-2021 by the D Language Foundation