Welcome! Log In Create A New Profile

Advanced

[PHP-DEV] Traits and memory size

Posted by Ben Schmidt 
Ben Schmidt
[PHP-DEV] Traits and memory size
April 10, 2012 03:40AM
Hi!

Just a quick question about traits.

During the flattening process, is a copy made of the trait code (or
bytecode or whatever)?

So, if I have

trait A {
// a bunch of methods
}
class B {
use A;
}
class C {
use A;
}
class D {
use A;
}

will I have four copies of A because it has been copied into all of the
classes, and thus this aspect of my code will take up four times as much
memory than if I did:

class A {
// a bunch of methods
}
class B extends A {
}
class C extends A {
}
class D extends A {
}

Or is the code shared so the memory use will be roughly the same?

I realise the use cases for traits and inheritance are different, and
often the situation will dictate the design, but sometimes you have
these two options so have to make a design decision, and it would be
nice to know the impact on memory use/code size.

Hope this is a simple question and won't take too much time for anyone
to answer!

Thanks!

Ben.




--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php
Jared Williams
RE: [PHP-DEV] Traits and memory size
April 10, 2012 04:10AM
> -----Original Message-----
> From: Ben Schmidt [mailto:[email protected]]
> Sent: 10 April 2012 02:35
> To: internals@lists.php.net
> Subject: [PHP-DEV] Traits and memory size
>
> Hi!
>
> Just a quick question about traits.
>
> During the flattening process, is a copy made of the trait
> code (or bytecode or whatever)?
>
> So, if I have
>
> trait A {
> // a bunch of methods
> }
> class B {
> use A;
> }
> class C {
> use A;
> }
> class D {
> use A;
> }
>
> will I have four copies of A because it has been copied into
> all of the classes, and thus this aspect of my code will take
> up four times as much memory than if I did:
>
> class A {
> // a bunch of methods
> }
> class B extends A {
> }
> class C extends A {
> }
> class D extends A {
> }
>
> Or is the code shared so the memory use will be roughly the same?
>
> I realise the use cases for traits and inheritance are
> different, and often the situation will dictate the design,
> but sometimes you have these two options so have to make a
> design decision, and it would be nice to know the impact on
> memory use/code size.
>
> Hope this is a simple question and won't take too much time
> for anyone to answer!
>
> Thanks!
>
> Ben.
>

I don't know the answer to your question.

But the decision to use traits or inheritance should be pretty
obvious, and the amount of memory either way uses should be
irrelevant.
If either choice appears to be allocating too much, then it's the Zend
Engine itself that should be looked at, and not rewritting PHP code.

Jared


--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php
Stefan Marr
Re: [PHP-DEV] Traits and memory size
April 10, 2012 09:50AM
Hi Ben:


On 10 Apr 2012, at 03:34, Ben Schmidt wrote:

> During the flattening process, is a copy made of the trait code (or
> bytecode or whatever)?


Thanks to Dmitry, the op_array, i.e., the methods op codes are not copied but shared between the methods.

Thus, there is a certain memory benefit of sharing code in traits, compared to manual copy and past.

Best regards
Stefan


--
Stefan Marr
Software Languages Lab
Vrije Universiteit Brussel
Pleinlaan 2 / B-1050 Brussels / Belgium
http://soft.vub.ac.be/~smarr
Phone: +32 2 629 2974
Fax: +32 2 629 3525


--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php
Ben Schmidt
Re: [PHP-DEV] Traits and memory size
April 10, 2012 02:40PM
On 10/04/12 5:42 PM, Stefan Marr wrote:
> Hi Ben:
> On 10 Apr 2012, at 03:34, Ben Schmidt wrote:
>
>> During the flattening process, is a copy made of the trait code (or
>> bytecode or whatever)?
>
> Thanks to Dmitry, the op_array, i.e., the methods op codes are not
> copied but shared between the methods.
>
> Thus, there is a certain memory benefit of sharing code in traits,
> compared to manual copy and past.

Thanks a lot for the answer, Stefan. That is good news.

Smiles,

Ben.




--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php
Ben Schmidt
Re: [PHP-DEV] Traits and memory size
April 10, 2012 02:50PM
>> During the flattening process, is a copy made of the trait
>> code (or bytecode or whatever)?
....
>> Or is the code shared so the memory use will be roughly the same?
....
>> I realise the use cases for traits and inheritance are
>> different, and often the situation will dictate the design,
>> but sometimes you have these two options so have to make a
>> design decision, and it would be nice to know the impact on
>> memory use/code size.
....
> But the decision to use traits or inheritance should be pretty
> obvious, and the amount of memory either way uses should be
> irrelevant.
> If either choice appears to be allocating too much, then it's the Zend
> Engine itself that should be looked at, and not rewritting PHP code.

Not necessarily. There are design tradeoffs when implementing traits. It
may (or may not) be the case that there is a tradeoff between execution
time and memory use. The implementor may have perfectly good reasons for
favouring, say, execution time, even though due to my environment I may
be more concerned about memory use, and there's no real reason why he
should change, and not me.

Also from a purely practical viewpoint, rewriting PHP code may be more
feasible in the short term. :-)

At any rate, Stefan has confirmed that there isn't code duplication
behind the scenes, so it's not an issue!

Smiles,

Ben.




--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php
Sorry, only registered users may post in this forum.

Click here to login