Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve performance when purifying tokens #32212

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

MegaphoneJon
Copy link
Contributor

Overview

We think of token rendering as expensive. But 90% of the time spent in Civi\Token\TokenRow->render() is spent on a getFields() API call whose only purpose is to check if a field might have rich text so that we can purify it.

We can replace this with a call to \Civi\Api\Request->entityFields(). In my testing, when combined with the caching in #32210, this reduces the time spent in Civi/Token/TokenRow::fill() from 24 seconds to .28 seconds.

Before

Slow.

After

Fast.

Comments

My main concern is that getFields returns custom fields in API3 and not in API4. However, in my testing, the custom fields are already populated in $htmlTokens, so this has no effect.

Copy link

civibot bot commented Feb 25, 2025

🤖 Thank you for contributing to CiviCRM! ❤️ We will need to test and review this PR. 👷

Introduction for new contributors...
  • If this is your first PR, an admin will greenlight automated testing with the command ok to test or add to whitelist.
  • A series of tests will automatically run. You can see the results at the bottom of this page (if there are any problems, it will include a link to see what went wrong).
  • A demo site will be built where anyone can try out a version of CiviCRM that includes your changes.
  • If this process needs to be repeated, an admin will issue the command test this please to rerun tests and build a new demo site.
  • Before this PR can be merged, it needs to be reviewed. Please keep in mind that reviewers are volunteers, and their response time can vary from a few hours to a few weeks depending on their availability and their knowledge of this particular part of CiviCRM.
  • A great way to speed up this process is to "trade reviews" with someone - find an open PR that you feel able to review, and leave a comment like "I'm reviewing this now, could you please review mine?" (include a link to yours). You don't have to wait for a response to get started (and you don't have to stop at one!) the more you review, the faster this process goes for everyone 😄
  • To ensure that you are credited properly in the final release notes, please add yourself to contributor-key.yml
  • For more information about contributing, see CONTRIBUTING.md.
Quick links for reviewers...

➡️ Online demo of this PR 🔗

@civibot civibot bot added the master label Feb 25, 2025
@MegaphoneJon
Copy link
Contributor Author

PS - anyone else who runs XDebug that can replicate my numbers, I'd appreciate it! I assume there's nothing that odd about my setup that would cause such different results on getFields() calls but it'd be good to confirm. Sending a mailing to 1,000 contacts is a good way to gather this profile.

@eileenmcnaughton
Copy link
Contributor

@MegaphoneJon oout of curiousity - is the apiv4 any different to v3?

@@ -288,7 +295,7 @@ public function fill($format = NULL) {
if ($entity == 'activity' && $field == 'details') {
$htmlTokens[$entity][$field] = $value;
}
elseif (($entityFields['values'][$field]['data_type'] ?? NULL) === 'Memo') {
elseif (($entityFields[$field]['data_type'] ?? NULL) === 'Text') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm confused about this. How the heck did it ever work before? Api3.getfields does not return "data_type". The output from the Api3 explorer looks like this:

        "activity_details": {
            "name": "details",
            "type": 32,
            "title": "Details",
            "description": "Details about the activity (agenda, notes, etc).",
            "rows": 8,
            "cols": 60,
            "usage": {
                "import": true,
                "export": true,
                "duplicate_matching": true,
                "token": false
            },
            "import": true,
            "where": "civicrm_activity.details",
            "export": true,
            "table_name": "civicrm_activity",
            "entity": "Activity",
            "bao": "CRM_Activity_BAO_Activity",
            "localizable": 0,
            "html": {
                "type": "RichTextEditor",
                "rows": 8,
                "cols": 60
            },
            "add": "1.1",
        }            

Copy link
Contributor

@seamuslee001 seamuslee001 Feb 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@colemanw @MegaphoneJon It seemed to get returned for custom fields tho

This is doing a civicrm_api3('contact', 'getfields', ['api_action' => 'get']); on smaster FYI

        "custom_3": {
            "id": "3",
            "label": "Marriage Date",
            "headerPattern": "//",
            "title": "Marriage Date",
            "custom_field_id": "3",
            "groupTitle": "Constituent Information",
            "data_type": "Date",
            "name": "custom_3",
            "type": 4,
            "html_type": "Select Date",
            "default_value": null,
            "text_length": null,
            "options_per_line": null,
            "custom_group_id": "1",
            "extends": "Individual",
            "is_search_range": "0",
            "extends_entity_column_value": null,
            "extends_entity_column_id": "",
            "is_view": "0",
            "is_multiple": "0",
            "option_group_id": null,
            "date_format": "mm/dd/yy",
            "time_format": 0,
            "is_required": "0",
            "table_name": "civicrm_value_constituent_information_1",
            "column_name": "marriage_date_3",
            "serialize": 0,
            "where": "civicrm_value_constituent_information_1.marriage_date_3",
            "extends_table": "civicrm_contact",
            "search_table": "contact_a"
        },

@MegaphoneJon
Copy link
Contributor Author

@eileenmcnaughton @seamuslee001 I address the v3/v4 and its custom fields in my original post. But now there are failing tests so hopefully I can address concerns that way.

@colemanw
Copy link
Member

colemanw commented Feb 26, 2025

Now I'm wondering if this code does anything at all. If

  1. This code only checks data_type if the field is not custom,
  2. And Api3 only returns data_type if the field is custom,
  3. Then this code is useless

@eileenmcnaughton
Copy link
Contributor

looks like it started here #13283

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants