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

Caching of casted/converted model attribute values #4207

Open
notbakaneko opened this issue Feb 25, 2019 · 0 comments
Open

Caching of casted/converted model attribute values #4207

notbakaneko opened this issue Feb 25, 2019 · 0 comments

Comments

@notbakaneko
Copy link
Collaborator

Every access of an attribute on an Eloquent model results in the following (simplified) general flow:

magic __get -> getAttributeValue -> check if cast or mutator method is required -> call cast or mutator method -> return.

These values are not cached and are re-casted/converted every time the attribute is accessed. Given that it is common to access a model’s attribute several times through out a single request, there is potentially some performance to be gained but not having to repeatedly recast the value; the flip side is that if an attribute is only accessed once, the value have have been cached unnecessarily.

Premilinary testing with caching the converted attribute values on Beatmap and Beatmapset on the beatmapsets/search endpoint yields about a 6% increase in throughput (completely replacing Laravel’s casting is a >15% improvement).

While it is possible to read a model’s converted attribute value into a variable and pass that around, it is simply not practical and unwieldy to constantly ferry around the required variables across the various layers of the application. We could group multiple variables into DTOs and pass those around instead, but then we’d have to be able to remap them back to Eloquent models, at which point we’re approaching the realm of being better off having proper domain models using a more straightforward data mapper, rather than Laravel’s ORM.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants