3

I am writing a stateful web application in PHP in which the state potentially contains lots of objects. At this moment, I am keeping all these objects in $_SESSION and have them serialised at the end of the request. This is a bit of a pain, because serialising the entire session takes seconds, and unserialising it takes even more time.

I wanted to try APC, because I hoped that the objects are then just being memcopied, instead of serialised. Alas, it seems that if I feed apc_store($object) an object, it seems to serialise it anyway before passing it to another process. (The story goes that primitive values and arrays are being memcopied without serialisation, but that is not a relevant solution for me, since my session objects have a lot of (circular) references.)

My question: Is there a known way of keeping objects persistent in PHP without having to serialise them after every request? I've heard rumours that the HipHop interpreter can help with this, but I haven't found any working examples on the net. Can somebody tell me if it is possible in PHP at all?

(I know, I should be writing this project in Java or another language that supports persistent instances without a TCP connection)

Pelle
  • 6,423
  • 4
  • 33
  • 50
  • 6
    If serializing your $_SESSION takes seconds, you have way too much stored in $_SESSION. Use a proper datastore (like a database), and store/retrieve only the portions of the "session" that you need. – Frank Farmer Jul 28 '11 at 18:03
  • 1
    There is no global native in-memory storage for PHP. If you need that, you indeed need another language (Java for instance). Or clean up your session as suggested. Quite possibly you can save a lot of time by removing the circular references (if that's what PHP has problems with) in a `__sleep` method, storing instead a way to restore them on `__wakeup`. – Wrikken Jul 28 '11 at 18:16
  • 2
    @AlienWebguy: also serialized strings. – Wrikken Jul 28 '11 at 18:21
  • In my statefull web application I offload most of my objects to a xml storage that's act as clever configuration xml, too. Therefore I don't need to serialize too many objects but only a subset (state) of my xml file. This is still a server-side approach but you can try to offload the serialization to the client? But maybe you serialize too much objects? And why not store the object in a file at run-time? – Micromega Jul 31 '11 at 11:30

1 Answers1

3

Whenever you need to store (freeze) an object, it needs to be serialized. That's independent to the storage (APC, session files, database etc.), it's because the script process will terminate and next time it starts, the objects need to come to life again.

So things can not be kept in a "run-state", objects will always be serialized to be stored.

It's known that PHP serialization is not the fastest. There are alternative implementations to it, you can use, for example igbinary PHP extension. It offers a serialize /deserialize function next to transparent session handling. Maybe this is actually helpful for your scenario.

In any case: The more you store inside the session, the more you need to un-freeze or wake-up at the beginning of the request, the more time it will take.

Related: What is the php_binary serialization handler?

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836
  • 1
    Not true, when APC is running in mod_php mode, an Apache process is spawned and persisted that stores all APC data. When a PHP script dies, it copies the data stored with `apc_store` to the other process, which makes it available for the next request. Strictly spoken, there is no need for serialisation here, PHP should (theoretically) be able to mem-copy an object's memory. but for some reason they decided to do it anyway. – Pelle Jul 31 '11 at 15:31
  • @Pelle ten Cate: So your "Not true" argument is just another hard-fact example *for* serialization? Which reason(s) can you imagine why serialization is used in APC? – hakre Aug 01 '11 at 01:21
  • Serializing to standard formats (e.g. json) can be relatively slow, but for simple structures serializing to another PHP file using `var_export` can be very fast. In this way, it does persist across requests a little bit, because opcache can store a parsed representation of that file. e.g. put `' – Josh from Qaribou Oct 22 '19 at 21:39