April 17, 2014 Re: Dynamically Sized Structs | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On Thursday, 17 April 2014 at 17:35:17 UTC, bearophile wrote:
> Kagamin:
>
>> Bound checked version of variable size struct: http://dpaste.dzfl.pl/fcd91d6912d3
>
> I think you are missing one of the main points of a variable sized struct, that is to reduce by 1 the number of indirection levels.
>
> Bye,
> bearophile
Yeah, otherwise one could have just used dynamic array field.
|
April 17, 2014 Re: Dynamically Sized Structs | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On Thursday, 17 April 2014 at 17:35:17 UTC, bearophile wrote:
> Kagamin:
>
>> Bound checked version of variable size struct: http://dpaste.dzfl.pl/fcd91d6912d3
>
> I think you are missing one of the main points of a variable sized struct, that is to reduce by 1 the number of indirection levels.
>
> Bye,
> bearophile
As far as I can see, my implementation has only 1 indirection level.
|
April 17, 2014 Re: Dynamically Sized Structs | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kagamin | On Thursday, 17 April 2014 at 17:45:22 UTC, Kagamin wrote:
> On Thursday, 17 April 2014 at 17:35:17 UTC, bearophile wrote:
>> Kagamin:
>>
>>> Bound checked version of variable size struct: http://dpaste.dzfl.pl/fcd91d6912d3
>>
>> I think you are missing one of the main points of a variable sized struct, that is to reduce by 1 the number of indirection levels.
>>
>> Bye,
>> bearophile
>
> As far as I can see, my implementation has only 1 indirection level.
And C-like dynamic structs have 0 indirection.
|
April 17, 2014 Re: Dynamically Sized Structs | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dicebot | State* pointer in sokoban example is a perfect 1 indirection. |
April 17, 2014 Re: Dynamically Sized Structs | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kagamin | On Thursday, 17 April 2014 at 18:17:59 UTC, Kagamin wrote:
> State* pointer in sokoban example is a perfect 1 indirection.
It is not related to actual "dynamic struct" thing, which is why I have highlighted the line to look at. Minimal example is this:
struct Dynamic
{
size_t length;
int[0] payload;
}
void main()
{
const length = 42;
Dynamic* entity = alloca(Dynamic.sizeof + int.sizeof*length);
entity.length = length;
// pointer to same stack space so not really an indirection
entity.payload[5] = 43;
}
|
April 17, 2014 Re: Dynamically Sized Structs | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dicebot | How is this different from my example? |
April 17, 2014 Re: Dynamically Sized Structs | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kagamin | On Thursday, 17 April 2014 at 18:29:21 UTC, Kagamin wrote:
> How is this different from my example?
b = new byte[StateImpl.sizeof + CellIndex.sizeof*cellCount];
this line creates heap indirection
|
April 17, 2014 Re: Dynamically Sized Structs | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dicebot | So you assert that variable length structs can't be allocated on heap and sokoban example is a wrong example of variable length struct usage? And how heap indirection is different from stack indirection? It's still indirection. |
April 17, 2014 Re: Dynamically Sized Structs | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dicebot | On Thursday, 17 April 2014 at 17:50:30 UTC, Dicebot wrote:
> On Thursday, 17 April 2014 at 17:45:22 UTC, Kagamin wrote:
>> On Thursday, 17 April 2014 at 17:35:17 UTC, bearophile wrote:
>>> Kagamin:
>>>
>>>> Bound checked version of variable size struct: http://dpaste.dzfl.pl/fcd91d6912d3
>>>
>>> I think you are missing one of the main points of a variable sized struct, that is to reduce by 1 the number of indirection levels.
>>>
>>> Bye,
>>> bearophile
>>
>> As far as I can see, my implementation has only 1 indirection level.
>
> And C-like dynamic structs have 0 indirection.
Well, technically everything on the stack is accessed through an indirection (RBP + offset), but there are more optimisation opportunities if the base and/or offset is known statically. Performance-wise you're gaining nothing over a static array (or any alloca buffer) and potentially losing something compared to normal stack variables / struct members.
|
April 17, 2014 Re: Dynamically Sized Structs | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kagamin | On Thursday, 17 April 2014 at 18:40:27 UTC, Kagamin wrote:
> And how heap indirection is different from stack indirection? It's still indirection.
Locality. The stack is (within reason) readily available in cache.
|
Copyright © 1999-2021 by the D Language Foundation