digitalmars.D.learn - optional reduction of overhead for classes
- clayasaurus (36/36) Apr 17 2006 With pointer = 4 bytes and float = 4 bytes...
- Tom S (13/22) Apr 17 2006 Well, before allocation, your EntityData doesn't really exist, only a
- clayasaurus (9/26) Apr 17 2006 Thanks for that :) After I allocate it, it says it uses 16 bytes. Since
- Don Clugston (2/32) Apr 18 2006
- xs0 (39/61) Apr 18 2006 I think that there are
With pointer = 4 bytes and float = 4 bytes... ------------------------------------------------ I'm trying to figure out a way I can have a class and give it the option to include less overhead, my idea goes something like this... Entity e = new Entity(); // Independent Entity e = new Entity(DEPENDENT); class Entity { this() { // allocate new resources of (4+4=8 bytes?) data = new EntityData; } this(int type) { assert(type == DEPENDENT); } void draw() { glDraw(data.x, data.y); } void drawAt(x, y) { glDraw(x,y); } EntityData data; // a class } class EntityData { float x, y; } ------------------------------------------------------------------------ My question is, how much memory does EntityData use before allocation (4-bytes?), and how much does it use after allocation (4+4+4 bytes total?). Thanks. ~ Clay
Apr 17 2006
clayasaurus wrote:class EntityData { float x, y; } ------------------------------------------------------------------------ My question is, how much memory does EntityData use before allocation (4-bytes?), and how much does it use after allocation (4+4+4 bytes total?).Well, before allocation, your EntityData doesn't really exist, only a null reference, which takes 4 bytes on 32 bit systems. IIRC, you can check the size of an object thru .classinfo.init.length. If you want to avoid overhead, consider making EntityData a struct, to which you'd store a pointer. -- -----BEGIN GEEK CODE BLOCK----- Version: 3.1 GCS/M d-pu s+: a-->----- C+++$>++++ UL P+ L+ E--- W++ N++ o? K? w++ !O !M V? PS- PE- Y PGP t 5 X? R tv-- b DI- D+ G e>+++ h>++ !r !y ------END GEEK CODE BLOCK------ Tomasz Stachowiak /+ a.k.a. h3r3tic +/
Apr 17 2006
Tom S wrote:clayasaurus wrote:IIRC, you canclass EntityData { float x, y; } ------------------------------------------------------------------------ My question is, how much memory does EntityData use before allocation (4-bytes?), and how much does it use after allocation (4+4+4 bytes total?).Well, before allocation, your EntityData doesn't really exist, only a null reference, which takes 4 bytes on 32 bit systems.check the size of an object thru .classinfo.init.length.Thanks for that :) After I allocate it, it says it uses 16 bytes. Since the size of a float is only four bytes and the null reference is 4 bytes, would that be 4+4+4 = 12? I wonder where the other 4 bytes come from, maybe typeinfo?If you want to avoid overhead, consider making EntityData a struct, to which you'd store a pointer.Ok, you mean like MyStruct *str = new MyStruct? Thanks. ~ Clay
Apr 17 2006
clayasaurus wrote:Tom S wrote:Try two floats, to see if it's just alignment padding.clayasaurus wrote:IIRC, you canclass EntityData { float x, y; } ------------------------------------------------------------------------ My question is, how much memory does EntityData use before allocation (4-bytes?), and how much does it use after allocation (4+4+4 bytes total?).Well, before allocation, your EntityData doesn't really exist, only a null reference, which takes 4 bytes on 32 bit systems.check the size of an object thru .classinfo.init.length.Thanks for that :) After I allocate it, it says it uses 16 bytes. Since the size of a float is only four bytes and the null reference is 4 bytes, would that be 4+4+4 = 12? I wonder where the other 4 bytes come from, maybe typeinfo?If you want to avoid overhead, consider making EntityData a struct, to which you'd store a pointer.Ok, you mean like MyStruct *str = new MyStruct? Thanks. ~ Clay
Apr 18 2006
clayasaurus wrote:Tom S wrote:I think that there are - 4 bytes for pointer to the virtual method table (you inherit from Object, so there are methods) - 4 bytes for pointer to synchronization data (because you could use it, even if you don't) - 8 bytes for two floats The reference to the object costs additional 4 bytes. Assuming the above is everything you need, you could use a struct instead, saving the first 8 bytes.. Finally, I think that the current GC will allocate 1 byte more than strictly needed, so the actual amount of memory used per instance of EntityData will be 32 bytes (for class) or 16 bytes (for struct). Another option would be to include the data in one version of the class, and a pointer to it in another (dependant) version. When you want to share the data, just set the pointer of EntityDependant to data of an Entity instance. struct EntityData { float x,y; } class EntityBase { abstract void draw(); } class Entity : EntityBase { EntityData data; void draw() { glDraw(data.x, data.y); } } class EntityDependant : EntityBase { EntityData *data; void draw() { glDraw(data.x, data.y); } } That way, you have no overhead if you use an Entity, and save 4 bytes (but lose some speed) for each instance of EntityDependant. xs0clayasaurus wrote:IIRC, you canclass EntityData { float x, y; } ------------------------------------------------------------------------ My question is, how much memory does EntityData use before allocation (4-bytes?), and how much does it use after allocation (4+4+4 bytes total?).Well, before allocation, your EntityData doesn't really exist, only a null reference, which takes 4 bytes on 32 bit systems.check the size of an object thru .classinfo.init.length.Thanks for that :) After I allocate it, it says it uses 16 bytes. Since the size of a float is only four bytes and the null reference is 4 bytes, would that be 4+4+4 = 12? I wonder where the other 4 bytes come from, maybe typeinfo?
Apr 18 2006