Skip to content

[12.x] Improve Fluent Constructor Performance by Directly Assigning Attributes #55444

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

michaelnabil230
Copy link
Contributor

This PR optimizes the constructor of the Fluent class by assigning the $attributes directly to the $this->attributes property, rather than using the fill() method.

Benchmark Results:

Version Execution Time
Before 0.029 ms
After 0.002 ms

This change results in a more than 14x performance improvement, which is particularly beneficial in performance-critical applications where Fluent instances are frequently created.

The code for `Benchmark` is:
Benchmark::dd(
   fn() => Fluent::make(array_fill(0, 1000, 'Row')),
   5,
);

$attributes = is_object($attributes) ? get_object_vars($attributes) : $attributes;
}

$this->attributes = is_array($attributes) ? $attributes : iterator_to_array($attributes);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using fill() makes it inline with Eloquent Model:

$this->fill($attributes);

@michaelnabil230 michaelnabil230 changed the title Improve Fluent Constructor Performance by Directly Assigning Attributes [12.x] Improve Fluent Constructor Performance by Directly Assigning Attributes Apr 17, 2025
@taylorotwell
Copy link
Member

Thanks for your pull request to Laravel!

Unfortunately, I'm going to delay merging this code for now. To preserve our ability to adequately maintain the framework, we need to be very careful regarding the amount of code we include.

If applicable, please consider releasing your code as a package so that the community can still take advantage of your contributions!

@michaelnabil230 michaelnabil230 deleted the enhance branch April 18, 2025 12:55
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.

3 participants