rfc:weak_maps
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
rfc:weak_maps [2019/11/04 18:36] – Fix example nikic | rfc:weak_maps [2020/01/03 09:56] (current) – Implemented nikic | ||
---|---|---|---|
Line 3: | Line 3: | ||
* Author: Nikita Popov < | * Author: Nikita Popov < | ||
* Proposed Version: PHP 8.0 | * Proposed Version: PHP 8.0 | ||
- | * Status: | + | * Status: |
* Implementation: | * Implementation: | ||
===== Introduction ===== | ===== Introduction ===== | ||
- | Weak maps allow creating a map from objects to arbitrary values (similar to SplObjectStorage) without preventing the objects that are used as keys from being garbage collected. If an object key is garbage collected, it will simply be removed from the map. This allows implementing caches and similar structures without causing memory leaks. | + | Weak maps allow creating a map from objects to arbitrary values (similar to SplObjectStorage) without preventing the objects that are used as keys from being garbage collected. If an object key is garbage collected, it will simply be removed from the map. |
In PHP 7.4 first-class support for [[rfc: | In PHP 7.4 first-class support for [[rfc: | ||
+ | |||
+ | The general use case for weak maps is to associate data with individual object instances, without forcing them to stay alive and thus effectively leak memory in long-running processes. For example, a weak map may be used to memoize a computation result: | ||
+ | |||
+ | <PHP> | ||
+ | class FooBar { | ||
+ | private WeakMap $cache; | ||
+ | | ||
+ | public function getSomethingWithCaching(object $obj) { | ||
+ | return $this-> | ||
+ | } | ||
+ | | ||
+ | // ... | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | This will invoke the '' | ||
===== Proposal ===== | ===== Proposal ===== | ||
Line 67: | Line 83: | ||
===== Vote ===== | ===== Vote ===== | ||
- | Add WeakMap class in PHP 8.0? Yes/No. | + | Voting started 2019-12-18 and closes 2020-01-01. |
+ | |||
+ | <doodle title=" | ||
+ | | ||
+ | | ||
+ | </ | ||
+ | |||
+ | ===== Differences to spl_object_id() and WeakReference ===== | ||
+ | |||
+ | Weak maps require first-class language support and cannot be implemented using existing functionality provided by PHP. | ||
+ | |||
+ | At first sight, it may seem that an array mapping from '' | ||
+ | |||
+ | * '' | ||
+ | * The object ID cannot be converted back into an object, so iteration over the map is not possible. | ||
+ | * The value stored under the ID will not be released when the object is destroyed. | ||
+ | |||
+ | Using the '' | ||
+ | |||
+ | < | ||
+ | // Insertion | ||
+ | $this-> | ||
+ | |||
+ | // Lookup | ||
+ | $id = spl_object_id($object); | ||
+ | if (isset($this-> | ||
+ | [$weakRef, $data] = $this-> | ||
+ | if ($weakRef-> | ||
+ | return $data; | ||
+ | } | ||
+ | // This entry belongs to a destroyed object. | ||
+ | unset($this-> | ||
+ | } | ||
+ | return null; | ||
+ | </ | ||
+ | |||
+ | This makes use of the '' | ||
+ | A native weak map implementation will instead remove the value from the weak map as soon as the object key is destroyed. |
rfc/weak_maps.1572892607.txt.gz · Last modified: 2019/11/04 18:36 by nikic