Log1x / acf-composer

Compose ACF Fields, Blocks, Widgets, and Option Pages with ACF Builder on Sage 10.
https://github.com/Log1x/acf-composer
MIT License
419 stars 57 forks source link

ACF Composer is making WP Admin very slow #176

Closed samfrank closed 7 months ago

samfrank commented 1 year ago

First of all, love the plugin. Great work!

We have been using Sage 10 and ACF Composer in most of our builds but we have started to notice that it is causing issues when we have a lot of fields and a lot of partials within a flexible sections.

We did have the same issue in ACF Builder, but we managed to cache the fields in a transient on remote servers so the build function did not have to run every time.

Has any one ever ran into this issue with ACF Composer and/ or have a potential solution?

This is what we did using ACF Builder but I am not sure how to implement this using ACF Composer

$transientName = 'TRANSIENT_NAME';

// Use the cached version of the fields
if (false === ($fields = get_transient($transientName))) {
       $fieldsCollection = collect(
            glob(get_template_directory() . '/app/AcfBuilder/Fields/*.php'),
        )
        ->map(function ($field) {
        return require_once $field;
    })
    ->map(function ($field) {
        return $field->build();
    })
    ->filter(function ($value) {
        return is_array($value);
    });

    $fields = $fieldsCollection->all();

    // Don't save transient on local
    if (!str_contains($_SERVER['SERVER_NAME'], '.local')) {
        set_transient($transientName, $fields, 3600);
    }
}

if (is_array($fields) && sizeof($fields) && function_exists('acf_add_local_field_group')) {
    foreach ($fields as $fieldGroup) {
        acf_add_local_field_group($fieldGroup);
    }
}
Log1x commented 1 year ago

definitely wanted to add some caching alongside #173 but I do not have time to work on it right this second.

caching doesn't entirely depend on #173 though and I'm open to PR's. ideally it would be separate from Acorn's cache (e.g. not affected by acorn optimize) and instead done with its own command acorn acf:cache during deployment.

Log1x commented 1 year ago

What did you do to narrow down the speed issues to ACF Builder, or specifically, building the field groups?

Even on a very slow droplet and a pretty thick site, I'm not seeing any huge performance hit when building the fields – but maybe I do not have as many fields.

Screenshot

Could you show me your results?

Simply change https://github.com/Log1x/acf-composer/blob/master/src/AcfComposer.php#L64 to

$timer = microtime(true);

$this->registerPath($this->app->path());

dd('Loaded in ' . round(microtime(true) - $timer, 3) . 's', $this->composers);
Log1x commented 1 year ago

The only time I see a performance hit is when acf_add_local_field_group is ran – and no matter what caching I do, nothing changes that. I don't think I have any control over that in particular. :(

samfrank commented 1 year ago

Hey @Log1x

Thanks for replying to me to help diagnose the issue - I may of been jumping to conclusions about it being ACF Composer causing the issues so apologies for that.

After looking into the issue over sporadically over the past week I can see that this is a common result when using a lot of fields in a flexible content, the load time compounds and get worse with every layout addition.

I did run the monitor function above on a remote server (dev environment) and I did get concerning load time.

Screenshot 2023-09-15 at 11 40 15 am

Log1x commented 1 year ago

Can you invite me to a repo that can potentially reproduce this?

samfrank commented 1 year ago

@Log1x Thank you, I have sent you an invite

samfrank commented 1 year ago

I think a big issue is that I am maybe not using the package as intended.

We are using Flexible Content with the Classic editor instead of Blocks, which seems to be the main difference comparing your screenshot when building the field.

I am also using a lot of groups as well, which I have read is not great for performance