Skip to content

Conversation

@teal-bauer
Copy link

Fixes memory exhaustion when using unique()->optional()->method() or unique()->valid()->method() chains by adding explicit optional() and valid() methods to UniqueGenerator.

This is the 2.0 branch counterpart to #1028.

When calling $faker->unique()->optional()->safeEmail(), the UniqueGenerator::__call() handler would call optional() on the underlying generator, returning a ChanceGenerator, then attempt to serialize() it for uniqueness tracking. The ChanceGenerator contains a reference to the entire Generator with all providers, causing massive serialized strings and exponential memory growth.

By adding explicit optional() and valid() methods to UniqueGenerator, ChanceGenerator now wraps UniqueGenerator (not the base Generator). When the final method is called, it delegates back to UniqueGenerator, which serializes only the final value (e.g., the email string), not generator objects.

Fixes #1027

…)->optional()

When calling unique()->optional()->method(), the __call handler would
serialize the returned ChanceGenerator object (which contains the entire
Generator with all providers) rather than the final value. This caused
exponential memory growth.

Adding explicit optional() and valid() methods ensures proper generator
chaining - ChanceGenerator now wraps UniqueGenerator, delegating back
for actual value generation while preserving uniqueness tracking.

Fixes FakerPHP#1027
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

unique()->optional() causes massive memory usage due to serializing Generator

1 participant