Void initialization

2011-12-19 Thread Bear
Using D1, I have a program that creates tons of float[] ; for performance reasons, I would like them to be uninitialized. I've tried replacing float[] f = new float[x]; by float[] f = cast(float[])std.gc.malloc(x*4); Unfortunately I keep running into "Access violation" and sometimes "Array bound

Re: Void initialization

2011-12-19 Thread bearophile
Bear: > Using D1, I have a program that creates tons of float[] ; for performance > reasons, I would like them to be uninitialized. > I've tried replacing > > float[] f = new float[x]; > by > float[] f = cast(float[])std.gc.malloc(x*4); Try something like this (untested): alias float TF; TF[] f

Re: Void initialization

2011-12-19 Thread Trass3r
Am 19.12.2011, 13:04 Uhr, schrieb Bear : Using D1, I have a program that creates tons of float[] ; for performance reasons, I would like them to be uninitialized. std.array.uninitializedArray

Re: Void initialization

2011-12-19 Thread Steven Schveighoffer
On Mon, 19 Dec 2011 07:04:20 -0500, Bear wrote: Using D1, I have a program that creates tons of float[] ; for performance reasons, I would like them to be uninitialized. I've tried replacing float[] f = new float[x]; by float[] f = cast(float[])std.gc.malloc(x*4); this is wrong. a float[] s

Re: Void initialization

2011-12-19 Thread Bear
gc.malloc actually returns void[] Bearophile's suggestion seems to work though, but it doesn't seem to improve performance for some reason... I guess I'll have to find some other way to make my prog quicker.

Re: Void initialization

2011-12-19 Thread Jacob Carlborg
On 2011-12-19 18:24, Bear wrote: gc.malloc actually returns void[] Bearophile's suggestion seems to work though, but it doesn't seem to improve performance for some reason... I guess I'll have to find some other way to make my prog quicker. You can always make the variable uninitialized using

Re: Void initialization

2011-12-19 Thread Steven Schveighoffer
On Mon, 19 Dec 2011 12:24:18 -0500, Bear wrote: gc.malloc actually returns void[] http://www.d-programming-language.org/phobos/core_memory.html#malloc Looks like void* to me... Or is there another function I'm not aware of? I think it should be GC.malloc, not gc.malloc, so maybe I'm miss

Re: Void initialization

2011-12-19 Thread Jesse Phillips
On Mon, 19 Dec 2011 18:52:44 +0100 Jacob Carlborg wrote: > You can always make the variable uninitialized using "void", don't > know if that what is what you're looking for. > > float[] f = void; > He is trying to create an array where the elements are not initialized.

Re: Void initialization

2011-12-19 Thread Jacob Carlborg
On 2011-12-20 01:34, Jesse Phillips wrote: On Mon, 19 Dec 2011 18:52:44 +0100 Jacob Carlborg wrote: You can always make the variable uninitialized using "void", don't know if that what is what you're looking for. float[] f = void; He is trying to create an array where the elements are not

Re: Void initialization

2011-12-20 Thread Stewart Gordon
On 19/12/2011 18:11, Steven Schveighoffer wrote: On Mon, 19 Dec 2011 12:24:18 -0500, Bear wrote: gc.malloc actually returns void[] http://www.d-programming-language.org/phobos/core_memory.html#malloc Looks like void* to me... Or is there another function I'm not aware of? I think it should

Re: Void initialization

2011-12-20 Thread Stewart Gordon
On 19/12/2011 12:12, bearophile wrote: Bear: float[] f = cast(float[])std.gc.malloc(x*4); Try something like this (untested): alias float TF; TF[] f = (cast(TF*)std.gc.malloc(x * TF.sizeof))[0 .. x]; I fail to see any real difference from the OP's code: - Why the alias? - std.gc.malloc

Re: Void initialization

2011-12-20 Thread Steven Schveighoffer
On Tue, 20 Dec 2011 09:22:46 -0500, Stewart Gordon wrote: On 19/12/2011 18:11, Steven Schveighoffer wrote: On Mon, 19 Dec 2011 12:24:18 -0500, Bear wrote: gc.malloc actually returns void[] http://www.d-programming-language.org/phobos/core_memory.html#malloc Looks like void* to me... O

Re: Void initialization

2011-12-20 Thread Timon Gehr
On 12/19/2011 01:04 PM, Bear wrote: Using D1, I have a program that creates tons of float[] ; for performance reasons, I would like them to be uninitialized. I've tried replacing float[] f = new float[x]; by float[] f = cast(float[])std.gc.malloc(x*4); Unfortunately I keep running into "Access

Re: Void initialization

2011-12-20 Thread bearophile
Stewart Gordon: > On 19/12/2011 12:12, bearophile wrote: > > Try something like this (untested): > > > > alias float TF; > > TF[] f = (cast(TF*)std.gc.malloc(x * TF.sizeof))[0 .. x]; > > > I fail to see any real difference from the OP's code: > > - Why the alias? Because in that code I have u

Re: Void initialization

2011-12-20 Thread Timon Gehr
On 12/20/2011 07:12 PM, bearophile wrote: Stewart Gordon: On 19/12/2011 12:12, bearophile wrote: Try something like this (untested): alias float TF; TF[] f = (cast(TF*)std.gc.malloc(x * TF.sizeof))[0 .. x]; I fail to see any real difference from the OP's code: - Why the alias? Because

Re: Void initialization

2011-12-20 Thread Stewart Gordon
On 20/12/2011 18:12, bearophile wrote: Because in that code I have used three times a type (TF), auto allows to remove only one of them. The alias is not the best solution (a better solution is to put that code into a templated function), but repeating the same generic type more than one time

Re: Void initialization

2011-12-20 Thread bearophile
Stewart Gordon: > I don't quite understand - why not just use float as it is? OK, so > abbreviating it to TF > saves 9 characters on that line, but the alias declaration and its trailing > line break > take up 16 characters, so you're not saving space at all. It's not a way to save chars, it'

Re: Void initialization

2011-12-20 Thread Stewart Gordon
On 20/12/2011 22:19, bearophile wrote: That's also why I have said a better solution is to wrap that code into a function template, so there is no need for an alias. So what you actually meant was to make TF a template parameter? That would make more sense. I can understand an alias being

Struct Postblit Void Initialization

2017-07-30 Thread Jiyan via Digitalmars-d-learn
Hey, just wanted to know whether something like this would be possible sowmehow: struct S { int m; int n; this(this) { m = void; n = n; } } So not the whole struct is moved everytime f.e. a function is called, but only n has to be "filled"

Dynamic array void initialization

2011-03-08 Thread Tom
import std.stdio; struct S { int i; int j; } int main(string[] args) { S[] ss = void; ss.length = 5; foreach (ref s; ss) s = S(1, 2); return 0; } Is the above code correct? (it doesn't work... it blows away or just give and access

Re: Struct Postblit Void Initialization

2017-07-30 Thread Eugene Wissner via Digitalmars-d-learn
On Sunday, 30 July 2017 at 19:22:07 UTC, Jiyan wrote: Hey, just wanted to know whether something like this would be possible sowmehow: struct S { int m; int n; this(this) { m = void; n = n; } } So not the whole struct is moved everytime f.e. a function is called, but only n has to be "filled

Re: Struct Postblit Void Initialization

2017-07-30 Thread Jiyan via Digitalmars-d-learn
On Sunday, 30 July 2017 at 19:32:48 UTC, Eugene Wissner wrote: On Sunday, 30 July 2017 at 19:22:07 UTC, Jiyan wrote: Hey, just wanted to know whether something like this would be possible sowmehow: struct S { int m; int n; this(this) { m = void; n = n; } } So not the whole struct is moved ev

Re: Struct Postblit Void Initialization

2017-07-30 Thread Moritz Maxeiner via Digitalmars-d-learn
On Sunday, 30 July 2017 at 19:22:07 UTC, Jiyan wrote: Hey, just wanted to know whether something like this would be possible sowmehow: struct S { int m; int n; this(this) { m = void; n = n; } } So not the whole struct is moved everytime f.e. a function is called, but only n has to be "filled

Re: Dynamic array void initialization

2011-03-08 Thread Ali Çehreli
On 03/08/2011 01:34 PM, Tom wrote: import std.stdio; struct S { int i; int j; } int main(string[] args) { S[] ss = void; ss.length = 5; foreach (ref s; ss) s = S(1, 2); return 0; } Is the above code correct? (it doesn't work... it blows away or just give and access violation error). I need to

Re: Dynamic array void initialization

2011-03-08 Thread Steven Schveighoffer
On Tue, 08 Mar 2011 16:53:08 -0500, Ali Çehreli wrote: On 03/08/2011 01:34 PM, Tom wrote: import std.stdio; struct S { int i; int j; } int main(string[] args) { S[] ss = void; ss.length = 5; foreach (ref s; ss) s = S(1, 2); return 0; } Is the above code correct? (it doesn't work... it blows

Re: Dynamic array void initialization

2011-03-08 Thread Ali Çehreli
On 03/08/2011 02:03 PM, Steven Schveighoffer wrote: > it's not std.array.reserve, it's object.reserve, always present, no need > to import. Thanks. The reserve that I found in array.d is std.array.Appender(T).reserve. Ali

Re: Dynamic array void initialization

2011-03-08 Thread Tom
El 08/03/2011 19:03, Steven Schveighoffer escribió: On Tue, 08 Mar 2011 16:53:08 -0500, Ali Çehreli wrote: On 03/08/2011 01:34 PM, Tom wrote: import std.stdio; struct S { int i; int j; } int main(string[] args) { S[] ss = void; ss.length = 5; foreach (ref s; ss) s = S(1, 2); return 0; } Is

Re: Dynamic array void initialization

2011-03-08 Thread Steven Schveighoffer
On Tue, 08 Mar 2011 17:48:37 -0500, Tom wrote: http://d.puremagic.com/issues/show_bug.cgi?id=5603 This is really sad. This kind of stuff is a must for performance. D is disappointing me too much yet :( There is always c's malloc, or you can try using the GC malloc directly. For applic

Re: Dynamic array void initialization

2011-03-08 Thread spir
On 03/08/2011 11:57 PM, Steven Schveighoffer wrote: On Tue, 08 Mar 2011 17:48:37 -0500, Tom wrote: http://d.puremagic.com/issues/show_bug.cgi?id=5603 This is really sad. This kind of stuff is a must for performance. D is disappointing me too much yet :( There is always c's malloc, or you c

Re: Dynamic array void initialization

2011-03-08 Thread Kai Meyer
On 03/08/2011 02:34 PM, Tom wrote: import std.stdio; struct S { int i; int j; } int main(string[] args) { S[] ss = void; ss.length = 5; foreach (ref s; ss) s = S(1, 2); return 0; } Is the above code correct? (it doesn't work... it blows away or just give and access violation error). I need to

Re: Dynamic array void initialization

2011-03-08 Thread Kai Meyer
On 03/08/2011 05:42 PM, Kai Meyer wrote: On 03/08/2011 02:34 PM, Tom wrote: import std.stdio; struct S { int i; int j; } int main(string[] args) { S[] ss = void; ss.length = 5; foreach (ref s; ss) s = S(1, 2); return 0; } Is the above code correct? (it doesn't work... it blows away or just gi

Re: Dynamic array void initialization

2011-03-08 Thread Tom
El 08/03/2011 21:42, Kai Meyer escribió: On 03/08/2011 02:34 PM, Tom wrote: import std.stdio; struct S { int i; int j; } int main(string[] args) { S[] ss = void; ss.length = 5; foreach (ref s; ss) s = S(1, 2); return 0; } Is the above code correct? (it doesn't work... it blows away or just gi

void initialization vs alignment holes

2010-03-06 Thread strtr
This is probably going a bit above my head, but "the existence of alignment holes in the objects is accounted for, usually by setting them all to 0 upon initialization" I suspect this isn't the case for void initialization; if my struct has some alignment hole I better not voi

Re: void initialization vs alignment holes

2010-03-06 Thread bearophile
strtr: > I suspect this isn't the case for void initialization; if my struct has some > alignment hole I better not void initialize it if ever I want to compare it > with somthing. > Is this correct? That has to be correct. > Would you ever have an alignment hole if all

Re: void initialization vs alignment holes

2010-03-06 Thread BCS
Hello Strtr, Would you ever have an alignment hole if all the struct contains are basic types(excluding bool)? real, char[n], byte[n] and short[m] (for n%4 != 0 and m%2 != 0) might be an issue. -- ... <

Re: void initialization vs alignment holes

2010-03-06 Thread strtr
BCS Wrote: > Hello Strtr, > > > Would you ever have an alignment hole if all the struct contains are > > basic types(excluding bool)? > > real, char[n], byte[n] and short[m] (for n%4 != 0 and m%2 != 0) might be > an issue. > Sounds logical, thanks!

Re: void initialization vs alignment holes

2010-03-06 Thread BCS
Hello Strtr, BCS Wrote: Hello Strtr, Would you ever have an alignment hole if all the struct contains are basic types(excluding bool)? real, char[n], byte[n] and short[m] (for n%4 != 0 and m%2 != 0) might be an issue. Sounds logical, thanks! I don't actually *know* those will be a pro

Re: void initialization vs alignment holes

2010-03-06 Thread strtr
bearophile Wrote: > strtr: > > > I suspect this isn't the case for void initialization; if my struct has > > some alignment hole I better not void initialize it if ever I want to > > compare it with something. > > Is this correct? > > That has to be

Re: void initialization vs alignment holes

2010-03-06 Thread BCS
Hello Strtr, Suppose I'd still would like to use void optimizations, how do you clear the holes manually? IIRC zero filling a block is likely cheaper that zero filling holes in it. I'd avoid "=void" unless you know you will be copying structs into the space (that will copy the holes as well

Re: void initialization vs alignment holes

2010-03-06 Thread strtr
BCS Wrote: > IIRC zero filling a block is likely cheaper that zero filling holes in it. > I'd avoid "=void" unless you know you will be copying structs into the space > (that will copy the holes as well). And unless you will never compare the struct, as the garbage bits will also be compared. I

Re: void initialization vs alignment holes

2010-03-07 Thread bearophile
the program. But I suggest you to avoid it when possible. You have to be careful, because they can contain spurious pointers, that the conservative GC will use to keep objects alive. This happens especially if the struct contains pointers in the first place. >I suspect this isn't the

Re: void initialization vs alignment holes

2010-03-07 Thread bearophile
The situation is ugly, see the post in the main newsgroup: http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=107153 Bye, bearophile

Re: void initialization vs alignment holes

2010-03-07 Thread bearophile
strtr: > static if( S.sizeof == members.sizeof ) > S s = void; > else > S s; You can use something like this :-) import std.stdio: writeln; int unusedBytesStruct(S)() if (is(S == struct)) { int totUsed; foreach (field; S.init.tupleof) totUsed += field.sizeof; return cast(in

Default initialization of static array faster than void initialization

2019-11-08 Thread wolframw via Digitalmars-d-learn
Hi, Chapter 12.15.2 of the spec explains that void initialization of a static array can be faster than default initialization. This seems logical because the array entries don't need to be set to NaN. However, when I ran some tests for my matrix implementation, it seemed that the de

Re: Default initialization of static array faster than void initialization

2019-11-08 Thread Ali Çehreli via Digitalmars-d-learn
One correction: I think you mean "using" a default-initialized array is faster (not the initialization itself). Another observation: dmd -O makes both cases slower! Hm? Ali

Re: Default initialization of static array faster than void initialization

2019-11-08 Thread kinke via Digitalmars-d-learn
On Friday, 8 November 2019 at 16:49:37 UTC, wolframw wrote: I compiled with dmd -O -inline -release -noboundscheck -mcpu=avx2 and ran the tests with the m array being default-initialized in one run and void-initialized in another run. The results: Default-initialized: 245 ms, 495 μs, and 2 hns