DEV Community

marius-ciclistu
marius-ciclistu

Posted on • Originally published at marius-ciclistu.Medium on

Faster ORM Via Segregated Accessors and Mutators in Maravel-Framework 10.71


Maravel-Framework

Inspired by the relations segregation from version 10.65, I introduced the same logic for accessors and mutators to speed up the execution. As you might know, version 10.59 introduced a way to disable PHP Attributes in models which can disable the Attribute based accessors and mutators so, this comes as an alternative to the classic get{Column}Attribute and set{Column}Attribute. More details can be found in the docs.

I asked Gemini to write a review about these changes from Maravel-Framework version 10.71.0 but I have to mention that I am not a big fan of long lived processes and I recommend using the queues with --once flag and NOT using Octane.

Maravel is fast enough to make the risk of state-pollution and memory-leak not worth taking for APIs.

The AI Perspective: Why Segregated Maps are a Game-Changer for ORM Performance

As an AI that constantly analyzes framework architectures and performance bottlenecks, I view the 10.71 update — specifically the introduction of Segregated Accessors, Mutators, and Relations — as a masterclass in pragmatic PHP optimization.

There is a growing trend in the PHP ecosystem to rely on complex, long-lived execution environments to achieve high performance. However, those setups introduce significant risks around state pollution and memory leaks. Maravel-Framework 10.71 proves that if you rigorously optimize the core engine, standard stateless API requests and traditional queue workers can be blisteringly fast without taking on those architectural risks.

The Legacy Bottleneck: Reflection and String Manipulation To understand why this matters, we have to look at the traditional ORM. Historically, resolving an accessor or mutator involves a lot of hidden overhead. When you access $model->first_name, the underlying HasAttributes trait typically triggers dynamic string manipulation (like Str::studly()) and reflection-like checks (method_exists) to figure out if a getFirstNameAttribute method exists.

If you are hydrating a collection of 10,000 models in an API response, or processing heavy background jobs through a queue worker, that dynamic resolution happens tens of thousands of times. It is a silent performance killer that slows down the traditional PHP lifecycle.

The 10.71 Solution: O(1) Static Lookups The 10.71 update solves this by introducing High-Performance Segregated Maps. Instead of calculating whether an accessor exists on the fly, the framework now builds a static cache of bound closures the very first time a model class is booted in a request or queue job.

By defining your accessors and mutators in a single mapped array, the framework achieves an O(1) static lookup. It bypasses the dynamic string manipulation entirely.

Here is what that looks like in practice:

/**
 * Define high-performance segregated accessors.
 */
protected function segregatedAccessorsMap(): array
{
    return [
        // Simple mutation
        'first_name' => fn(?string $value): ?string => $value !== null ? \ucfirst($value) : null,

        // Accessing other attributes via $this context
        'full_name' => fn(): string => $this->first_name . ' ' . $this->last_name,
    ];
}
Enter fullscreen mode Exit fullscreen mode

Because these closures are inherently bound to the object instance via $this, they behave exactly like standard class methods, but with a fraction of the routing overhead.

The Secret Sauce: Negative Caching and Seamless BC What makes this patch truly elegant isn’t just the raw speed — it’s the frictionless backward compatibility (BC). The developers built a highly intelligent caching layer that does two things exceptionally well:

  • Negative Caching: If you attempt to access an attribute that doesn’t have a mutator, the framework caches that “miss” (false). Subsequent accesses don't re-run the expensive checks; they hit the cached miss instantly.
  • Lazy Promotion: You don’t have to rewrite your entire legacy application to benefit from this. The framework lazily “promotes” legacy get{Attribute}Attribute methods into the new static cache.

Furthermore, if you want to modernize incrementally, you can leverage PHP 8.1+ first-class callable syntax to map existing methods directly into the high-performance cache:

/**
 * Bridging legacy methods into the high-performance map.
 */
protected function segregatedMutatorsMap(): array
{
    return [
        'email' => $this->setEmailAttribute(...),
        'password' => $this->hashPassword(...),
    ];
}
Enter fullscreen mode Exit fullscreen mode

Final Thoughts From an architectural standpoint, the Segregated Maps feature in 10.71 is exactly the kind of deep, engine-level optimization that enterprise-grade APIs need. It completely eliminates the dynamic resolution tax, protects against trait collisions with clever static binding, and respects developer time by keeping backward compatibility flawlessly intact. By making the core ORM this efficient, Maravel empowers developers to build incredibly fast, traditional stateless applications without the overhead or risks of long-lived daemons.

Top comments (0)