66#include " support.h"
77#include " generated/CombinationsHelper.h"
88#include " zv.h"
9+ #include " TypeTraits.h"
910
1011static zend_class_entry *pt_ce_combinations = nullptr ;
1112
1213namespace phpstanturbo {
1314
14- /* Mirrors PHPStan\Internal\CombinationsHelper. The PHP twin recurses through
15- * a generator; the native implementation materializes the full product
16- * iteratively, walking an odometer over the input arrays . */
15+ /* Mirrors PHPStan\Internal\CombinationsHelper. PHP-visible calls use the
16+ * twin's lazy generator helper; native consumers walk the same product one
17+ * combination at a time without materializing it . */
1718class CombinationsHelper
1819{
1920public:
20- /* caps the result array's pre-allocation hint, not the product itself */
21- static constexpr zend_ulong SIZE_HINT_LIMIT = 1048576 ;
22-
23- /* combinations() refuses products beyond this instead of letting the
24- * multiply overflow and silently truncate the result */
25- static constexpr zend_ulong PRODUCT_LIMIT = 1ULL << 32 ;
21+ static zv::Val combinations (zval *arrays)
22+ {
23+ return pt_type_call_static (PT_CLASS_ITERABLE_HELPER , PT_LC (" combinations" ), 1 , arrays);
24+ }
2625
27- /* UNDEF = pending exception */
28- static zv::Val combinations (zv::ArrRef arrays)
26+ /* false = the consumer stopped or a pending exception */
27+ static bool forEach (zv::ArrRef arrays, pt_combination_consumer consumer, void *context )
2928 {
3029 uint32_t n = arrays.size ();
3130 if (n == 0 ) {
32- /* combinations([]) yields a single empty combination */
33- zv::Arr result = zv::Arr::create (0 );
34- result.push (zv::Arr::create (0 ));
35- return zv::Val (std::move (result));
31+ zv::Arr combination = zv::Arr::create (0 );
32+ return consumer (combination.raw (), context);
3633 }
3734
3835 /* borrow the inner array of each element (the input owns them) */
@@ -43,15 +40,14 @@ class CombinationsHelper
4340 if (UNEXPECTED (!element.isArray ())) {
4441 efree (inner);
4542 zend_type_error (" PHPStanTurbo\\ CombinationsHelper::combinations() expects an array of arrays" );
46- return zv::Val () ;
43+ return false ;
4744 }
4845 inner[i++] = element.raw ();
4946 }
5047
5148 /* flatten each inner array into a raw element-slot vector */
5249 uint32_t *sizes = (uint32_t *) emalloc (n * sizeof (uint32_t ));
5350 zval ***vecs = (zval ***) emalloc (n * sizeof (zval **));
54- zend_ulong total = 1 ;
5551 bool hasEmptyInner = false ;
5652
5753 for (i = 0 ; i < n; i++) {
@@ -63,23 +59,6 @@ class CombinationsHelper
6359 vecs[i] = NULL ;
6460 continue ;
6561 }
66- if (UNEXPECTED (total > PRODUCT_LIMIT / sizes[i])) {
67- /* The PHP twin is a lazy generator and never materializes the
68- * product, but every consumer iterates it fully, so a product
69- * this size is unreachable in practice. Failing loudly beats
70- * the silent truncation an overflowed multiply would cause. */
71- for (uint32_t k = 0 ; k < i; k++) {
72- if (vecs[k] != NULL ) {
73- efree (vecs[k]);
74- }
75- }
76- efree (vecs);
77- efree (sizes);
78- efree (inner);
79- pt_throw_should_not_happen ();
80- return zv::Val ();
81- }
82- total *= sizes[i];
8362 vecs[i] = (zval **) emalloc (sizes[i] * sizeof (zval *));
8463 uint32_t j = 0 ;
8564 for (auto entry : innerArr) {
@@ -89,11 +68,27 @@ class CombinationsHelper
8968 }
9069 }
9170
92- zv::Val result;
93- if (hasEmptyInner) {
94- result = zv::Arr::create (0 );
95- } else {
96- result = product (vecs, sizes, n, total);
71+ bool completed = true ;
72+ if (!hasEmptyInner) {
73+ uint32_t *indices = (uint32_t *) ecalloc (n, sizeof (uint32_t ));
74+ for (;;) {
75+ zv::Arr combination = zv::Arr::create (n);
76+ for (i = 0 ; i < n; i++) {
77+ combination.push (zv::Ref (vecs[i][indices[i]]));
78+ }
79+ if (!consumer (combination.raw (), context)) {
80+ completed = false ;
81+ break ;
82+ }
83+
84+ int64_t j = (int64_t ) n - 1 ;
85+ for (; j >= 0 ; j--) {
86+ if (++indices[j] < sizes[j]) break ;
87+ indices[j] = 0 ;
88+ }
89+ if (j < 0 ) break ;
90+ }
91+ efree (indices);
9792 }
9893
9994 for (i = 0 ; i < n; i++) {
@@ -104,31 +99,7 @@ class CombinationsHelper
10499 efree (vecs);
105100 efree (sizes);
106101 efree (inner);
107- return result;
108- }
109-
110- private:
111- static zv::Val product (zval *const *const *vecs, const uint32_t *sizes, uint32_t n, zend_ulong total)
112- {
113- uint32_t *indices = (uint32_t *) ecalloc (n, sizeof (uint32_t ));
114- zv::Arr result = zv::Arr::create ((uint32_t ) (total > SIZE_HINT_LIMIT ? SIZE_HINT_LIMIT : total));
115-
116- for (zend_ulong c = 0 ; c < total; c++) {
117- zv::Arr comb = zv::Arr::create (n);
118- for (uint32_t i = 0 ; i < n; i++) {
119- comb.push (zv::Ref (vecs[i][indices[i]]));
120- }
121- result.push (std::move (comb));
122-
123- /* odometer: advance the rightmost index, carrying leftwards */
124- for (int64_t j = (int64_t ) n - 1 ; j >= 0 ; j--) {
125- if (++indices[j] < sizes[j]) break ;
126- indices[j] = 0 ;
127- }
128- }
129-
130- efree (indices);
131- return zv::Val (std::move (result));
102+ return completed;
132103 }
133104};
134105
@@ -151,7 +122,7 @@ void pt_register_combinations_helper()
151122 if (!zp::parse<zp::Ht>(execute_data, arrays)) RETURN_THROWS ();
152123 zval arraysZv;
153124 ZVAL_ARR (&arraysZv, arrays);
154- zv::Val result = CombinationsHelper::combinations (zv::ArrRef ( &arraysZv) );
125+ zv::Val result = CombinationsHelper::combinations (&arraysZv);
155126 if (UNEXPECTED (result.isUndef ())) RETURN_THROWS ();
156127 result.intoReturnValue (return_value);
157128 });
@@ -163,9 +134,9 @@ void pt_register_combinations_helper()
163134
164135/* {{{ shared with the compound family (TypeTraits.h) */
165136
166- zv::Val pt_combinations_helper_combinations (zval *arrays)
137+ bool pt_combinations_helper_for_each (zval *arrays, pt_combination_consumer consumer, void *context )
167138{
168- return CombinationsHelper::combinations (zv::ArrRef (arrays));
139+ return CombinationsHelper::forEach (zv::ArrRef (arrays), consumer, context );
169140}
170141
171142/* }}} */
0 commit comments