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

FIX 11577 performance issues with large list of groups #11578

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

Conversation

beerbohmdo
Copy link
Contributor

@beerbohmdo beerbohmdo commented Jan 27, 2025

Description

If you have many groups you can no longer edit a member in the admin.

Manual testing steps

As this is a performance issue, I am not sure howto create a unittest for this. But here is a task to create a massive amount of groups:

<?php

use SilverStripe\Dev\BuildTask;
use SilverStripe\ORM\DB;
use SilverStripe\Security\Group;

class GroupCreationTask extends BuildTask
{
    public function run($request): void
    {
        DB::query('TRUNCATE "Group"');
        DB::query('TRUNCATE "Permission"');

        Group::singleton()->requireDefaultRecords();

        $conn = DB::get_conn();
        $conn->transactionStart();

        $start = hrtime(true);

        for ($i = 0; $i < 3000; $i++) {
            if (($i % 100) == 0) {
                $conn->transactionEnd();
                $conn->transactionStart();

                $end = hrtime(true);
                $t = ($end - $start) / 1e9;

                printf("%d - %fs\n", $i, $t);

                $start = hrtime(true);
            }

            $parentGroup = Group::create();
            $parentGroup->Code = 'group_' . $i;
            $parentGroup->Title = 'Group ' . $i;
            $parentGroup->write();

            for ($j = 0; $j < 5; $j++) {
                $group = Group::create();
                $group->Code = 'group_' . $i . '_' . $j ;
                $group->Title = 'Group ' . $i . ' ' . $j;
                $group->ParentID = $parentGroup->ID;
                $group->write();
            }
        }

        $conn->transactionEnd();

        $end = hrtime(true);
        $t = ($end - $start) / 1e9;

        printf("%d - %fs\n", $i, $t);
    }
}

Issues

Pull request checklist

  • The target branch is correct
  • All commits are relevant to the purpose of the PR (e.g. no debug statements, unrelated refactoring, or arbitrary linting)
    • Small amounts of additional linting are usually okay, but if it makes it hard to concentrate on the relevant changes, ask for the unrelated changes to be reverted, and submitted as a separate PR.
  • The commit messages follow our commit message guidelines
  • The PR follows our contribution guidelines
  • Code changes follow our coding conventions
  • This change is covered with tests (or tests aren't necessary for this change)
  • Any relevant User Help/Developer documentation is updated; for impactful changes, information is added to the changelog for the intended release
  • CI is green

@beerbohmdo beerbohmdo force-pushed the 11577-slow-group-tree branch from 120baae to c8b41b0 Compare January 27, 2025 10:17
@beerbohmdo beerbohmdo changed the title 11577 Fix performance issues with large list of groups FIX 11577 performance issues with large list of groups Jan 30, 2025
Copy link
Member

@GuySartorelli GuySartorelli left a comment

Choose a reason for hiding this comment

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

Thanks for submitting this - I've requested a few changes below.

FYI the beta release for CMS 5.4 (which is also the feature freeze for all of CMS 5) is currently scheduled for Feb 17, so for this change to be included in CMS 5 it will need to be merged before then.
I will do my best to be quick to respond and review this PR as you update it.

Comment on lines 89 to 92
private static $searchable_fields = [
'Title',
'Description',
];
Copy link
Member

Choose a reason for hiding this comment

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

I don't think this change should alter the searchable fields

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Without the default search context is empty and the SearchableDropdownField does not filter.

Comment on lines 133 to 160
$threshold = DBForeignKey::config()->get('dropdown_field_threshold');
$overThreshold = $groups->count() > $threshold;
Copy link
Member

Choose a reason for hiding this comment

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

We shouldn't be using that unrelated configuration here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This are the default settings for SearchableDropdownField. I can create a extra config for that.

Comment on lines +484 to +520
public function getBreadcrumbTitle(): string
{
return $this->getBreadcrumbs(' > ');
}
Copy link
Member

Choose a reason for hiding this comment

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

Why do we need this new method?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the Label for the FormField.

Comment on lines 738 to 789
private function dedupeCode(): void
{
$currentGroups = Group::get()
->exclude('ID', $this->ID)
->map('Code', 'Title')
->toArray();
$code = $this->Code;
$count = 2;
while (isset($currentGroups[$code])) {
$code = $this->Code . '-' . $count;
$count++;

if ($code) {
while ($this->checkIfCodeExists($code)) {
$code = $this->Code . '-' . $count;
$count++;
}

$this->setField('Code', $code);
}
$this->setField('Code', $code);
}

private function checkIfCodeExists(string $code): bool
{
return Group::get()->filter('Code', $code)->exclude('ID', $this->ID)->exists();
}
Copy link
Member

Choose a reason for hiding this comment

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

How do changes to the deduping code relate to usage of a different form field?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is more or less the same issue as with the large list in the FormField. Without you are not able to save a group if you have many of them.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You can check the output of my generater script above with and without the change. Without the script will get slower with each iteration.

Comment on lines 1377 to 1378
$threshold = DBForeignKey::config()->get('dropdown_field_threshold');
$overThreshold = $groups->count() > $threshold;
Copy link
Member

Choose a reason for hiding this comment

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

Like with the change in Group, we shouldn't use this unrelated configuration.

@beerbohmdo beerbohmdo force-pushed the 11577-slow-group-tree branch 2 times, most recently from 7ffbd21 to 99aaf6b Compare February 5, 2025 07:58
@beerbohmdo beerbohmdo force-pushed the 11577-slow-group-tree branch from 99aaf6b to 9ec4dd6 Compare February 5, 2025 08:09
@beerbohmdo
Copy link
Contributor Author

I created custom configs, a getter for a custom search context and added some comments.

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

Successfully merging this pull request may close these issues.

2 participants