Giter Club home page Giter Club logo

vavr-benchmark's People

Contributors

danieldietrich avatar dependabot[bot] avatar mincong-h avatar ruslansennov avatar

Watchers

 avatar  avatar  avatar

vavr-benchmark's Issues

Array.permutations() performance

@jest reported performance degradation of Array.permutations() in vavr:0.9.0. It seems the problem commit is 9e37950e2fa3113cba6fbcd03feebd1fc86ca1e1

Here is (slightly modified for testing) old code:

public static <T> Array<Array<T>> permOld(Array<T> array) {
    if (array.isEmpty()) {
        return Array.empty();
    } else {
        final Array<T> tail = array.tail();
        if (tail.isEmpty()) {
            return Array.of(array);
        } else {
            final Array<Array<T>> zero = Array.empty();
            return array.distinct().foldLeft(zero, (xs, x) -> {
                final Function<Array<T>, Array<T>> prepend = l -> l.prepend(x);
                return xs.appendAll(array.remove(x).permutations().map(prepend));
            });
        }
    }
}

And here is new code:

public static <T> Array<Array<T>> permNew(Array<T> array) {
    if (array.isEmpty()) {
        return Array.empty();
    } else {
        Array<Array<T>> results = Array.empty();
        for (T t : array.distinct()) {
            for (Array<T> ts : array.remove(t).permutations()) {
                results = results.append(Array.of(t).appendAll(ts));
            }
        }
        return results;
    }
}

Return same instance instead of wrapping Iterator results when possible

  1. Remove the method useIsEqualToInsteadOfIsSameAs() from unit tests and test for isSameAs()
  2. fix broken unit tests

Example: take(0)

// AbstractTraversableTest
@Test
public void shouldTakeNoneOnNil() {
    if (useIsEqualToInsteadOfIsSameAs()) {
        assertThat(empty().take(1)).isEqualTo(empty());
    } else {
        assertThat(empty().take(1)).isSameAs(empty());
    }
}
// PriorityQueue (current)
@Override
public PriorityQueue<T> take(int n) {
    return ofAll(comparator, iterator().take(n));
}

// PriorityQueue (time and mem saver)
@Override
public PriorityQueue<T> take(int n) {
    if (n <= 0) {
        return this;
    } else if (n >= size()) {
        return empty(comparator());
    } else {
        return ofAll(comparator, iterator().take(n));
    }
}

This was only an example. We have many similar occurrences.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.