Skip to content

Releases

1.1.1

Choose a tag to compare

@pkoutsogiannis pkoutsogiannis released this 04 Nov 23:22

UCache 1.1.1, the user cache of WinCache 2.0.0.8 as a PHP extension of its own, for non-thread-safe (NTS) x64 PHP on Windows.

Writes are faster, above all when several processes write at once, which now store about four times as many small values a second, and every entry takes one block of shared memory, 40 to 200 bytes less than in 1.1.0.

  • Several processes writing at once: a ucache_set() or ucache_add() of a value whose entry takes at most ucache.lockedcopy bytes with its key (2048 by default) now copies the value into the cache under the cache lock, with its insert, and frees the entry it replaces in the same hold of the allocator's lock, so that it takes each of the two locks that the processes of a pool share once, where 1.1.0 took the allocator's lock again for each block it allocated or freed (4 to 10 locks for a set that replaced an entry), and processes that wrote at once queued at each of them. With 2 to 6 processes writing at once on PHP 7.2, aggregate ucache_set() throughput is 4.3 to 4.5 times 1.1.0's for [1, 2, 3] (about 190,000 sets a second instead of 42,000 to 44,000) and 3.9 to 4.0 times for a small nested array (about 170,000 instead of 42,000 to 44,000), and 2.5 to 3.2 and 4.5 to 8.2 times WinCache 2.0.0.8's; one process makes 3.7 times as many sets of [1, 2, 3] (604,000 a second instead of 163,000) and 3.4 times as many of the nested array. A larger value is still copied in before the cache lock is taken, so that no process waits for its copy.
  • Reads beside busy writers: the writers now hold the cache lock while they copy their values in, and make four times as many writes, so a process that only reads while others write small values back to back waits longer for the lock: beside 4 such writers on PHP 7.2, its ucache_get() took 23 microseconds at the median and 63 at the 99th percentile instead of 1 and 11, and it made 40,500 reads a second instead of 560,000 (WinCache 2.0.0.8: 61 and 151 microseconds, 15,600 reads a second). Mixes of reads and writes such as those of web requests are not slowed down: with 5% or 20% of writes on shared keys, 1 to 6 processes made as many calls a second as with 1.1.0, or up to 1.9 times as many.
  • ucache.lockedcopy: a new setting, the largest entry, in bytes, whose value a set or add copies under the cache lock, 0 to 65536 (2048 by default). With 0, every value is copied before the lock is taken: beside 4 processes writing [1, 2, 3] back to back on PHP 7.2, a process that only read waited 21 microseconds at the median and 36 at the 99th percentile instead of 23 and 63, while the writers made about as many sets; without a reader, 1, 4 or 6 writers made 26 to 27% fewer sets (17 to 22% for a small nested array), and 2 as many, still about 3.5 times 1.1.0's with 4 writers. A value copied under the lock is always one block, so a higher value makes larger arrays need a free block of their whole size in a fragmented cache. Each process reads the setting when it starts, and it does not change how the cache is laid out: a recycle of the pool applies a new value.
  • One block per entry: every entry is now one block of shared memory, which holds the entry, its key, its zval and every part of its value, and is freed with one free whatever its size, where 1.1.0 took 2 blocks for an integer, 3 for a string and 4 for an array. An entry takes 40 bytes less for an integer, 80 for a string and 200 for an array (PHP 7.2): the smallest entry 128 bytes instead of 168, an integer with a key of 40 characters 168 instead of 208, a row of 14 short strings 1,880 bytes instead of 2,080. 500,000 entries, 6% of them rows of 14 strings serialized to 1.6 KB and the rest integers and short strings, take 130.3 MB instead of 150.7 MB, and a full 512 MB cache of small entries holds 24% to 29% more of them. ucache_meminfo() counts one used block per entry; value_size is unchanged. Before PHP 8.1, an array that array_merge() and the like make of $GLOBALS, and an array larger than ucache.lockedcopy (2 KB by default) that no free block holds whole, still go in pieces.
  • Faster writes in one process: a ucache_set() that replaces an entry takes 1.3 microseconds instead of 3.6 for an integer, 1.5 instead of 5.9 for a small array, 1.9 instead of 6.4 for a row of 14 strings and 3.1 instead of 5.9 for a string of 64 KB, and a delete followed by an add 2.8 instead of 3.9 (PHP 7.2; 8.4 alike); filling a cache with 500,000 integers, short strings and serialized rows took 2.55 microseconds per set instead of 3.65. Deleting every second one of 100,000 entries takes 0.06 s instead of 0.09, and with 125,000 free blocks too small for them, 2000 sets of a 1000-byte string take 3.9 ms instead of 7.1. Reads are as fast as before.
  • ucache_clear(), full passes and mass expiries: an entry is freed with one free, so a clear of 3.0 million entries in 512 MB takes 1.6 s instead of 2.0 s (PHP 7.2), and after a mass expiry the steps on writes remove about 10,000 entries a second instead of 6,000 (500,000 entries in 128 MB, ucache.gc.threshold=50); but a full cache holds more entries: a full 512 MB cache of entries with short keys clears in 2.2 s instead of 2.0 s, and a full pass of a full 128 MB cache of small entries that removes nothing takes 31 ms instead of 21 (90 instead of 71 with ucache.bucketbytes=4096).
  • A value larger than the empty cache: the size of a value is now counted exactly before it is copied, as the block of its entry, so a value that does not fit in the empty cache fails at once, before it takes any shared memory, however little it is over; 1.1.0 left the headers of its blocks out of the count (80 bytes for a string, more for an array), and a value over by less than that ran the full pass of a full cache, which with ucache.gc.evict evicted every entry, before the write failed. A string that fills an empty 8 MB cache can be 80 bytes longer (8,253,839 bytes instead of 8,253,759; PHP 7.2).
  • Expired entries and refused sets: ucache_set() now looks its key up only once it gets as far as storing its value, so that a set takes the cache lock once: a set that fails before (a value that cannot be stored, memory_limit, a value larger than the empty cache) no longer removes the expired entry of its key or the other expired entries of its hash bucket, which a later lookup, a step or a full pass removes. ucache_add() still looks its key up first, and fails before it serializes the objects of its value when the key exists.
  • A process killed while it writes: a process killed while it copies a small value in holds the cache lock and the allocator's lock, so the kill counts twice in total_abandoned_lock_count, and up to 34 entries stay lost until every process of the pool has exited (the 32 waiting to be freed, the entry being replaced and the one being stored), instead of 33.

Each zip holds the DLL for one PHP version, which loads in any NTS x64 build of that version from windows.php.net, whatever the patch release. There is no thread-safe (ZTS) build.

To install it, unzip the zip of your PHP version, rename the DLL in it to php_ucache.dll, copy it into the ext folder of PHP and add extension=php_ucache.dll to php.ini. Settings and functions: see the README.

With PIE: pie install pkoutsogiannis/ucache:1.1.1.

Built from 78b42a7 with the PHP 7.2.34, 7.3.33, 7.4.33, 8.0.30, 8.1.34, 8.2.35, 8.3.36, 8.4.27 and 8.5.12 devel packs.

1.1.0

Choose a tag to compare

@pkoutsogiannis pkoutsogiannis released this 04 Nov 23:21

UCache 1.1.0, the user cache of WinCache 2.0.0.8 as a PHP extension of its own, for non-thread-safe (NTS) x64 PHP on Windows.

Lookups stay fast as the cache fills, processes that IIS kills or starts at any moment no longer break the cache, ucache_clear() no longer holds up the pool and full caches hold it up for much less time, stored arrays take much less memory, and the settings and pool names are read as written.

  • More hash buckets: the hash table of the cache has one bucket per 512 bytes of ucache.size (16381 for the default 8 MB, 1048573 for 512 MB), instead of one per 32 to 64 KB in 1.0.1 (197 for 8 MB, 12853 for 512 MB). Its chains hold 2 to 3 entries on average even in a cache full of the smallest entries, where they could hold hundreds. With 500,000 entries of 40-character keys in 128 MB, ucache_get() takes about 1 microsecond, found or not, instead of 9.4 for a key found and 18.3 for a missing one; with 500,000 entries of integers, short strings and serialized rows in 512 MB, 1.15 instead of 3.41 for a key found and 1.00 instead of 5.92 for a missing one (PHP 7.2).
  • ucache.bucketbytes: a new setting sets the size of the hash table: one bucket per that many bytes of ucache.size, 64 to 65536, 512 by default (the table above). ucache_info() reports the buckets of the cache as hash_bucket_count, and ucache.php shows them next to the setting. As with ucache.size, only the process that creates the cache uses it, and a cache keeps its table for as long as it lives. Around the default it moves little memory either way, and larger buckets soon slow down the lookups of small entries: in a 128 MB cache filled with small entries (PHP 7.2), 128 stored 4.8% fewer entries and read them in 1.07 microseconds instead of 1.19, 4096 stored 1.4% more and read them in 2.0, and 65536 1.6% more in 16.9, and the full pass of the full cache took 71 ms instead of 21 at 4096 and 65536; with arrays of 14 strings, reads took 1.68, 1.68, 1.78 and 2.26 microseconds. A value outside 64 to 65536 is clamped, and one that is not a number of bytes gives the default, each with a startup warning.
  • Long keys: keys longer than 260 characters are hashed in full. 1.0.1 put all of them in the same bucket, so every such key stored slowed down the lookups of the others: with 5,000 keys of 300 characters, ucache_get() takes 1.4 microseconds instead of 11.1 for a key found, and 1.2 instead of 21.9 for a missing one (PHP 7.2).
  • A process killed in the middle of an update: a process that ends while it holds the cache lock or the allocator's lock (an IIS FastCGI activityTimeout or requestTimeout, a stopped or recycled pool, a crash, or memory_limit on PHP builds that end the process there) no longer leaves the shared memory corrupt for every process of the pool. Each update of a hash chain and the count of entries, of a ucache_clear(), of the lists of free blocks, the block headers and the counts of the allocator now first saves what it changes in a log in the shared memory, and the next process to take the lock puts it back, so that the update did not happen: an entry being removed stays, an entry being added is left out (its memory is lost), a clear does not clear, a block being allocated stays free, and a block being freed stays in use, lost with the rest of its entry. In 1.0.1 the processes went on with the half-done update: depending on where the process ended, the next process crashed, every later ucache_set() failed and every read missed, a hash chain listed one entry twice and led to freed memory, or the counts of entries and blocks drifted.
  • total_abandoned_lock_count: ucache_info() has a new key, total_abandoned_lock_count: the times since the cache was created that a process found the cache lock or the allocator's lock abandoned (the process that held it ended without releasing it), and ucache.php shows it on its Scavenged entries tile. On the CVE-patched PHP 7.2, which ends the process at memory_limit, a ucache_get() that reaches memory_limit while it copies a value out is one of them (nothing is undone then: a copy out changes nothing).
  • Starting while the cache is created or closed: a process that started while the last process of its pool was closing the cache, or after a process died while it created the cache or the pool's table of caches, could crash on its first call with a key (a division by zero, exit code 0xC0000094), and so could every process that opened the cache while it ran; or it initialized the memory allocator again under the entries of the cache, after which entries were lost or calls crashed (0xC0000005); or it ran without a cache, every function returning false, as did every process of the pool that started while it ran. Whether the shared memory must be initialized is now decided once, under the init mutex of the cache (of the table, for the table), from whether the memory already existed and from a mark that the process that initializes it writes last, so such a process gets a working cache, and one that starts while the last process is closing the cache keeps its entries instead of starting a new cache. One named mutex fewer is created per cache (..._ALLOC_INIT).
  • ucache.size while the last old process ends: a process that started while the last process with another ucache.size was closing the cache could crash (a larger size) or initialize the allocator again under the entries without a word (a smaller size); it now uses that cache at the size it has, as the processes that start while the cache is in use already did. After changing ucache.size, stop the pool and start it again once its php-cgi processes have exited.
  • ucache.filemapdir after a killed process: a process that maps the file that a killed process left behind now initializes it as a new cache; 1.0.1 reopened the entries of that dead cache, which could be half written, when another process still had the init mutex of the cache open (otherwise it deleted the file).
  • A process killed while it records the cache: a process killed while it added the cache to the pool's table of caches could leave an entry without a name or size there, and every process of the pool that started while the table was in use then ran without a cache; an entry is now published only once it is complete, and one found without a name or size is written again.
  • ucache_clear() no longer holds up the pool: the cache is now emptied at once for every process, under the cache lock, and its entries are then freed in batches with the lock released between them, so the other processes go on reading and writing: with a full cache of 512 MB (2.5 million entries of about 200 bytes, PHP 7.2), a read in another process waited at most 0.6 ms during the clear, where it waited the whole clear (about 4 to 4.5 s) in 1.0.1, and a write about 0.1 s at most: it first helps free the entries for up to that long, so that few values go in the holes of the entries being freed, where they would keep the free memory in small pieces (with 4 processes writing during the clear of a full 512 MB cache, a read waited about 4 ms at most, and the clear left about 70 free blocks, in which a value of 8 MB fitted); a write that needs more memory than the freed entries have yet joined into fails after about 0.12 s (7 of 27 writes of 1000 bytes made during such a clear did); the clear itself took 1.5 to 2.1 s instead of about 4 to 4.5 s. A ucache_clear() while another process is still freeing the entries of an earlier one now returns at once (under 0.1 ms), where 1.0.1 waited for the lock until the earlier clear had ended and then went through the table again, and a process killed during a clear (an IIS FastCGI timeout) now leaves the cache cleared, where 1.0.1 left the entries it had not reached readable; what it had not freed is freed by writes, the ends of requests (while ucache.gc.interval > 0), lookups, steps or the next ucache_clear().
  • Stored arrays take the memory they hold: before copying an array, ucache_set() and ucache_add() now count what its tables and strings take, each part once however often it occurs, and put them in one block of that size, instead of in chunks of 512 bytes to 4 KB that small tables left mostly empty: a row of 14 short strings takes 2,080 bytes instead of 6,400, an array of 8 short strings 1,272 instead of 5,768, and 500,000 entries in 512 MB, 6% of them rows of 14 strings (about 1.6 KB serialized) stored as arrays and the rest integers and short strings, took 185.8 MB instead of 255.4 (PHP 7.2). Storing an array is faster too (a row of 14 short strings: 6.6 microseconds instead of 17.5), several processes that store arrays at once take turns on the allocator's lock for fewer allocations, and deleting, replacing or clearing an array frees a few blocks (an array of 100,000 small arrays: in under 0.1 ms). Reading is as fast as before.
  • A fragmented cache stays fast: free blocks are now kept in lists by size, so storing a value takes the same time however many free blocks the cache holds (unless many of them are in its list and a little too small for it, while the lists of larger sizes are empty): with 250,000 free blocks too small for it, a ucache_set() of a 1000-byte string takes about 4 microseconds, where 1.0.1 went along a single list of all the free blocks, in address order, to the first one large enough. A block is no longer split when the rest could not hold even the smallest block the cache asks for (16 bytes): 1.0.1 left free blocks of 8 bytes that nothing could use.
  • A full cache with ucache.gc.evict: a value larger than the free memory, in a cache with at least ucache.gc.evict percent free and nothing expired, now makes the full pass evict and is stored, where 1.0.1 evicted nothing, failed the write and made every process skip the full pass for a second or more. A value that takes, counted before it is copied, more than the empty cache holds (8,253,952 bytes for 8 M...
Read more

1.0.1

Choose a tag to compare

@pkoutsogiannis pkoutsogiannis released this 04 Nov 23:21

UCache 1.0.1, the user cache of WinCache 2.0.0.8 as a PHP extension of its own, for non-thread-safe (NTS) x64 PHP on Windows.

PHP 7.3 to 8.5, a scavenger that runs at the end of requests and can evict, values stored without holding the cache lock, and fixes of many bugs that 1.0.0 kept from WinCache 2.0.0.8.

  • PHP 7.3, 7.4, 8.0, 8.1, 8.2, 8.3, 8.4 and 8.5, each with a DLL of its own, next to 7.2. On PHP 8 the optional parameters have their defaults in arginfo, so named arguments work: ucache_set($values, ttl: 60), ucache_info(key: $key).

  • A new scavenger, built on the scavenger of Datasteam's 2018-2020 fork of WinCache 2.0.0.8 and on its gc settings, ucache.gc.threshold, ucache.gc.interval, ucache.gc.step and ucache.gc.ttl, but run at the end of requests rather than by the fork's wincache_ucache_gc(), which ucache does not have: once ucache.gc.interval seconds (10 by default; 0 turns this off) have passed since the last step in any process of the pool, the next request to end runs a step, which goes through ucache.gc.step hash buckets (0, the default, sizes the steps so that they go through the whole table in about 5 minutes while requests keep coming) and stops after about 1 ms under the cache lock. A step removes the expired entries and, with ucache.gc.ttl, the entries unused for that many seconds, whatever their ttl; with ucache.gc.threshold, writes run steps too while free memory is at or below that percentage of the cache, and the steps at the end of requests run only then. 1.0.0 ran a step of 16 buckets inside a write at most every 10 seconds, so a cache that was only read was never scavenged, and it removed an entry once its ttl had passed since its last use instead of since it was stored.

  • A full cache, and expired entries on contact: a ucache_set() or ucache_add() that runs out of memory runs a full pass over the table, which removes the expired entries and, with ucache.gc.evict, evicts the entries unused for the longest time until that percentage of the cache is free, and then tries again, where 1.0.0 failed the write. Every call that looks up a key removes the expired entries it passes, so storing a key again after it expired leaves one entry, where 1.0.0 left two, and deleting an expired key removes it. ucache_info() reports what the scavenger removed and evicted and when it last ran (last_scavenge_seconds, last_scavenge_count, total_scavenged_count and total_evicted_count).

  • Values stored without the cache lock: ucache_set() and ucache_add() serialize the objects of a value first and then build its entry in shared memory without holding the cache lock, and ucache_get() unserializes objects once it has released it, so __sleep(), __wakeup(), __serialize(), __unserialize() and the methods of Serializable no longer run under the lock, readers no longer wait while a value is copied in, and the cache functions work inside Serializable::serialize() and Serializable::unserialize(), where 1.0.0 corrupted both the nested value and the enclosing object. A fatal error during a call (memory_limit while ucache_get() copies a big value, for example) no longer leaves the cache lock held, which made every other process of the pool wait until the failing one exited. The cost: with several processes writing at once, aggregate ucache_set() throughput is about 2 to 2.7 times lower than in 1.0.0 with 2 to 6 writers (PHP 7.2), as concurrent copies take turns on the allocator's lock for each allocation; a single writer is as fast.

  • Fixes of bugs that 1.0.0 kept from WinCache 2.0.0.8, among them: reading an array no longer leaks request memory (about 1.2 KB per read); deleting, replacing or scavenging an entry no longer takes time in proportion to the number of entries, and a large entry is freed after the cache lock has been released, in time linear in its size instead of quadratic; a value nested more than 10,000 levels deep is refused instead of crashing the process; a value whose serialization throws (a Closure, for example) is no longer stored as a broken entry; a fatal error during ucache_set() no longer loses shared memory until every process of the pool has exited; ucache_get() returns false when an object of the value cannot be unserialized, where 1.0.0 put null in the object's place; integer keys in the array forms of ucache_get(), ucache_set() and ucache_add(), and large ttls, are no longer cut to 32 bits; ucache_get() returns the numeric keys of an array of keys as integer keys; $success no longer leaks the value that its variable held; and $GLOBALS can be stored.

  • ucache.prefix sets the prefix of the function names, in place of ucache_, for code that already defines functions of these names (a wrapper around wincache_ucache_*(), for example).

  • Other changes: ucache_info() takes offset and limit, which list a part of the entries without copying the others; every function checks its arguments first, so invalid arguments throw a TypeError or an ArgumentCountError (a warning on PHP 7 without strict_types) even with the cache off, where 1.0.0 returned false silently; ini_get() and phpinfo() show the whole ucache.namesalt, of which only the first 8 characters are used, as before; and ucache.localheap, a debugging setting with which no value could be read back, is removed.

  • ucache.php, the statistics page, in the source code archive, is rewritten: it shows the settings, the memory use, the hit and miss counts, what the scavenger removed and evicted and when it last ran, the entries 250 per page (copying only those) and the value stored under a key; it escapes everything it prints, clears the cache with a token-protected form instead of a link, refuses to be shown in a frame, works on PHP 8 and no longer needs the GD extension. With USE_AUTHENTICATION at 0 it now shows the values too, so anyone who can reach it can read the cache, but it still offers no Clear All.

  • build-all.bat: builds and tests php_ucache.dll for every PHP version, one after the other, and lists the versions that passed and failed (build-all.bat install installs each version whose tests passed).

Each zip holds the DLL for one PHP version, which loads in any NTS x64 build of that version from windows.php.net, whatever the patch release. There is no thread-safe (ZTS) build.

To install it, unzip the zip of your PHP version, rename the DLL in it to php_ucache.dll, copy it into the ext folder of PHP and add extension=php_ucache.dll to php.ini. Settings and functions: see the README.

Built from f75458e with the PHP 7.2.34, 7.3.33, 7.4.33, 8.0.30, 8.1.34, 8.2.35, 8.3.36, 8.4.27 and 8.5.12 devel packs.

1.0.0

Choose a tag to compare

@pkoutsogiannis pkoutsogiannis released this 04 Nov 23:20

UCache 1.0.0, the user cache of WinCache 2.0.0.8 as a PHP extension of its own, for non-thread-safe (NTS) x64 PHP on Windows.

The first release, for PHP 7.2 only (WinCache 2.0.0.8's sources do not compile for 7.3 and later). It keeps the API and the behaviour of WinCache's user cache under new names, and fixes three memory-safety bugs.

  • Functions: ucache_get(), ucache_set(), ucache_add(), ucache_delete(), ucache_clear(), ucache_exists(), ucache_info(), ucache_meminfo(), ucache_inc(), ucache_dec() and ucache_cas(), in place of wincache_ucache_*(), with the same arguments and return values. Debug builds also have ucache_lasterror() and ucache_runtests(), in place of wincache_ucache_lasterror() and wincache_runtests().
  • Settings: ucache.enabled and ucache.size (5 to 930 MB, 8 by default) in place of wincache.ucenabled and wincache.ucachesize, and ucache.enablecli, ucache.namesalt, ucache.apppoolid, ucache.filemapdir, ucache.debuglevel and ucache.localheap (a debugging setting: leave it at 0) in place of the wincache.* settings of the same names. As with WinCache, the processes of one IIS application pool, or else those with the same parent process, share a cache; ucache does not see the data that WinCache stores.
  • Left out: the file cache and the resolve-path cache, with their file change notification (wincache_fcache_*(), wincache_rplist_*(), wincache_fcnotify_*(), wincache_refresh_if_changed() and the settings wincache.fcenabled, fcachesize, maxfilesize, filecount, chkinterval, ttlmax, ignorelist, fcenabledfilter and fcndetect), the rerouting of PHP's file functions (wincache.reroute_enabled), the session handler (session.save_handler = wincache, wincache.scachesize, wincache_scache_*()), wincache_lock() and wincache_unlock(), and ETW tracing.
  • The scavenger is WinCache's: every ucache_set(), ucache_add(), ucache_delete(), ucache_inc(), ucache_dec() and ucache_cas() runs a step at most every 10 seconds, which looks at 16 hash buckets and removes their entries whose ttl has passed since they were last used. Expired entries can no longer be read, but keep their memory until a step removes them; a cache that is only read is never scavenged, and a write that finds the cache full fails. There are no settings for it.
  • Three memory-safety fixes: ucache_set() and ucache_add() no longer write their ttl, an 8-byte integer, into a 4-byte variable, which overwrote the stack next to it; ucache.size is no longer written as an 8-byte value into a 4-byte field and read back as a 32-bit unsigned number (with WinCache 2.0.0.8, a negative size gave 930 MB and 4G gave 5 MB): it is read whole, then clamped to 5 to 930 MB; and ucache_delete() with an array of keys no longer turns the integer elements of the caller's array into strings in place, which could crash a FastCGI worker running opcache.
  • ucache.php, the statistics page, in the source code archive: WinCache's wincache.php with only its Summary and User Cache pages, which show the settings, the memory use, the hit and miss counts and the entries, and, with authentication on, the value stored under a key and a Clear All link. It refuses to work while its PASSWORD is the default, ucache, and its charts need the GD extension.

Each zip holds the DLL for one PHP version, which loads in any NTS x64 build of that version from windows.php.net, whatever the patch release. There is no thread-safe (ZTS) build.

To install it, unzip the zip of your PHP version, rename the DLL in it to php_ucache.dll, copy it into the ext folder of PHP and add extension=php_ucache.dll to php.ini. Settings and functions: see the README.

Built from 7567978 with the PHP 7.2.34 devel packs.