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 file sorting performance #6112

Merged
merged 10 commits into from
Feb 5, 2024

Conversation

distantnative
Copy link
Member

@distantnative distantnative commented Jan 1, 2024

This PR …

  • Add unit tests for HasFiles::acceptedFileTemplates()
  • Restore API endpoint, but create own request Panel in site area to use without return

Best to review each commit separately as each contains one contained improvement

Enhancements

  • Significantly improved file sorting performance (thanks @rasteiner)

Ready?

  • Unit tests for fixed bug/feature
  • In-code documentation (wherever needed)
  • Tests and checks all pass

For review team

@distantnative distantnative added the type: performance ⚡️ Is about performance; improves performance label Jan 1, 2024
@distantnative distantnative added this to the 4.1.0 milestone Jan 1, 2024
@distantnative distantnative self-assigned this Jan 1, 2024
@distantnative distantnative linked an issue Jan 1, 2024 that may be closed by this pull request
@distantnative distantnative requested a review from a team January 1, 2024 21:48
@bastianallgeier
Copy link
Member

Maybe we could kill two birds with one stone here. We want the Panel to be independent from the API anyway. We could keep the return value for the API call to avoid breaking changes. But have our own sorting request in the site area.

src/Cms/File.php Outdated Show resolved Hide resolved
@distantnative distantnative added needs: discussion 🗣 Requires further discussion to proceed needs: changes 🔁 Implement any requested changes to proceed labels Jan 10, 2024
@distantnative distantnative modified the milestones: 4.1.0, 4.2.0 Jan 18, 2024
@distantnative distantnative removed the needs: discussion 🗣 Requires further discussion to proceed label Jan 18, 2024
@distantnative
Copy link
Member Author

@bastianallgeier sorting request in the site area or would it make more sense to add API route support to sections (like we have for fields)?

@bastianallgeier
Copy link
Member

@distantnative you are right. Makes more sense!

@bastianallgeier
Copy link
Member

Looking at this again, I was wondering if we should move the acceptedFileTemplates method to the Blueprint classes instead.

@rasteiner
Copy link
Contributor

rasteiner commented Jan 26, 2024

Looking at this again, I was wondering if we should move the acceptedFileTemplates method to the Blueprint classes instead.

Currently there's a one to one mapping between model instances and their blueprint (you create a new blueprint for every model instance, even if it's the "same" blueprint as that of other models). Therefore every blueprint instance actually refers to a single instance of a model. The inherent idea of "blueprints" would however suggest that the same blueprint would be used by multiple models.

As long as there is this 1 to 1 mapping, I don't think it really makes a difference (other than semantics): it's the same code with the same outcome, simply executed in another place.
If you intend to break this mapping in the future (for example to improve efficiency) and have the same blueprint instance for many models, placing the function in the blueprint rather than the model would make a difference if, for example, the accepted file templates depend on a specific model (e.g. a model accepting only a certain number of a file type).

@distantnative
Copy link
Member Author

@rasteiner but I think @bastianallgeier is right, cause even if blueprint object were unique per blueprint (and not 1:1 for each model), the logic in acceptedFileTemplates doesn't depend on the model but the blueprint.

@distantnative
Copy link
Member Author

@bastianallgeier I think I could use help with creating the API endpoint for the section (aka also first adding API endpoints feature for sections as we have for fields)

@bastianallgeier
Copy link
Member

Ok, for some reason I totally thought that we already had the section API, which is obvisouly not true 🙈 I think it's a good idea to add it to 4.2 as well and then get this part of the task done. It's not super complicated. But it's definitely beyond the scope of this PR. Let's wrap this one up first by adding the unit tests and reverting the breaking change in the API route. I will start a new branch with the section API. I've already started working on it.

@bastianallgeier
Copy link
Member

bastianallgeier commented Jan 31, 2024

@distantnative I've added unit tests and refactored the detection for templates in fields a bit more. I also realised by doing so that we completely ignore fields in fieldsets so far. I would suggest to handle this in a separate PR though. It's going to be quite a challenge.

@rasteiner
Copy link
Contributor

rasteiner commented Jan 31, 2024

@rasteiner but I think @bastianallgeier is right, cause even if blueprint object were unique per blueprint (and not 1:1 for each model), the logic in acceptedFileTemplates doesn't depend on the model but the blueprint.

Sure, I wasn't saying that one approach is right or not. Just saying that one might have more future consequences than the other.
Technically (even though this isn't implemented yet), having a page blueprint like:

sections:
  files:
    template: foo
    max: 1

could mean that some models would accept a new "foo" file, while others (which already have a "foo") might not.

I guess it's therefore debatable if "acceptedFileTemplates" is a property of the model or the blueprint: the blueprint specifies the same rules for many models, but those same rules might give different results depending on the model. So, finally, is it the blueprint which accepts a file template, or is it the model? (honest question)

Should you decide to decouple model and blueprint, having this functionality in the blueprint, when results depend on the model like in the example above, would bring back the need for a static WeakMap<HasFiles, array> to cache results in the blueprint. While having it in the model instead, the cache would stay a "simple" array.

In the end, however, I guess it doesn't really matter and this is bikeshedding. If it were up to me to design this from ground up, I'd probably go for a façade function in the model which caches the results of a call to a blueprint function which accepts a model as parameter. (pseudocode: return $this->cache = $this->blueprint()->acceptedFileTemplates($this);).

@distantnative
Copy link
Member Author

But in your example, foo would always be an accepted file template from the model/blueprint. The specific section is defining a limit which shouldn't limit what this method returns. I know that this might open up possibilities where changing a file template somewhere ends up in this section having 2 entries. This however is really this area where we hit limits with the section based rules and we have to accept some shortcomings. We've seen it with other occasions, such things would only be solved properly with page-wide definitions, not sections.

@distantnative
Copy link
Member Author

@bastianallgeier I am fine with first merging this, but we should add an issue for the lack of checking field sets

src/Cms/Blueprint.php Outdated Show resolved Hide resolved
@bastianallgeier
Copy link
Member

It felt better to solve this directly instead of moving it to yet another issue.

@distantnative
Copy link
Member Author

Looks good to me. Tests also only seem to fail because Codecov upload has a hiccup once again

@bastianallgeier bastianallgeier merged commit 3c6e462 into develop-minor Feb 5, 2024
7 of 10 checks passed
@bastianallgeier bastianallgeier deleted the fix/6063-file-sort-performance branch February 5, 2024 11:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs: changes 🔁 Implement any requested changes to proceed type: performance ⚡️ Is about performance; improves performance
Projects
None yet
Development

Successfully merging this pull request may close these issues.

File sorting performance
4 participants