-
Notifications
You must be signed in to change notification settings - Fork 9.3k
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
Properly compute module load order #10649
Properly compute module load order #10649
Conversation
Use toposort for properly determining the order in which modules should be loaded
Did you compare an actual execution time of old vs new implementation? Unfortunately proposed implementation is still Proper implementation may be based on https://e-maxx-eng.appspot.com/graph/topological-sort.html or simply involve some existing library like https://github.com/marcj/topsort.php. |
@orlangur Thank you for your feedback! Yes, I measured some significant differences between the original bubble sort implementation and my topological sort implementation. Before getting into the micro-benchmark results, I'd like to briefly mention that the goal of this sorting implementation is to order the modules more accurately, and not necessarily improve the speed in which the dependency graph is sorted. That said, outperforming the time complexity of bubble sort is trivial; just about any other algorithm can do better. Fortunately for us, Kahn's algorithm is not, in fact, an I measured the unit tests (where there are some very small graphs, 3 and 5 nodes each), and then I measured a real production Magento 2 store (containing 119 nodes). The results below are the average of three runs for each sorting algorithm. Bubble Sort
Toposort
|
I know the complexity of Kahn's algorithm, the problem is your implementation is Thanks for the benchmark, I do agree that the main point here is to get rid of buggy ordering, just wanted to be sure new implementation is not slower. Could you please provide a gist with patch for such measurement?
Automated test case needs to be added which reveals such bug in old implementation. Ideally if it will be the unit test. What do you think about https://github.com/marcj/topsort.php? Not necessary to fully include the library, for example, we can borrow just |
I'm not sure I agree with this, what part of the implementation is
I do agree with this; I'm working on trimming down a large production case that can demonstrate improper sorting, and I will add it as a unit test.
I strayed away from adding this dependency, for two reasons:
|
At least
Thanks, especially for the old links, I remember reading it before and was surprised there is no topological sorting in Magento Framework yet (I thought it was merged). Will look deeper into it tomorrow. Could you provide a benchmarking gist? Wanna do a quick check of that library speed compared to current PR. |
Thanks for the information about
I will provide a link to a gist with some benchmark patches. |
@korostii how using topological sort instead of bubble sort will make order deterministic? |
@orlangur, I am sorry if I lead to some confusion on this part. I might've misunderstood the point of changes done here when I skimmed through the PR for the first time. Here's what I was basing that assumption on: if you take a look at that issue this PR claims to solve, which is #10112, the issue described seems to be the following: Now that I've given it some thought: why not make that broken dependency explicit instead (via adding I now suspect that solving #10112 is just a side-effect of changing the algorithm and it might not work consistently (given different conflicting modules). Could maybe @t-richards shine some explanation into how this all is tied together? |
@korostii, see https://en.wikipedia.org/wiki/Sorting_algorithm#Stability. As you can see bubble sort is stable, I believe the order could be random just because modules are randomly read from filesystem. So, by design we will not get any stability from topological sort usage instead of bubble sort. With something like https://stackoverflow.com/questions/11230881/stable-topological-sort/11236027#11236027 we can get it (if we make the input deterministic, of course), but it will add a
Modules in sequence must be specified according to business logic only. I'm not even sure 8479 is even valid, will share my thoughts there. |
No, modules in sequence should be specified according to business logic only. In theory. A couple examples: So, the Tax module should either learn to live without these or declare these dependencies outright. |
Just noticed: Magento_Tax/Magento_Checkout dependency is already added on 2.2.0 branch already; I am now sure this PR cannot fix #10112 on branch Additionally, it is my understanding that module sorting is only performed during some CLI commands ( @t-richards, could you please elaborate on\explain the benefits of this change? The PR description seems to be lacking on that part right now. |
What I meant is that we should not add unreasonable nodes to Too bad eca1579 happened in scope of some random bugfixing and not solved for the overall system yet. As you can see,
Let me quote the PR author:
So, looks like in some cases sorting did not correspond to |
@orlangur Thanks for pointing out the stable topological sort, I will do some more research on this and see how it compares. Despite many attempts, I was not able to produce any test cases which demonstrated an incorrect sort of the modules using the original sorting method. This makes me wonder whether there are module dependency issue(s) of a different kind. For now, I suppose I'll have to live with bubble sort. If I happen to come up with a better set of test cases, I'll make a new PR. |
Description
This PR replaces the ... interesting ... choice of bubble sort for determining the order in which modules should be loaded.
The
sortBySequence
method now uses topological sorting to correctly linearize the sequence of modules. The topological sort algorithm implemented in theMagento\Framework\Data\Graph
data structure is Kahn's algorithm.Fixed Issues (if relevant)
Manual testing scenarios
Magento_Customer
Magento_Catalog
, causing all sorts of trouble.Contribution checklist
The code is covered by existing unit tests. More tests can be added if desired.