•  
      request #21377 Optimize CompatPDODataAccess::escapeInt
    Infos
    #21377
    Thomas Gerbet (tgerbet)
    2021-05-20 09:33
    2021-05-19 12:37
    22972
    Details
    Optimize CompatPDODataAccess::escapeInt

    I recently took a look to see why the display of my report in this tracker was slow to see if there was some easy wins (spoiler: there was see request #21364 and request #21363).

    On the latest profile one part stands out, about 10% of the CPU time (~6% of the wall time) is taken by CompatPDODataAccess::escapeInt due to the large number of values that goes through it.

    Of course it would be far better to remove its usage completely to use prepared statements (and possibly rework the section of the code that gives that much values to escaped by inlining the SQL queries) but in the meantime saving a few CPU cycles basically everywhere in Tuleap is still nice to have.

    The method currently looks like that:

        public function escapeInt($v, $null = CODENDI_DB_NOT_NULL)
        {
            if ($null === CODENDI_DB_NULL && $v === '') {
                return 'NULL';
            }
            $m = [];
            if (preg_match('/^([+-]?[1-9][0-9]*|[+-]?0)$/', $v, $m)) {
                return $m[1];
            }
            return '0';
        }
    

    if we avoid using preg_match to deal with the cast we can save some time:

        public function escapeInt($v, $null = CODENDI_DB_NOT_NULL)
        {
            if ($null === CODENDI_DB_NULL && $v === '') {
                return 'NULL';
            }
            return (string) (int) $v;
        }
    

    When benchmarking on my instance with 10000 values (less than what I have in the profile of my report in this tracker), we save about 55% the execution time (139ms ➝ 61ms).

    Code used to benchmark the method:

    <?php
    
    $ar = range(1, 100000);
    $dao = new DataAccessObject();
    
    $start = hrtime(true);
    $dao->getDa()->escapeIntImplode($ar);
    var_dump((hrtime(true) - $start) /1e6);
    
    Trackers
    All
    Empty
    • [ ] enhancement
    • [ ] internal improvement
    Empty
    Stage
    Thomas Gerbet (tgerbet)
    Closed
    2021-05-20
    Attachments
    Empty
    References

    Follow-ups