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.
-----Original Message-----
From: Ben Schmidt [mailto:mail_ben_schmidt@yahoo.com.au]
Sent: 10 April 2012 02:35
To: internals@lists.php.net
Subject: [PHP-DEV] Traits and memory sizeHi!
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
Hi Ben:
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
Hi Ben:
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.