kphp icon indicating copy to clipboard operation
kphp copied to clipboard

add initial SPL ArrayIterator class support

Open quasilyte opened this issue 2 years ago • 0 comments

ArrayIterator provides a way to iterate the array without using foreach statement or legacy next()/current() APIs.

In PHP it has almost identical performance to the above mentioned pair of next()/current(). In KPHP it works with minimal overhead when compared to a normal foreach statement. This means that is can be used in lower level components that need to iterate the array in unusual way.

Some performance comparison results:

Direct1000         7065.0 ns/op   0 B/op      0 allocs/op
ArrayIterator1000  8375.0 ns/op   0 B/op      0 allocs/op
CopyKeys1000       40541.0 ns/op  16088 B/op  1 allocs/op
Reslice1000        74655.0 ns/op  88088 B/op  1001 allocs/op

Direct - foreach array iteration. ArrayIterator uses the newly introduced class. CopyKeys uses array_keys() to iterate the array. Reslice uses array_slice() to extract the array portion.

The 0 allocations are possible with KPHP function reset_array_iterator that can be used to re-use the array iterator objects when possible. This function is trivially polyfilled in PHP.

Until KPHP supports generic classes, this ArrayIterator only works with mixed[] typed arrays.

quasilyte avatar Aug 17 '22 17:08 quasilyte