I have a fair number of items in a session, and I am wondering if there are any advantages to copying the session variable to a normal php variable (in order to close the session file as quickly as possible) and then do any calls from there. Something like:

$copiedSession = $_SESSION;
if (isset($copiedSessionData['foo'])){
  1. I am looking for ways to test performance on this.
  2. Does it make sense to do so?
  3. Is there a break-even point where it helps / doesn't help?
  • Why the downvote? I can't learn from a mistake unless I know what is it. Thanks. – Cymbals Apr 2 '13 at 18:47

My PHP experience is very rusty, but in the example you provided, aren't you just creating a reference variable pointing to the session? In other words,

$copiedSession = $_SESSION;

doesn't give you a "copy" of the session, but just a second variable from which you can reach it.

You want a deep copy.

foreach ($_SESSION as $key => $value)
    $copiedSession[$key] = $value

I think that is what you want.

As for better performance, I suspect not. It depends on whether $_SESSION is a pure associative array or if it is a special file-backed map that reads/writes through in real-time.

Judging by the PHP manual's SessionHandler interface, whose read/write methods take a session id and session array, I suspect that $_SESSION is a plain old array which comes from SessionHandler::read and gets passed back to SessionHandler::write.

However, to be absolutely sure, the best way is simply to test it.

You could do reading and writing in a loop say 10,000 times against the $_SESSION variable directly and measure min, max and mean execution time. Then do the same thing against a copy (using the code I gave you above, not the reference pointer code) and compare the numbers.

| improve this answer | |
  • Thanks for the response. If you want a reference, it would be $copiedSession = &$_SESSION. php.net/manual/en/language.references.pass.php. I like your idea on the performance testing. If anyone else is looking for ways to implement something like this: stackoverflow.com/questions/535020/… – Cymbals Mar 25 '13 at 19:06
  • 1
    @Cymbals PHP uses copy-on-write, so $copiedSession = $_SESSION isn't actually a copy; they really are pointing at the same data structure until one of them changes - if Brandon is correct and it really is just an array. If it's a special object, I'm not so sure. – Izkata Mar 28 '13 at 0:49

It makes sense to do this when your storage engine is the file system and you have multiple scripts accessing the same session data. (ex: ajax calls). The file containing the session data will be locked until the first script ends execution and this can lead to performance issues. You can find a detailed explanation here

| improve this answer | |
  • That is a good thing to keep in mind. – Cymbals Mar 25 '13 at 19:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.