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 performance of ChooseK algorithm #1590

Closed
hatyo opened this issue Apr 6, 2022 · 0 comments · Fixed by #1588
Closed

Improve performance of ChooseK algorithm #1590

hatyo opened this issue Apr 6, 2022 · 0 comments · Fixed by #1588
Assignees
Labels
performance Performance issues

Comments

@hatyo
Copy link
Contributor

hatyo commented Apr 6, 2022

The algorithm is maintaining its state by using few iterators that are allocated and reallocated too many times, the same state could be maintained by simple using integer counters. This could reduce memory consumption and make the algorithm a bit faster.

I wrote a simple synthetic benchmark to verify this, the benchmark uses chooseK to choose 10 items from a set of 28 (~13 millions combinations), and repeats for ten times. Here is the benchmark:

@Test
void testChooseKStressOld() {
    final var elementsBuilder = ImmutableSet.<String>builder();
    IntStream.range(1, 28 + 1 /* exclusive */).forEach(i -> elementsBuilder.add("e" + i));
    final Set<String> elements = elementsBuilder.build();
    final EnumeratingIterable<String> combinationsIterable = ChooseK.chooseK(elements, 10);
    int cnt = 0;
    do {
        long numCombinations = 0;
        final EnumeratingIterator<String> iterator = combinationsIterable.iterator();
        while (iterator.hasNext()) {
            iterator.next();
            numCombinations++;
        }
        Assertions.assertEquals(13123110L, numCombinations);
    } while (cnt++ < 10);
}

It showed that indeed we reduce the memory consumption, and get almost 15% performance increase, pictures below shows differential view between current algorithm and the refactored one using only integers of both memory and CPU measurements.

image

image

@hatyo hatyo added the performance Performance issues label Apr 6, 2022
@hatyo hatyo self-assigned this Apr 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performance Performance issues
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant