Use FFD algorithm for ArrayUtil.joinWithMax #219
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
It reduces the number of queries, especially the user has many watching repositories.
Problem
In my environment, the watching queries are too many because I watch 1000+ repositories.
It introduces a serious performance issue. Because Jasper executes one query per one iteration. So if it has 100 queries, Jasper needs 100 iterations to execute all 100 queries.
Solution
Improve the algorithm to pack queries.
I replace the
joinWithMax
implementation with FFD (First Fit Decreasing) algorithm.The algorithm is super simple.
values
by decreasing order by length.['short', 'looooong']
to['looooong', 'short']
values
toresult
with the following rules.result
has the capacity, push thevalue
to theresult
.Else, try the next
result
.result
has no capacity for thevalue
, allocate newresult
.This algorithm is an approximation algorithm, but it is better than the current naive implementation, and it is enough I think.
See this article for more details. https://en.wikipedia.org/wiki/Bin_packing_problem
Benchmark
I benchmarked my watching query. By this change, the watching queries count is reduced from 121 to 114. It is still super large but better a little.
I benchmarked with the following code: