CRM-8393 Performance improvement in DataObject->fetch()

    Details

    • Type: Patch
    • Status: Done/Fixed
    • Priority: Major
    • Resolution: Fixed/Completed
    • Affects Version/s: 3.3.6
    • Fix Version/s: 3.4.5
    • Component/s: Core CiviCRM
    • Labels:
      None

      Description

      The fetch() method in DataObject calls str_replace() in a loop, in order to modify the keys of the array. This is an expensive operation, especially since fetch() is called in a loop itself when fetching records one row at a time.

      The str_replace() function can perform its own iteration on arrays, so we can cut down drastically on the number of calls to str_replace() by re-writing the keys prior to entering the loop.

      The code block starts on line 560.

      The original code is:
      foreach($array as $k=>$v) {
      $kk = str_replace(array("."," "), "_", $k);
      if (!empty($_DB_DATAOBJECT['CONFIG']['debug']))

      { $this->debug("$kk = ". $array[$k], "fetchrow LINE", 3); }

      $this->$kk = $array[$k];
      }

      The new code is:
      $keys = str_replace(array("."," "), "_", array_keys($array));
      $i = 0;
      foreach ($array as $val)

      { $key = $keys[$i++]; if (!empty($_DB_DATAOBJECT['CONFIG']['debug'])) $this->debug("$key = $val", "fetchrow LINE", 3); $this->$key = $val; }

      We are using this modification, and are seeing performance improvements on all platforms.

        Attachments

          Activity

          [CRM-8393] Performance improvement in DataObject->fetch()
          Graylin Kim added a comment -

          Its worth noting that this is actually an improvement on a previous patch (already accepted) detailed on the PEAR site at:

          http://pear.php.net/bugs/bug.php?id=18602

          Donald A. Lobo added a comment -

          Deepak:

          can u check the original PEAR patch also and patch the main civicrm code base. Seems like a great performance improvement

          Deepak Srivastava added a comment -

          Thanks Graylin & Ken,

          Patch applied to both fetch() and fetchRow() in r35234.

          Could this improvement be also submitted to pear or already done ?

          Graylin Kim added a comment -

          Thanks for the quick turn around Deepak. I'll be handling its submission to PEAR, just haven't quite gotten around to it yet. Maybe today...

            People

            • Assignee:
              Deepak Srivastava
              Reporter:
              Ken Zalewski

              Dates

              • Created:
                Updated:
                Resolved: