digitalmars.D - New abstraction: Layout
- Andrei Alexandrescu (12/12) Feb 16 2018 I've been long bothered that the builtin .tupleof and our own
- rikki cattermole (3/20) Feb 16 2018 Could use the name for the field as well. At the minimum useful for
- Andrei Alexandrescu (3/24) Feb 17 2018 That would be tricky because fields are decomposed down to primitive
- Nathan S. (4/9) Feb 19 2018 That's unfortunate. Not being able to get the names and types
- Steven Schveighoffer (22/32) Feb 20 2018 .tupleof does all this:
- thedeemon (6/8) Feb 17 2018 Off-topic:
- psychoRabbit (6/14) Feb 17 2018 We're talking about Andrei here, so it's far more likely
- Steven Schveighoffer (15/29) Feb 17 2018 Can't you just use offsetof?
- Steven Schveighoffer (7/39) Feb 17 2018 I found this also works:
- Andrei Alexandrescu (2/42) Feb 17 2018 Yes, the implementation uses offsetof. -- Andrei
- Steven Schveighoffer (5/15) Feb 17 2018 I guess I'm just confused based on the statement "the builtin .tupleof
- Dmitry Olshansky (5/21) Feb 18 2018 I guess the construct captures offsets as part of type. This is
- Steven Schveighoffer (9/30) Feb 20 2018 I haven't looked at it in depth, so I didn't know the result of the
- Andrei Alexandrescu (38/72) Feb 20 2018 There's the difference that with inline static foreach you can express
- Steven Schveighoffer (5/21) Feb 21 2018 [snip]
- Basile B. (3/16) Feb 20 2018 how does this deal with static members ?
- Jonathan M Davis (5/26) Feb 20 2018 It's built on tupleof, so it doesn't do anything with static members. Bu...
- Andrei Alexandrescu (3/23) Feb 21 2018 Static members are not part of the layout - aside from lexical scoping,
I've been long bothered that the builtin .tupleof and our own abstractions Fields and RepresentationTypeTuple in std.traits - all omit the essential information of field offsets. That makes types that use align() to have the same .tupleof, Fields, and RepresentationTypeTuple even though they shouldn't. The right answer is Layout a tuple of (offset, type) pairs describing entirely the memory layout of a type. We need such for memory allocation, garbage collection, serialization, and more. The implementation turned out to be quite compact - 81 lines including a compile-time mergesort. Destroy! https://github.com/dlang/phobos/pull/6192 Andrei
Feb 16 2018
On 17/02/2018 12:04 AM, Andrei Alexandrescu wrote:I've been long bothered that the builtin .tupleof and our own abstractions Fields and RepresentationTypeTuple in std.traits - all omit the essential information of field offsets. That makes types that use align() to have the same .tupleof, Fields, and RepresentationTypeTuple even though they shouldn't. The right answer is Layout a tuple of (offset, type) pairs describing entirely the memory layout of a type. We need such for memory allocation, garbage collection, serialization, and more. The implementation turned out to be quite compact - 81 lines including a compile-time mergesort. Destroy! https://github.com/dlang/phobos/pull/6192 AndreiCould use the name for the field as well. At the minimum useful for debugging purposes.
Feb 16 2018
On 02/16/2018 10:10 PM, rikki cattermole wrote:On 17/02/2018 12:04 AM, Andrei Alexandrescu wrote:That would be tricky because fields are decomposed down to primitive types. -- AndreiI've been long bothered that the builtin .tupleof and our own abstractions Fields and RepresentationTypeTuple in std.traits - all omit the essential information of field offsets. That makes types that use align() to have the same .tupleof, Fields, and RepresentationTypeTuple even though they shouldn't. The right answer is Layout a tuple of (offset, type) pairs describing entirely the memory layout of a type. We need such for memory allocation, garbage collection, serialization, and more. The implementation turned out to be quite compact - 81 lines including a compile-time mergesort. Destroy! https://github.com/dlang/phobos/pull/6192 AndreiCould use the name for the field as well. At the minimum useful for debugging purposes.
Feb 17 2018
On Saturday, 17 February 2018 at 12:49:07 UTC, Andrei Alexandrescu wrote:On 02/16/2018 10:10 PM, rikki cattermole wrote:That's unfortunate. Not being able to get the names and types together is my main dissatisfaction with the current interface.Could use the name for the field as well. At the minimum useful for debugging purposes.That would be tricky because fields are decomposed down to primitive types. -- Andrei
Feb 19 2018
On 2/19/18 6:18 AM, Nathan S. wrote:On Saturday, 17 February 2018 at 12:49:07 UTC, Andrei Alexandrescu wrote:.tupleof does all this: struct S { int x; double y; } static foreach(alias v; S.tupleof) { pragma(msg, typeof(v)); pragma(msg, v.offsetof); pragma(msg, __traits(identifier, v)); } output while compiling: int 0LU x double 8LU y It's admittedly an ugly interface... -SteveOn 02/16/2018 10:10 PM, rikki cattermole wrote:That's unfortunate. Not being able to get the names and types together is my main dissatisfaction with the current interface.Could use the name for the field as well. At the minimum useful for debugging purposes.That would be tricky because fields are decomposed down to primitive types. -- Andrei
Feb 20 2018
On Saturday, 17 February 2018 at 00:04:16 UTC, Andrei Alexandrescu wrote:The implementation turned out to be quite compact - 81 lines including a compile-time mergesort. Destroy!Off-topic: I've just realized Andrei puts "Destroy!" at the end of his messages because it's the end of scope and he wants to free any allocated resources. ;)
Feb 17 2018
On Saturday, 17 February 2018 at 09:30:23 UTC, thedeemon wrote:On Saturday, 17 February 2018 at 00:04:16 UTC, Andrei Alexandrescu wrote:We're talking about Andrei here, so it's far more likely 'destroy' is an efficient, compact, generic term, that can take on many different meanings - depending on the context in which it used. 'D'estroy!The implementation turned out to be quite compact - 81 lines including a compile-time mergesort. Destroy!Off-topic: I've just realized Andrei puts "Destroy!" at the end of his messages because it's the end of scope and he wants to free any allocated resources. ;)
Feb 17 2018
On 2/16/18 7:04 PM, Andrei Alexandrescu wrote:I've been long bothered that the builtin .tupleof and our own abstractions Fields and RepresentationTypeTuple in std.traits - all omit the essential information of field offsets. That makes types that use align() to have the same .tupleof, Fields, and RepresentationTypeTuple even though they shouldn't. The right answer is Layout a tuple of (offset, type) pairs describing entirely the memory layout of a type. We need such for memory allocation, garbage collection, serialization, and more. The implementation turned out to be quite compact - 81 lines including a compile-time mergesort. Destroy! https://github.com/dlang/phobos/pull/6192Can't you just use offsetof? struct S { ubyte x; int y; } static foreach(i; 0 .. s.tupleof.length) { writeln(s.tupleof[i].offsetof); } outputs: 0 4 -Steve
Feb 17 2018
On 2/17/18 8:19 AM, Steven Schveighoffer wrote:On 2/16/18 7:04 PM, Andrei Alexandrescu wrote:I found this also works: static foreach(alias x; S.tupleof) { writeln(x.offsetof); } -SteveI've been long bothered that the builtin .tupleof and our own abstractions Fields and RepresentationTypeTuple in std.traits - all omit the essential information of field offsets. That makes types that use align() to have the same .tupleof, Fields, and RepresentationTypeTuple even though they shouldn't. The right answer is Layout a tuple of (offset, type) pairs describing entirely the memory layout of a type. We need such for memory allocation, garbage collection, serialization, and more. The implementation turned out to be quite compact - 81 lines including a compile-time mergesort. Destroy! https://github.com/dlang/phobos/pull/6192Can't you just use offsetof? struct S { ubyte x; int y; } static foreach(i; 0 .. s.tupleof.length) { writeln(s.tupleof[i].offsetof); } outputs: 0 4
Feb 17 2018
On 02/17/2018 09:03 AM, Steven Schveighoffer wrote:On 2/17/18 8:19 AM, Steven Schveighoffer wrote:Yes, the implementation uses offsetof. -- AndreiOn 2/16/18 7:04 PM, Andrei Alexandrescu wrote:I found this also works: static foreach(alias x; S.tupleof) { writeln(x.offsetof); }I've been long bothered that the builtin .tupleof and our own abstractions Fields and RepresentationTypeTuple in std.traits - all omit the essential information of field offsets. That makes types that use align() to have the same .tupleof, Fields, and RepresentationTypeTuple even though they shouldn't. The right answer is Layout a tuple of (offset, type) pairs describing entirely the memory layout of a type. We need such for memory allocation, garbage collection, serialization, and more. The implementation turned out to be quite compact - 81 lines including a compile-time mergesort. Destroy! https://github.com/dlang/phobos/pull/6192Can't you just use offsetof? struct S { ubyte x; int y; } static foreach(i; 0 .. s.tupleof.length) { writeln(s.tupleof[i].offsetof); } outputs: 0 4
Feb 17 2018
On 2/17/18 9:59 AM, Andrei Alexandrescu wrote:On 02/17/2018 09:03 AM, Steven Schveighoffer wrote:I guess I'm just confused based on the statement "the builtin .tupleof ... [omits] the essential information of field offsets." What is this construct giving us that .tupleof doesn't? -SteveI found this also works: static foreach(alias x; S.tupleof) { writeln(x.offsetof); }Yes, the implementation uses offsetof.
Feb 17 2018
On Saturday, 17 February 2018 at 19:37:12 UTC, Steven Schveighoffer wrote:On 2/17/18 9:59 AM, Andrei Alexandrescu wrote:I guess the construct captures offsets as part of type. This is useful for allocators + 2 things with same Layout can be bitblitted to each other.On 02/17/2018 09:03 AM, Steven Schveighoffer wrote:I guess I'm just confused based on the statement "the builtin .tupleof ... [omits] the essential information of field offsets." What is this construct giving us that .tupleof doesn't?I found this also works: static foreach(alias x; S.tupleof) { writeln(x.offsetof); }Yes, the implementation uses offsetof.-Steve
Feb 18 2018
On 2/18/18 4:52 AM, Dmitry Olshansky wrote:On Saturday, 17 February 2018 at 19:37:12 UTC, Steven Schveighoffer wrote:I haven't looked at it in depth, so I didn't know the result of the abstraction (I thought it was a tuple, or a pair of tuples). Note, you could do this without the need for a new abstraction, simply with .tupleof on the type itself. And static foreach has made this much simpler. But definitely the interface to getting things from tupleof is not consistent. This reason alone may be cause to add such a construct. -SteveOn 2/17/18 9:59 AM, Andrei Alexandrescu wrote:I guess the construct captures offsets as part of type. This is useful for allocators + 2 things with same Layout can be bitblitted to each other.On 02/17/2018 09:03 AM, Steven Schveighoffer wrote:I guess I'm just confused based on the statement "the builtin .tupleof ... [omits] the essential information of field offsets." What is this construct giving us that .tupleof doesn't?I found this also works: static foreach(alias x; S.tupleof) { writeln(x.offsetof); }Yes, the implementation uses offsetof.
Feb 20 2018
On 02/20/2018 07:34 AM, Steven Schveighoffer wrote:On 2/18/18 4:52 AM, Dmitry Olshansky wrote:There's the difference that with inline static foreach you can express one processing of one layout, whereas with a structured result you can express the notion of any processing of any layout. The distinction between an inlined for loop and the map function comes to mind. In particular, static foreach would be a good implementation device for Layout. I happened to express it a different way because it seemed simpler, but contributions welcome. Next step is to manipulate layouts. I have in mind an algorithm "eraseNonPointerTypes". It does the following. Consider: struct A { int a, b; string x; double c; int[] y; } So the layout is Layout!(0, int, 4, int, 8, string, 24, double, 32, int[]). Next we want to not care about non-pointers - just merge all that nonsense together. So eraseNonPointerTypes applied to this layout would yield Layout!(0, ubyte[8], 8, string, 24, ubyte[8], 32, int[]). At this point we figure that this partially erased layout is the same as for this other type: struct B { double a; string b; long c; int[] d; } And the ba dum tss of it all is that you can allocate an A, deallocate it, then allocate a B in the same memory - all in safe code. Even if you have a dangling pointer to A messing with a B object, the code will be incorrect but will not lose memory safety. This is a major part of Alexandru Jercaianu's work on Blizzard - a memory-safe allocation framework. I am so excited about the impending release I can't stand myself. AndreiOn Saturday, 17 February 2018 at 19:37:12 UTC, Steven Schveighoffer wrote:I haven't looked at it in depth, so I didn't know the result of the abstraction (I thought it was a tuple, or a pair of tuples). Note, you could do this without the need for a new abstraction, simply with .tupleof on the type itself. And static foreach has made this much simpler. But definitely the interface to getting things from tupleof is not consistent. This reason alone may be cause to add such a construct.On 2/17/18 9:59 AM, Andrei Alexandrescu wrote:I guess the construct captures offsets as part of type. This is useful for allocators + 2 things with same Layout can be bitblitted to each other.On 02/17/2018 09:03 AM, Steven Schveighoffer wrote:I guess I'm just confused based on the statement "the builtin .tupleof ... [omits] the essential information of field offsets." What is this construct giving us that .tupleof doesn't?I found this also works: static foreach(alias x; S.tupleof) { writeln(x.offsetof); }Yes, the implementation uses offsetof.
Feb 20 2018
On 2/20/18 10:56 PM, Andrei Alexandrescu wrote:On 02/20/2018 07:34 AM, Steven Schveighoffer wrote:[snip] Yep, this all makes sense, thanks! Sounds very similar to the Strawman structs thing, only probably better thought out ;) -SteveI haven't looked at it in depth, so I didn't know the result of the abstraction (I thought it was a tuple, or a pair of tuples). Note, you could do this without the need for a new abstraction, simply with .tupleof on the type itself. And static foreach has made this much simpler. But definitely the interface to getting things from tupleof is not consistent. This reason alone may be cause to add such a construct.There's the difference that with inline static foreach you can express one processing of one layout, whereas with a structured result you can express the notion of any processing of any layout. The distinction between an inlined for loop and the map function comes to mind.
Feb 21 2018
On Saturday, 17 February 2018 at 00:04:16 UTC, Andrei Alexandrescu wrote:I've been long bothered that the builtin .tupleof and our own abstractions Fields and RepresentationTypeTuple in std.traits - all omit the essential information of field offsets. That makes types that use align() to have the same .tupleof, Fields, and RepresentationTypeTuple even though they shouldn't. The right answer is Layout a tuple of (offset, type) pairs describing entirely the memory layout of a type. We need such for memory allocation, garbage collection, serialization, and more. The implementation turned out to be quite compact - 81 lines including a compile-time mergesort. Destroy! https://github.com/dlang/phobos/pull/6192 Andreihow does this deal with static members ?
Feb 20 2018
On Wednesday, February 21, 2018 05:53:31 Basile B. via Digitalmars-d wrote:On Saturday, 17 February 2018 at 00:04:16 UTC, Andrei Alexandrescu wrote:It's built on tupleof, so it doesn't do anything with static members. But static members have nothing to do with the layout of a type, so it wouldn't make sense for it to do anything with static members. - Jonathan M DavisI've been long bothered that the builtin .tupleof and our own abstractions Fields and RepresentationTypeTuple in std.traits - all omit the essential information of field offsets. That makes types that use align() to have the same .tupleof, Fields, and RepresentationTypeTuple even though they shouldn't. The right answer is Layout a tuple of (offset, type) pairs describing entirely the memory layout of a type. We need such for memory allocation, garbage collection, serialization, and more. The implementation turned out to be quite compact - 81 lines including a compile-time mergesort. Destroy! https://github.com/dlang/phobos/pull/6192 Andreihow does this deal with static members ?
Feb 20 2018
On 02/21/2018 12:53 AM, Basile B. wrote:On Saturday, 17 February 2018 at 00:04:16 UTC, Andrei Alexandrescu wrote:Static members are not part of the layout - aside from lexical scoping, they have the same regime as module-level static data. -- AndreiI've been long bothered that the builtin .tupleof and our own abstractions Fields and RepresentationTypeTuple in std.traits - all omit the essential information of field offsets. That makes types that use align() to have the same .tupleof, Fields, and RepresentationTypeTuple even though they shouldn't. The right answer is Layout a tuple of (offset, type) pairs describing entirely the memory layout of a type. We need such for memory allocation, garbage collection, serialization, and more. The implementation turned out to be quite compact - 81 lines including a compile-time mergesort. Destroy! https://github.com/dlang/phobos/pull/6192 Andreihow does this deal with static members ?
Feb 21 2018