CodeIgniter Forums
feat: Separate unused lists in classes - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Feature Requests (https://forum.codeigniter.com/forumdisplay.php?fid=29)
+--- Thread: feat: Separate unused lists in classes (/showthread.php?tid=92542)



feat: Separate unused lists in classes - ozornick - 03-03-2025

Hi. Don't you think that some of the infrequently used data in classes can be optimized?
For example, in the Entity, DataCaster:
Code:
private defaultCastHandlers -> array (15)
array => string (33) "CodeIgniter\Entity\Cast\ArrayCast"
...
I think it's redundant to have 1000 copies of a list in 1000 entities. At least it affects memory. Can we put this in a separate class, enum?
I add class and replace instance DataCaster (this is not a complete solution.):
PHP Code:
class CastHandlers {
    public const array DefaultCastHandlers = [
        'array'    => ArrayCast::class,
        // ...
    ];
}

public function 
__construct(?array $data null)
{
    $this->dataCaster = new DataCaster(
        array_merge(CastHandlers::DefaultCastHandlers$this->castHandlers),
        null,
        null,
        false,
    );
    // ...


A quick test shows an improvement of 15-25% (appstarter on build-in server, opcache, PHP 8.4.4):
Before: 100ms 5.641 MB
After: 81ms  4.904 MB, 72ms  4.904 MB

The difference between 21 entities and 42 is the same:
Before: 180ms 10.300 MB
After: 156ms 8.880 MB