krakjoe · GitHub

TysonAndre

@TysonAndre

Closed

See spl_vector.stub.php for the userland API.
Earlier work on the implementation can be found at
https://github.com/TysonAndre/pecl-teds
and it can be tested out at https://pecl.php.net/package/teds as `Teds\Vector`
(currently the same apart from reusing spl's conversion of mixed to `int $offset`)
This was originally based on spl_fixedarray.c and previous work I did on an RFC.
Notable features:
- Roughly half the memory usage of (non-constant) arrays
  due to not needing a list of indexes of buckets separately
  from the zval buckets themselves.
- Same memory usage as SplFixedArray in 8.2 for a given **capacity**
- Lower memory usage and better performance than SplDoublyLinkedList
  or its subclasses (SplStack) due to being an array instead of a linked list
- More efficient resizing than SplFixedArray's setSize(getSize+1)
```php
final class Vector implements IteratorAggregate, Countable, JsonSerializable, ArrayAccess
{
	public function __construct(
		iterable $iterator = [],
		bool $preserveKeys = true
	) {}
	public function getIterator(): InternalIterator {}
	public function count(): int {}
	public function capacity(): int {}
	public function clear(): void {}
	public function setSize(int $size): void {}
	public function __serialize(): array {}
	public function __unserialize(array $data): void {}
	public static function __set_state(array $array): Vector {}
	public function push(mixed $value): void {}
	public function pop(): mixed {}
	public function toArray(): array {}
	// Strictly typed, unlike offsetGet/offsetSet
	public function valueAt(int $offset): mixed {}
	public function setValueAt(int $offset, mixed $value): void {}
	public function offsetGet(mixed $offset): mixed {}
	public function offsetExists(mixed $offset): bool {}
	public function offsetSet(mixed $offset, mixed $value): void {}
	// Throws because unset and null are different things.
	public function offsetUnset(mixed $offset): void {}
	public function indexOf(mixed $value): int|false {}
	public function contains(mixed $value): bool {}
	public function shrinkToFit(): void {}
	public function jsonSerialize(): array {}
}
```
I'm guessing this is a bug seen when inlining and one function using assembly
and the other not using assembly related to `zend_string_equal_content`?
At lower optimization levels it passes.
(Environment: gcc 9.3 on Linux)
`make test TESTS='ext/spl/tests/Vector/aggregate.phpt -m --show-mem --show-diff`
fails while compiling the variable in the foreach.

@TysonAndre

Read the original on github.com ↗