Skip to content
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

Improve performance of CastPropertiesDataPipe by using hash lookups instead of ->first #721

Merged

Conversation

LauJosefsen
Copy link
Contributor

Hi Spatie developers.

I was running into some performance issues when creating thousands of data objects from array payloads, which lead me to run the xdebug profiler.

I setup a local benchmark where I created and validated 10k data objects using the ::collect method, the data class has 18 properties, most of which have casts in place (enums, Carbon, and other custom casts)

I noticed from my xdebug profile results that the closure in the deleted line is ran a whopping 1.710.000 times. By replacing with the new line, I managed to reduce my crude benchmark from an 11 seconds runtime, to 6.5 seconds. I thought you would be interested in this finding.

Unfortunately I am not very well versed in this project, and I am a bit uncertain if the DataClass:properties collection is always keyed by the data property name, but I am hoping we can collaborate into merging either this change, or a similar change, that introduces a hash lookup instead of the ->first method currently in place.

@LauJosefsen LauJosefsen changed the title Improve performance of CastPropertiesDataPipe by using Improve performance of CastPropertiesDataPipe by using hash lookups instead of ->first Mar 27, 2024
@rubenvanassche
Copy link
Member

Great catch, can't believe I missed that one!

@rubenvanassche rubenvanassche merged commit daa17e5 into spatie:main Apr 4, 2024
11 of 12 checks passed
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.

2 participants