Thread overview | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
December 19, 2011 Void initialization | ||||
---|---|---|---|---|
| ||||
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 bounds error". I've tried adding setTypeInfo(typeid(float), f.ptr); and hasNoPointer(f.ptr); which didn't work. However f[] = float.nan; solved the problem, but kinda defeats the purpose of using malloc... What am I doing wrong? |
December 19, 2011 Re: Void initialization | ||||
---|---|---|---|---|
| ||||
Posted in reply to Bear | 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 = (cast(TF*)std.gc.malloc(x * TF.sizeof))[0 .. x];
Using x*4 is error-prone. I suggest to wrap that code inside a templated function, where T is the type of the items, to avoid some bugs.
Bye,
bearophile
|
December 19, 2011 Re: Void initialization | ||||
---|---|---|---|---|
| ||||
Posted in reply to Bear | Am 19.12.2011, 13:04 Uhr, schrieb Bear <joanyleprince@yahoo.fr>:
> Using D1, I have a program that creates tons of float[] ; for performance
> reasons, I would like them to be uninitialized.
std.array.uninitializedArray
|
December 19, 2011 Re: Void initialization | ||||
---|---|---|---|---|
| ||||
Posted in reply to Bear | On Mon, 19 Dec 2011 07:04:20 -0500, Bear <joanyleprince@yahoo.fr> 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[] slice is actually a struct, whereas gc.malloc returns a pointer. What you have done is cast a pointer into a pointer+length struct, leaving anyones guess as to what the length is set to. I don't even know why this compiles... A slice is a pointer + length, and you can slice a pointer to "add a length" to it. Follow bearophile's suggestion. > However > f[] = float.nan; > solved the problem, but kinda defeats the purpose of using malloc... > What am I doing wrong? If this works, it's not doing what you think :) -Steve |
December 19, 2011 Re: Void initialization | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | 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. |
December 19, 2011 Re: Void initialization | ||||
---|---|---|---|---|
| ||||
Posted in reply to Bear | 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 "void", don't know if that what is what you're looking for. float[] f = void; -- /Jacob Carlborg |
December 19, 2011 Re: Void initialization | ||||
---|---|---|---|---|
| ||||
Posted in reply to Bear | On Mon, 19 Dec 2011 12:24:18 -0500, Bear <joanyleprince@yahoo.fr> 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 missing something... > 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. Actually, an issue with bearophile's suggestion is that it allocates a block marked as containing pointers. Such a block is bulk-initialized to 0. Try this: float[] f = (cast(float*)GC.malloc(x * TF.sizeof, GC.BlkAttr.NO_SCAN))[0 .. x]; This will leave the memory uninitialized. And depending on the usage, this optimization may or may not make a huge difference. -Steve |
December 20, 2011 Re: Void initialization | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jacob Carlborg | On Mon, 19 Dec 2011 18:52:44 +0100
Jacob Carlborg <doob@me.com> 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.
|
December 20, 2011 Re: Void initialization | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jesse Phillips | On 2011-12-20 01:34, Jesse Phillips wrote: > On Mon, 19 Dec 2011 18:52:44 +0100 > Jacob Carlborg<doob@me.com> 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. Ah, I see. -- /Jacob Carlborg |
December 20, 2011 Re: Void initialization | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On 19/12/2011 18:11, Steven Schveighoffer wrote:
> On Mon, 19 Dec 2011 12:24:18 -0500, Bear <joanyleprince@yahoo.fr> 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 missing something...
<snip>
You are. That the OP was talking about std.gc.malloc in D1, not the core.memory stuff in D2.
Stewart.
|
Copyright © 1999-2021 by the D Language Foundation