Skip to content

Commit

Permalink
minor #6011 Performance optimization in FieldCollection (javiereguiluz)
Browse files Browse the repository at this point in the history
This PR was merged into the 4.x branch.

Discussion
----------

Performance optimization in FieldCollection

EasyAdmin is pretty fast, but from time to time I profile some apps to check possible performance improvements using the amazing [Blackfire profiler](https://blackfire.io/).

While checking the `detail` page of some entity I saw this:

<img width="418" alt="performance-issue" src="https://github.com/EasyCorp/EasyAdminBundle/assets/73419/aa9ee069-a2b5-4709-acaa-168ee6e65d4c">

5,000 calls to `FieldDto::getUniqueId()` and another 5,000 calls to Symfony's `Ulid::__toString()` 😱

If you check the changes of this PR, it's easy to understand why. The field collection is already indexed by the field's uniqueID, so let's reuse it instead of recomputing it.

This is the before/after comparison:
<img width="654" alt="optimization-result" src="https://github.com/EasyCorp/EasyAdminBundle/assets/73419/119495b6-3bea-4cce-a0fe-06b33062ac04">

Commits
-------

d807133 Performance optimization in FieldCollection
  • Loading branch information
javiereguiluz committed Nov 8, 2023
2 parents 510945d + d807133 commit a235a0a
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/Collection/FieldCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,13 @@ public function prepend(FieldDto $newField): void
public function insertBefore(FieldDto $newField, FieldDto $existingField): void
{
$newFields = [];
foreach ($this->fields as $field) {
if ($existingField->getUniqueId() === $field->getUniqueId()) {
$existingFieldUniqueId = $existingField->getUniqueId();
foreach ($this->fields as $fieldUniqueId => $field) {
if ($existingFieldUniqueId === $fieldUniqueId) {
$newFields[$newField->getUniqueId()] = $newField;
}

$newFields[$field->getUniqueId()] = $field;
$newFields[$fieldUniqueId] = $field;
}

$this->fields = $newFields;
Expand Down

0 comments on commit a235a0a

Please sign in to comment.