August 11, 2013 Re: Get class size of object | ||||
---|---|---|---|---|
| ||||
Posted in reply to JS | On Sunday, 11 August 2013 at 04:25:21 UTC, JS wrote:
> Given an object, is there a built in way to get the size of the class the object represents?
try this:
Object obj = new Whatever();
auto size = typeid(obj).init.length;
|
August 11, 2013 Re: Get class size of object | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On Sunday, 11 August 2013 at 16:16:26 UTC, bearophile wrote:
> Maxim Fomin:
>
>> GC.sizeOf seems to return size of allocated page which is bigger than needed (for ex. it returns 16 for a new int which is 4).
>
> The object instance contains a pointer to the virtual table, so there's a way to know what object it is. With that runtime information it should be possible to know the static size of the instance.
>
> Bye,
> bearophile
I do not know how you can calculate precisely object size by knowing that GC.sizeOf returns some aligned amount plus knowing something about virtual table. In case of simple objects (like ints) it does not work since int has no virtual table.
Anyway such guessing is useless since D provides feature to get size of class instance (if it is defined) even in comple time like enum S = _get_size ...
|
August 11, 2013 Re: Get class size of object | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | On Sunday, 11 August 2013 at 16:36:53 UTC, Adam D. Ruppe wrote:
> On Sunday, 11 August 2013 at 04:25:21 UTC, JS wrote:
>> Given an object, is there a built in way to get the size of the class the object represents?
>
> try this:
>
> Object obj = new Whatever();
> auto size = typeid(obj).init.length;
This.
|
August 11, 2013 Re: Get class size of object | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | On Sunday, 11 August 2013 at 16:36:53 UTC, Adam D. Ruppe wrote:
> On Sunday, 11 August 2013 at 04:25:21 UTC, JS wrote:
>> Given an object, is there a built in way to get the size of the class the object represents?
>
> try this:
>
> Object obj = new Whatever();
> auto size = typeid(obj).init.length;
Yes, this is answer to the question. By the way, it can be simply
enum S = typeid(Whatever).init.length;
|
August 11, 2013 Re: Get class size of object | ||||
---|---|---|---|---|
| ||||
Posted in reply to Maxim Fomin | On Sunday, 11 August 2013 at 16:40:48 UTC, Maxim Fomin wrote:
> Yes, this is answer to the question. By the way, it can be simply
>
> enum S = typeid(Whatever).init.length;
If exact type is statically known, no ClassInfo is required:
enum S = __traits(classInstanceSize, Whatever);
|
August 11, 2013 Re: Get class size of object | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dicebot | On Sunday, 11 August 2013 at 16:43:09 UTC, Dicebot wrote: > On Sunday, 11 August 2013 at 16:40:48 UTC, Maxim Fomin wrote: >> Yes, this is answer to the question. By the way, it can be simply >> >> enum S = typeid(Whatever).init.length; > > If exact type is statically known, no ClassInfo is required: > > enum S = __traits(classInstanceSize, Whatever); This is essentially what I'm doing BUT I am trying to avoid having to repeat the exact same crap in every class because it is time consuming, verbose, and error prone. For those that care, here is a snippet of what I am doing. For those that are bitches, GTFO. Remember, I'm a troll so everything I say or do here is irrelevant, so I don't even know why you are reading this in the first place. http://dpaste.dzfl.pl/d295bbbb Note that because it is a function call it is extremely expensive compared to a compile time literal. Disassembly shows that the actual size is a constant so the vtabl lookup and a function call is all that is required. I'd like to minimize the overhead as much as possible and, ideally not have to manually add the template mixins to each class(it would be nice if it were automated). I'm trying to reduce the costs of RT lookup as much as possible(as I feel it is not necessary). |
August 11, 2013 Re: Get class size of object | ||||
---|---|---|---|---|
| ||||
Posted in reply to JS | On 08/11/2013 05:28 PM, JS wrote: > On Sunday, 11 August 2013 at 13:40:41 UTC, Timon Gehr wrote: >> On 08/11/2013 06:25 AM, JS wrote: >>> Given an object, is there a built in way to get the size of the class >>> the object represents? >> >> Yes. > > Troll. http://en.wikipedia.org/wiki/Troll_(Internet) |
August 11, 2013 Re: Get class size of object | ||||
---|---|---|---|---|
| ||||
Posted in reply to JS | On Sunday, 11 August 2013 at 17:03:04 UTC, JS wrote:
> For those that care, here is a snippet of what I am doing.
What are you actually going to use the number for? If you are going to write your own New function, you'll need the init anyway, so you aren't really paying more to get the size from it either.
|
August 11, 2013 Re: Get class size of object | ||||
---|---|---|---|---|
| ||||
Posted in reply to Timon Gehr | On Sunday, 11 August 2013 at 17:12:48 UTC, Timon Gehr wrote:
> On 08/11/2013 05:28 PM, JS wrote:
>> On Sunday, 11 August 2013 at 13:40:41 UTC, Timon Gehr wrote:
>>> On 08/11/2013 06:25 AM, JS wrote:
>>>> Given an object, is there a built in way to get the size of the class
>>>> the object represents?
>>>
>>> Yes.
>>
>> Troll.
>
> http://en.wikipedia.org/wiki/Troll_(Internet)
Seriously, do you not have anything better to do with your life? Why do you hang out on my posts if you are the one that's not the troll. I don't want any of your help so why don't you just do us all a favor and ignore any post I make?
|
August 11, 2013 Re: Get class size of object | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | On Sunday, 11 August 2013 at 17:19:21 UTC, Adam D. Ruppe wrote:
> On Sunday, 11 August 2013 at 17:03:04 UTC, JS wrote:
>> For those that care, here is a snippet of what I am doing.
>
> What are you actually going to use the number for? If you are going to write your own New function, you'll need the init anyway, so you aren't really paying more to get the size from it either.
What's the difference between init.length and classInstanceSize? The class instance size must be known at compile time and I don't see why there will be a difference and what little testing I have done shows no difference.
|
Copyright © 1999-2021 by the D Language Foundation