Preloading PHP Objects to optimize on performance

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
sunilbhatia79
Forum Newbie
Posts: 24
Joined: Sun Nov 11, 2007 9:37 pm
Location: Mumbai, India

Preloading PHP Objects to optimize on performance

Post 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
User avatar
sergio-pro
Forum Commoner
Posts: 88
Joined: Sat Dec 27, 2008 12:26 pm

Re: Preloading PHP Objects to optimize on performance

Post 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 ;)
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Preloading PHP Objects to optimize on performance

Post 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.
sunilbhatia79
Forum Newbie
Posts: 24
Joined: Sun Nov 11, 2007 9:37 pm
Location: Mumbai, India

Re: Preloading PHP Objects to optimize on performance

Post 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
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Re: Preloading PHP Objects to optimize on performance

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Preloading PHP Objects to optimize on performance

Post 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.
(#10850)
sunilbhatia79
Forum Newbie
Posts: 24
Joined: Sun Nov 11, 2007 9:37 pm
Location: Mumbai, India

Re: Preloading PHP Objects to optimize on performance

Post 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
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Preloading PHP Objects to optimize on performance

Post 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.
(#10850)
sunilbhatia79
Forum Newbie
Posts: 24
Joined: Sun Nov 11, 2007 9:37 pm
Location: Mumbai, India

Re: Preloading PHP Objects to optimize on performance

Post 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
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Preloading PHP Objects to optimize on performance

Post by Christopher »

A faster database server would be first because that's really easy, then probably Memcache.
(#10850)
Post Reply