Page 1 of 1

Preloading PHP Objects to optimize on performance

Posted: Sun Dec 28, 2008 11:24 am
by sunilbhatia79
Hi All,

Has anyone done any research on how could PHP objects could be preloaded so that execution optimization could be achieved.

For example, I could have a Constants class that would define all the constants that I need in the project. I would just want to load that once so that other classes could use them as and when required.

The problem that I face is that everytime I have to include the file - which makes in compile and execute - which means that after the script finishes its execution - the memory is cleared.

Its the same case with Static classes.

I am sure that there could be a framework or a work around to have these objects preloaded to achieve execution optimization.

Can someone please help?

Regards,
Suniil
http://www.sunilb.com - PHP5 Tutorials

Re: Preloading PHP Objects to optimize on performance

Posted: Sun Dec 28, 2008 2:07 pm
by sergio-pro
Hi

There is a Shared Memory module that allows you to work with memory http://www.phpdig.net/ref/rn57.html
Im not sure if it really will increase performance, if you store some data in memory this way.

If php pasring/compiling is too slow for your task - then:
Dont use PHP, use Java ;)

Re: Preloading PHP Objects to optimize on performance

Posted: Sun Dec 28, 2008 4:23 pm
by josh
Yep, aggressive loading ( "preloading" ) vs lazy loading ("auto loading"), you could also employ lazy loading and then sprinkle aggressive loading over your cake, before you ever so elegantly "eat it", too.

Re: Preloading PHP Objects to optimize on performance

Posted: Sun Dec 28, 2008 10:33 pm
by sunilbhatia79
Thanks guys.

I have been through the Share Memory Module on phpdig. Need to study more.

My question however is that even if I create data in Shared Memory - wont it get destroyed once my PHP script completes its execution?

I am looking at a functionality that Java Containers provide - where the static classes are loaded in memory and always remain there till the time the container is not stopped.

A little bit more guidance will help me a lot!

Regards,
Suniil

Re: Preloading PHP Objects to optimize on performance

Posted: Sun Dec 28, 2008 10:35 pm
by Ambush Commander
Really, the best you can do with PHP is caching the opcodes from files using APC. The great thing about PHP is that you lose all state between results! It means you don't get any odd effects from old requests wreaking havoc on your newest request.

You can try the PHP module for Apache, which keeps the PHP interpreter around between requests. It won't preserve class definitions, but it saves the background machinery.

Re: Preloading PHP Objects to optimize on performance

Posted: Mon Dec 29, 2008 9:37 pm
by Christopher
sunilbhatia79 wrote:The problem that I face is that everytime I have to include the file - which makes in compile and execute - which means that after the script finishes its execution - the memory is cleared.

Its the same case with Static classes.
That is exactly how PHP and Apache are supposed to work. I should ask the obvious question: Do you actually have a performance problem? If not then you should not be searching for anti-PHP techniques in PHP.

Re: Preloading PHP Objects to optimize on performance

Posted: Tue Dec 30, 2008 12:46 am
by sunilbhatia79
arborint wrote:
sunilbhatia79 wrote:The problem that I face is that everytime I have to include the file - which makes in compile and execute - which means that after the script finishes its execution - the memory is cleared.

Its the same case with Static classes.
That is exactly how PHP and Apache are supposed to work. I should ask the obvious question: Do you actually have a performance problem? If not then you should not be searching for anti-PHP techniques in PHP.
I don't have a performance issue as of now. But was wondering how I could help improve performance while building an Enterprise level application. I love PHP and that's the reason why I am investing efforts on understanding how this could be handled.

The project my friend and I are trying to build contain a list of Keywords - which we need preloaded in memory always (after reading them from DB). This means that whenever the script runs - we should have a handle to the list of keywords object - so that I would not have to read DB and load the list for each request.

Regards,
Suniil

Re: Preloading PHP Objects to optimize on performance

Posted: Tue Dec 30, 2008 2:54 am
by Christopher
sunilbhatia79 wrote:I don't have a performance issue as of now. But was wondering how I could help improve performance while building an Enterprise level application. I love PHP and that's the reason why I am investing efforts on understanding how this could be handled.
Since you don't have a problem now, you are wasting your time investigating because you don't know what the performance problems might be.
sunilbhatia79 wrote:The project my friend and I are trying to build contain a list of Keywords - which we need preloaded in memory always (after reading them from DB). This means that whenever the script runs - we should have a handle to the list of keywords object - so that I would not have to read DB and load the list for each request.
How big is the list? I would wrap the Keywords list in an object and put them somewhere easy for not -- like in the DB. Then when this menacing performance problems arrives you can use a faster subsystem to store the list. You should be focused now on getting the design right.

Re: Preloading PHP Objects to optimize on performance

Posted: Tue Dec 30, 2008 3:05 am
by sunilbhatia79
Ok. For now will focus on the design.

One last question :)

What type of faster sub-system would you use if you face issues with performance. Asking this now so that I could do a bit of research in free time.

Thanks
Suniil

Re: Preloading PHP Objects to optimize on performance

Posted: Tue Dec 30, 2008 3:43 am
by Christopher
A faster database server would be first because that's really easy, then probably Memcache.