May 26, 2016 Re: Testing array ptr for offset 0... | ||||
---|---|---|---|---|
| ||||
Posted in reply to ag0aep6g | On Thursday, 26 May 2016 at 22:15:42 UTC, ag0aep6g wrote: > On 05/26/2016 11:13 PM, Era Scarecrow wrote: >> void scan(ref Data[][] data, Condition cond) { >> foreach(i; ...) { >> if (cond) >> data[i] ~= ... >> } >> } > > Sorry, I'm still lost. Why can't you do whatever you're doing in opOpAssign directly there, or in a free function? Does the pseudo-array contain any additional data? Would a wrapper around a built-in array not work? Well a few things at work. First the array is originally a static array, so I pass it as a slice; But if you pass it as a slice you get the entire length, and modifying the length requires either changing length or appending, both which may cause allocation (something I want to avoid). By making my own compatible structure I can manage the length and pointer directly and precisely. Second I would prefer the length to start at 0 and grow UP TO the size of the static array. If it was just a single array this wouldn't be an issue, but I'm working with a couple hundred of these. Plus I don't want to add more data to the memory or structure than necessary to treat it as an array after the fact. I shouldn't have to. Truthfully the same result can be done with shrinking it afterwards, but I was trying to see if I could make it any faster by managing the pointer and length directly and avoiding the large temporaries. So far any speed gains are so minimal it's worth dropping. > Oh, I see. Yeah, the answer is garbage. Seeing the spec section that Adam D. Ruppe linked, the length field actually comes first. > > That CTFE errors out when you move the pointer to a variable screams that something is wrong. The code seems to be seriously misinterpreted in CTFE. > > I've filed an issue: > https://issues.dlang.org/show_bug.cgi?id=16081 Glad I found a bug! :) And all because [].ptr.offsetof wasn't an option... I just wanted to guarantee it was the correct order (in case it gets changed in the future). But ultimately it isn't worth pursuing. |
May 27, 2016 Re: Testing array ptr for offset 0... | ||||
---|---|---|---|---|
| ||||
Posted in reply to Era Scarecrow | On Thursday, 26 May 2016 at 22:47:02 UTC, Era Scarecrow wrote:
> On Thursday, 26 May 2016 at 22:15:42 UTC, ag0aep6g wrote:
>> Sorry, I'm still lost. Why can't you do whatever you're doing in opOpAssign directly there, or in a free function? Does the pseudo-array contain any additional data? Would a wrapper around a built-in array not work?
>
> Well a few things at work. First the array is originally a static array, so I pass it as a slice; But if you pass it as a slice you get the entire length, and modifying the length requires either changing length or appending, both which may cause allocation (something I want to avoid). By making my own compatible structure I can manage the length and pointer directly and precisely.
You can do that with arrays, too, without causing allocations:
assert(slice.length < static_array.length);
slice = slice.ptr[0 .. slice.length+1];
Of course that's unsafe, but your pointer magic certainly is, too.
|
May 27, 2016 Re: Testing array ptr for offset 0... | ||||
---|---|---|---|---|
| ||||
Posted in reply to Marc Schütz | On Friday, 27 May 2016 at 09:18:47 UTC, Marc Schütz wrote:
> You can do that with arrays, too, without causing allocations:
>
> assert(slice.length < static_array.length);
> slice = slice.ptr[0 .. slice.length+1];
>
> Of course that's unsafe, but your pointer magic certainly is, too.
Initial impressions suggest it's actually slower. The multiple dereferences to do it since it's a 2d array...
|
May 27, 2016 Re: Testing array ptr for offset 0... | ||||
---|---|---|---|---|
| ||||
Posted in reply to Era Scarecrow | On Thursday, 26 May 2016 at 21:13:14 UTC, Era Scarecrow wrote:
> To do what I want currently it's something like...
>
> enum Size = 1024, Other = 128;
> Data[Size][Other] staticarray; //stack allocation
> Data[][] sliced = staticarray[];
> scan(sliced, condition);
>
> void scan(ref Data[][] data, Condition cond) {
> int lengths[Size];
>
> foreach(i; ...) {
> if (cond)
> data[i][lengths[i]++] = ...
> }
>
> //cleanup/shrink
> foreach(i, l; lengths)
> data[i] = data[i][0 .. l];
> }
Like this:
static void rawlength(ref Data[] r, size_t len) {
r = r.ptr[0..len];
}
void scan(ref Data[][] data, Condition cond) {
foreach(i; ...) {
if (cond)
data[i].rawlength = ...
}
}
|
Copyright © 1999-2021 by the D Language Foundation