Why are there no (plans for) structs in dart?

1,487 views
Skip to first unread message

jako

unread,
Apr 2, 2012, 2:11:49 PM4/2/12
to General Dart Discussion
I couldn't find anything about this topic so... why are there no
structs in dart... or plans to add them?

Seth Ladd

unread,
Apr 2, 2012, 2:16:40 PM4/2/12
to jako, General Dart Discussion
Hi Jako,

Nothing is ruled out forever, but right now Dart is being designed to be familiar with Java and JavaScript developers. Neither of those languages have a formal struct. Also, Dart classes can be defined quite easily:

class Person {
  String firstName;
  String lastName;
  Date dob;
}

And the fields will have getters and setters:

Person bob = new Person();
bob.firstName = 'Bob';
bob.lastName = 'Dartsmith';

Also, I should mention that Dart ships with JSON libraries, which help you serialize and deserialize rich data structures like maps and lists.

Might I ask what use cases you have in mind for formal struct in Dart?

Thanks,
Seth

jako

unread,
Apr 2, 2012, 3:12:08 PM4/2/12
to General Dart Discussion
Yes thats right, bit imagine you deal with a lot of data maybe 200
Vector3 objects. You get quite a lot of memory useage just because of
the references for each of the object. Also (at least in the .net c#
implementation) you can use it to reduce garbage collection calls.

Rémi Forax

unread,
Apr 3, 2012, 2:56:16 AM4/3/12
to mi...@dartlang.org
On 04/02/2012 09:12 PM, jako wrote:
> Yes thats right, bit imagine you deal with a lot of data maybe 200
> Vector3 objects. You get quite a lot of memory useage just because of
> the references for each of the object. Also (at least in the .net c#
> implementation) you can use it to reduce garbage collection calls.

but you do a lot of useless copy on stack.

R�mi

Florian Loitsch

unread,
Apr 4, 2012, 7:32:33 AM4/4/12
to jako, General Dart Discussion
On Mon, Apr 2, 2012 at 21:12, jako <p4j...@googlemail.com> wrote:
Yes thats right, bit imagine you deal with a lot of data maybe 200
Vector3 objects. You get quite a lot of memory useage just because of
the references for each of the object. Also (at least in the .net c#
implementation) you can use it to reduce garbage collection calls.
This clarifies your initial question. For many of us "struct" does not necessarily mean non-heap objects.
If I understand correctly you want to get rid of the pointers to the heap-objects and store objects directly in lists (or even fields).
Structs (as you call them) would be extremely difficult to implement. Imagine the following example:
===
var v = struct Vector3(1, 2, 3);  // <= assuming the Vector3 is a struct.
var list = new List(3);
list[0] = v;
...
===
The list must be able to store any type in it. It cannot just reserve three times the size of Vector3. Also, since Dart is dynamic, it needs to attach some type-information to the object. Note that even "internal" types, like doubles, share this fate. When a double is stored in a list or a field it needs to be boxed. Naively this would even be true for integers, but the VM uses pointer-tagging to allow a specific range (usually 31 or 63bits) to stay unboxed. (We call these numbers Smis for "Small Integers".)
That said: the devs of v8 (Chrome's JavaScript implementation) are working on removing this overhead by automatically unboxing when it detects homogenous arrays (of doubles). Eventually Dart might try to do the same.
// florian



On 2 Apr., 20:16, Seth Ladd <sethl...@google.com> wrote:
> Hi Jako,
>
> Nothing is ruled out forever, but right now Dart is being designed to be
> familiar with Java and JavaScript developers. Neither of those languages
> have a formal struct. Also, Dart classes can be defined quite easily:
>
> class Person {
>   String firstName;
>   String lastName;
>   Date dob;
>
> }
>
> And the fields will have getters and setters:
>
> Person bob = new Person();
> bob.firstName = 'Bob';
> bob.lastName = 'Dartsmith';
>
> Also, I should mention that Dart ships with JSON libraries, which help you
> serialize and deserialize rich data structures like maps and lists.
>
> Might I ask what use cases you have in mind for formal struct in Dart?
>
> Thanks,
> Seth
>
>
>
>
>
>
>
> On Mon, Apr 2, 2012 at 11:11 AM, jako <p4j...@googlemail.com> wrote:
> > I couldn't find anything about this topic so... why are there no
> > structs in dart... or plans to add them?



--
Give a man a fire and he's warm for the whole day,
but set fire to him and he's warm for the rest of his life. - Terry Pratchett

Ladislav Thon

unread,
Apr 4, 2012, 7:43:35 AM4/4/12
to Florian Loitsch, jako, General Dart Discussion
Naively this would even be true for integers, but the VM uses pointer-tagging to allow a specific range (usually 31 or 63bits) to stay unboxed. (We call these numbers Smis for "Small Integers".)

Doesn't the tag actually take 2 bits? At least looking at the declaration of class Smi in runtime/vm/object.h, it seems to be that case. That gives you 4 possible tags, one needs to be used for heap-allocated objects and one is for Smis. The remaining two remain unused?

LT

Florian Loitsch

unread,
Apr 4, 2012, 7:56:17 AM4/4/12
to Ladislav Thon, jako, General Dart Discussion
On Wed, Apr 4, 2012 at 13:43, Ladislav Thon <lad...@gmail.com> wrote:
Naively this would even be true for integers, but the VM uses pointer-tagging to allow a specific range (usually 31 or 63bits) to stay unboxed. (We call these numbers Smis for "Small Integers".)

Doesn't the tag actually take 2 bits? At least looking at the declaration of class Smi in runtime/vm/object.h, it seems to be that case. That gives you 4 possible tags, one needs to be used for heap-allocated objects and one is for Smis. The remaining two remain unused?
I think the name "kBits" is badly chosen...
As you can see in the following lines kMaxValue = 1 << kBits - 1 and kMinValue = -(1 << kBits) which gives a range of kBits + 1.
Also look at kSmiTagShift (raw_object.h: 113) which is set to 1.
// florian

LT
Reply all
Reply to author
Forward
0 new messages