Welcome Guest, Not a member yet? Register   Sign In
feat: Separate unused lists in classes
#1

(This post was last modified: 03-03-2025, 12:25 AM by ozornick.)

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
Simple CI 4 project for beginners codeigniter-expenses ( topic )
Reply


Messages In This Thread
feat: Separate unused lists in classes - by ozornick - 03-03-2025, 12:11 AM



Theme © iAndrew 2016 - Forum software by © MyBB