memory consumption

73 views
Skip to first unread message

Don David Alegria

unread,
May 17, 2013, 10:33:01 AM5/17/13
to mi...@dartlang.org
I read that var had a variable memory consumption. What is memory consumption exactly?

What would be
int a = 1 // 64 bit, 32 bit ?
var booly=1  // 1, 2, 4 bytes? equal to num bool=1  or boolean b=true ?
var bits=[0,1,0,0,1,0,1,0]; //   64 bytes .. 8 bytes or ... 1 byte?
var bitmatrix= [[0,1,0,0,1,0,1,0],[0,1,0,0,1,0,1,0]]; // ? bytes
class bitnode {var list=[[0,1,0,0,1,0,1,0],[0,1,0,0,1,0,1,0]];} 3, 20 bytes?

Thanks a lot. 

Florian Loitsch

unread,
May 17, 2013, 11:11:12 AM5/17/13
to General Dart Discussion
On Fri, May 17, 2013 at 4:33 PM, Don David Alegria <dondavi...@gmail.com> wrote:
I read that var had a variable memory consumption. What is memory consumption exactly?
I'm not sure you can say that "var" has a variable memory consumption. Conceptually every variable is a pointer to an object. This includes numbers, booleans and even null.
Since this would be extremely costly VMs generally cheat when they can. In particular they avoid the boxing of integers when they can avoid it. Instead of having a pointer to an object they will use the pointer-address to contain the actual number. Smis (small integers) are integers that are encoded in a pointer.
VMs can use smaller registers if it doesn't modify the behavior of the program, but I'm not aware of any optimization in that direction inside V8 or Dart.

What would be
int a = 1 // 64 bit, 32 bit ?
 1 is a Smi, an integer encoded into the pointer. So this would use up one register.

var booly=1  // 1, 2, 4 bytes? equal to num bool=1  or boolean b=true ?
Same. The static type declaration never has any effect on the dynamic behavior of a program. Even in checked mode it doesn't change the semanctics. It just adds some type-checks.

var bits=[0,1,0,0,1,0,1,0]; //   64 bytes .. 8 bytes or ... 1 byte?
The VM is free to optimize if it sees something like this. By default it is probably a list of 8 Smis.
 
var bitmatrix= [[0,1,0,0,1,0,1,0],[0,1,0,0,1,0,1,0]]; // ? bytes
Same. Two lists of 8 Smis.
 
class bitnode {var list=[[0,1,0,0,1,0,1,0],[0,1,0,0,1,0,1,0]];} 3, 20 bytes?
Same.

Note that some VMs actually do optimize lists when they encounter special types. For example, in V8 lists of doubles are automatically transformed to typed-arrays.
 

Thanks a lot. 

--
For other discussions, see https://groups.google.com/a/dartlang.org/
 
For HOWTO questions, visit http://stackoverflow.com/tags/dart
 
To file a bug report or feature request, go to http://www.dartbug.com/new
 
 



--
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

Don David Alegria

unread,
May 17, 2013, 12:49:40 PM5/17/13
to mi...@dartlang.org
I guessed so... unfortunately. 
Well, not that I'd use it every day, but.. a Bitarray would be nice.

Thanks anyway.

Florian Loitsch

unread,
May 17, 2013, 5:26:08 PM5/17/13
to General Dart Discussion
On Fri, May 17, 2013 at 6:49 PM, Don David Alegria <dondavi...@gmail.com> wrote:
I guessed so... unfortunately. 
Well, not that I'd use it every day, but.. a Bitarray would be nice.
You can use typed lists if you want packed lists. http://api.dartlang.org/docs/releases/latest/dart_typed_data.html

kc

unread,
May 17, 2013, 5:49:11 PM5/17/13
to mi...@dartlang.org


On Friday, May 17, 2013 3:33:01 PM UTC+1, Don David Alegria wrote:
I read that var had a variable memory consumption. What is memory consumption exactly?

What would be
int a = 1 // 64 bit, 32 bit ?


Bob N. make a great point a while back: 

Local variable declarations are the strangest part to me in Dart. Using a type to declare a variable like "int foo" makes sense in C because creating a variable isn't even meaningful without knowing the storage requirements for it, which is determined by the type.

That doesn't apply to Dart in the least. Storage and type are unrelated to each other, especially since the types are completely optional!


----
I think lots of users will think like this - and miss the true dynamic nature of the language. 
And users will be tempted to convert perfectly valid 'var' to explicit types - in the mistaken belief of some performance benefit.

K.
Reply all
Reply to author
Forward
0 new messages