Fix ~25% performance hit when no CiviRules exist for an entity

JonGold requested to merge JonGold/civirules:performance into master

I was doing some performance testing by running a script to update a contact's custom fields 100 times (see below).

Without CiviRules disabled, it completed in 46 seconds. With CiviRules, it completed in 58 seconds. It was surprising since I only have Contribution triggers.

I saw that CRM_Civirules_Utils_PreData made database calls to collect the pre data regardless of whether it was needed. With the four lines I added in that file, I got this script to run in 48 seconds. I also added a cache to CRM_Civirules_BAO_Rule::findRulesByObjectNameAndOp, which got me down to 47 seconds. That last optimization is within the margin of error, so might not be helping. However, that first change is real and dramatic.

It seems like this could go even further to see if there's any sort of Field Value Comparison conditions to skip the database calls in even more scenarios, but that can be a separate MR.

eval(`cv php:boot`);
for ($i = 1; $i <= 100; $i++) {
  $custom_37 = $i % 2 ? '0' : '1';
  $params = [
    'contact_id' => 3,
    'custom_37' => $custom_37,
    'custom_30' => $i,
    'contact_type' => 'Individual',
    'version' => 3,

PS - I realize now that $triggerCache is probably wrong - maybe $ruleCache? If you'd approve this otherwise, let me know and I'll change the variable name to whatever you prefer.

Merge request reports