th3n3rd/cartesian-product
Memory-efficient Cartesian Product generator for PHP. Uses iterators to yield one tuple at a time, letting you handle very large combinations without big memory usage. Build products via fluent with() calls or CartesianProduct::of(), iterate or toArray().
filter() or map() on Cartesian products).composer require installation with zero configuration. No service provider or facade required.| Risk Area | Assessment | Mitigation Strategy |
|---|---|---|
| Memory Management | Iterator-based design minimizes risk, but toArray() could cause memory spikes. |
Enforce lazy evaluation in public APIs; document memory constraints in usage guidelines. |
| Performance Overhead | Iterator overhead for small datasets (~<100 items). | Benchmark against native solutions; optimize for use cases with ≥1000 combinations. |
| Thread Safety | Stateless iterators are thread-safe, but concurrent toArray() calls may race. |
Recommend single-threaded usage for toArray(); use iterators in async contexts. |
| Laravel-Specific Quirks | None; agnostic to frameworks. | Test with Laravel’s service container and event loop (e.g., queues). |
CartesianProduct::query() for Eloquent) be built?collect() for chaining (e.g., filter(), map()).CartesianProduct → dispatch()).StreamedResponse for memory efficiency.expect()->iteratesOver().CartesianProduct::fake()).app/Services/CartesianProductService) to standardize usage.class CartesianProductService {
public function generateCombinations(array $arrays): \Generator {
return CartesianProduct::of($arrays);
}
}
Collect::macro('cartesian', function ($arrays) {
return $this->pipe(fn ($collection) => CartesianProduct::of($arrays));
});
composer.json; validate basic usage in a single service.toArray()").var_dump individual tuples).$startMemory = memory_get_usage();
foreach ($cartesianProduct as $tuple) {
// Process tuple
}
Log::debug("Memory used: " . (memory_get_usage() - $startMemory));
toArray() results for repeated computations (e.g., CartesianProduct::remember() wrapper).| Scenario | Impact | Mitigation |
|---|---|---|
| Memory Exhaustion | toArray() crashes on large data. |
Enforce iterator-only usage in production; add size limits. |
| Slow Iteration | High-latency for large datasets. | Use async processing (e.g., queues) or paginate results. |
| Concurrent Access | Race conditions in toArray(). |
Restrict toArray() to single-threaded contexts; use iterators for async. |
| Invalid Input | Silent failures for bad data. | Add input validation (e.g., assertAllArrays()). |
How can I help you explore Laravel packages today?