Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use short ints (16-bit) in PHP?

Background: I have a large 2D array of integers that I need to load into memory in PHP for each Apache request. I want it to take up less memory.

PHP stores ints in PHP_INT_SIZE bytes, which is 32 bits on most systems. All the integers are less than 2^16, which means they could be a short int (eg in C). Am I right in thinking that storing ints as short would take up half the RAM?

Ideally I'd like to be able to do:

$s = (short) 1234; // takes up 2 bytes instead of 4

More info:

  • The array takes up about 100mb of RAM and is generated by including a 30MB var_export() dump
  • The array is written in a cron process. Only the reading needs to be memory efficient (and quick)
  • The only operations I need to do on the integers are comparing all of them (<, >, ===) and then reading a few of them (similar to the Floyd-Warshall algorithm)
  • Reading each value from a DB is way too slow as there are a few hundred million reads per request

Some crazy ideas:

  • Use pack() / unpack() but that would still store the values as 32 bit ints when they were unpacked
  • Store the values as pixels in an image and use PHP's GD library to read them (would this be slow)
  • Use shmop_read() and have the Apache processes share the array
  • Memcached might work but I have no experience with it and I guess it would be many times slower than a native PHP array
  • Learn C++ and write a PHP extension
  • Recompile PHP (or HipHop?) to use 2 bytes for ints
  • Use Igbinary (useful, but will have same problem as pack())
like image 339
dave1010 Avatar asked Sep 07 '10 13:09

dave1010


People also ask

Are ints 16 bits?

The size of a signed int or unsigned int item is the standard size of an integer on a particular machine. For example, in 16-bit operating systems, the int type is usually 16 bits, or 2 bytes. In 32-bit operating systems, the int type is usually 32 bits, or 4 bytes.

What is the range of a 16-bit integer?

A 16-bit integer can store 216 (or 65,536) distinct values. In an unsigned representation, these values are the integers between 0 and 65,535; using two's complement, possible values range from −32,768 to 32,767.

Can int have decimals PHP?

An integer must NOT have a decimal point. An integer can be either positive or negative.

How many bytes integer in PHP?

The size of an int is platform-dependent, although a maximum value of about two billion is the usual value (that's 32 bits signed). 64-bit platforms usually have a maximum value of about 9E18. PHP does not support unsigned ints.


1 Answers

I would not recommend last approach. :-)

For the quick solution, I would pack 2 your integers in 1 PHP integer using this:

$big = $int1 + ($int2<<16);

And uppack as:

$int1 = $big & 65535;
$int2 = ($big>>16) & 65535;

Also, BIG thumbs up for using shared memory. This will make your APP way faster.

like image 146
BarsMonster Avatar answered Nov 05 '22 08:11

BarsMonster