4

I'm experimenting with SplFixedArray. I have some work done with dynamic arrays which i'm trying to convert to more memory efficient fixed arrays (limited RAM to work with).

Reading some PHP documentation, found the function in the Title and proceeded to just apply to an array which is like:

$array[x][y]['field']

(3d array with string as index, impossible in fixed arrays) by doing

$testArray =  SplFixedArray::fromArray(generateArray(256)); 
// generateArray is a function to create the array and set it to zero.

I checked if i could get some memory savings from this versus standard array and no. Replaced the string index with numbers, same amount of ram used (94 mb) to generate the array.

If i use SplFixedArray properly (not converting from an existing array) i low the mem used to 74mb, but i have a lot of functions and rutines which works with the base 3d array and would be a pain in the ass to convert everything to "proper" fixed array. That's why when i read abut SPL::fromArray i jumped on my chair. But with these tests, i found ZERO memory nor speed benefits.

Am i not using it correctly? Is this function just for other type of things?

Thanks!

SolarBear
  • 4,534
  • 4
  • 37
  • 53
Gabriel
  • 5,453
  • 14
  • 63
  • 92
  • 1
    A single 256 element array is simply not big enough to take advantage of SPLFixedArray's more efficient structure. – Matthew Jun 17 '11 at 05:30
  • 1
    SplFixedArray just has some savings on the array keys, not on the values. There is still much PHP zval overhead. The difference to normal PHP arrays is, that it's a pure indexed list, not a dictionary. But speed advantages do not materialize because that feature is hobbled on, the interpreter was optimized for normal arrays/dicts. – mario Jun 17 '11 at 05:32
  • See http://matthewturland.com/2010/05/20/new-spl-features-in-php-5-3/ - the answer is: it depends on what you are doing and how much data you got in there. SplFixedArray can significantly improve performance. – Gordon Jun 17 '11 at 07:51
  • Actually, the array is 3D, total amount of values is 256*256*5. – Gabriel Jun 17 '11 at 15:54
  • note in that case you are creating 1+256*256 SplFixedArray objects, each of which consume the typical PHP overhead. I just tested, and when using a 3D SplFixedArray of that size with integers, memory usage is 55M, but when using a flat 2D SplFixedArray[256 * 256 * 5], memory usage is 19M. So it goes to show that you need to minimize the number of PHP objects if you want to minimize memory usage. – Matthew Jun 17 '11 at 16:04

3 Answers3

8

The short of it is that PHP is not designed to be working with such large data structures in a memory efficient manner. Nothing you do is going to change that. Trying to work PHP within a 256MB VPS is very difficult, especially if you've got a web server and database server.

As I illustrated in your other question, SplFixedArrays use less memory. That's a fact, and you can look up in the PHP source to see how the objects are created. The numbers don't lie.

But it's only one piece of the puzzle... If you are storing lots of big things in the array or are working with other data structures, it's possible that the array isn't the "bottleneck" of memory usage.

Regarding SplFixedArray::fromArray(), you will definitely be adding to your peak usage, because you are now creating two array structures. If you delete the temporary array, you will then be using less memory... but in the interim, you'll be using more.

You'd probably use less peak memory if you just wrote your own function that shifted off an element of the temporary array one by one and added it to the SplFixedArray, as you wouldn't be duplicating your data structure size. (Due to copy-on-write, the actual savings may not be that big.)

Again, some benchmarks of 1024*1024 sized array with 64-bit integers in each slot:

SplFixedArray:            92,914,280
array:                   218,756,976
SplFixedArray::fromArray 227,147,408 peak, 92,915,088 after

So as you see, if you load fromArray, you are using more memory, but after the temporary array is deleted, it's back to the savings. But since the goal is to minimize peak memory usage, using fromArray will be worse than simply using arrays.

Community
  • 1
  • 1
Matthew
  • 47,584
  • 11
  • 86
  • 98
3

My experience is that most of the Spl classes are not real improvements over the native array performance wise. At best the tests prove inconclusive, at worse, they show the Spl libraries to be slower.

The major benefits? They are more reliable and consistent objects than array. SplFixedArray gives you the benefit of knowing that the indexes are ints (numeric indexes, I believe, actually are faster than string indexes), and that you can specifically set the length -- a major bonus if you have no idea how you ended up with some bizarro extra value.

cwallenpoole
  • 79,954
  • 26
  • 128
  • 166
  • 1
    For the curious such as myself, here are a few comparison benchmarks I just found. ([johnciacia.com](http://www.johnciacia.com/2011/02/01/array-vs-splfixedarray/), [blog.shay.co](http://blog.shay.co/phps-native-array-vs-splfixedarray-performance/), and the comments on [this PHP man page](http://php.net/manual/en/class.splfixedarray.php)) – Wiseguy Jun 17 '11 at 05:04
  • @Wiseguy Well, those benchmarks certainly explain the muddled responses I got when I last looked into this. – cwallenpoole Jun 17 '11 at 05:17
0

I ran some tests on it as well. There seems to be no point in using SplFixedArray::fromArray. The memory savings is hardly noticeable.

datasage
  • 19,153
  • 2
  • 48
  • 54