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

ZOOKEEPER-2817: Using Collections.singletonList instead of Arrays.asList(oneElement) #290

Closed
wants to merge 1 commit into from
Closed

Conversation

@asdf2014
Copy link
Member

@asdf2014 asdf2014 commented Jun 22, 2017

Using Collections.singletonList instead of Arrays.asList(oneElement) for reusing a immutable object instead of creating a new object.

@asdf2014 asdf2014 changed the title Using Collections.singletonList instead of Arrays.asList(oneElement) ZOOKEEPER-2817: Using Collections.singletonList instead of Arrays.asList(oneElement) Jun 22, 2017
Copy link
Contributor

@afine afine left a comment

This seems like a very small optimization for a part of the code that is not performance sensitive. The change creates inconsistencies in Commands.java between the commands without much explanation as to why it is necessary. @asdf2014 would you mind explaining why this change is necessary?

@asdf2014
Copy link
Member Author

@asdf2014 asdf2014 commented Jun 30, 2017

Hi, @afine. Thank you for code review. It maybe due to...

// -ea -Xmx512M -Xms512M -Xmn256M -XX:+AlwaysPreTouch
@Test
public void testSingletonListPerformance() {
    String site = "yuzhouwan.com";
    int count = 1_0000_0000;
    long spendTime = 0;
    while (count > 0) {
        long startTime = System.nanoTime();
        Collections.singleton(site);
        long endTime = System.nanoTime();
        count--;
        spendTime += endTime - startTime;
    }
    Runtime runtime = Runtime.getRuntime();
    long totalMemory = runtime.totalMemory() / 1024 / 1024;
    long freeMemory = runtime.freeMemory() / 1024 / 1024;
    // [Collections.singleton] Spend Time: 1370786852ns = 1370.786852ms, Mem: 39/441/480 MB (diff/free/total)
    _log.info("[Collections.singleton] Spend Time: {}ns = {}ms, Mem: {}/{}/{} MB (diff/free/total)",
            spendTime, spendTime / Math.pow(10, 6), totalMemory - freeMemory, freeMemory, totalMemory);
}

// -ea -Xmx512M -Xms512M -Xmn256M -XX:+AlwaysPreTouch
@Test
public void testArraysAsListPerformance() {
    String site = "yuzhouwan.com";
    int count = 1_0000_0000;
    long spendTime = 0;
    while (count > 0) {
        long startTime = System.nanoTime();
        Arrays.asList(site);
        long endTime = System.nanoTime();
        count--;
        spendTime += endTime - startTime;
    }
    Runtime runtime = Runtime.getRuntime();
    long totalMemory = runtime.totalMemory() / 1024 / 1024;
    long freeMemory = runtime.freeMemory() / 1024 / 1024;
    // [Arrays.asList] Spend Time: 1549508768ns = 1549.508768ms, Mem: 195/312/507 MB (diff/free/total)
    _log.info("[Arrays.asList] Spend Time: {}ns = {}ms, Mem: {}/{}/{} MB (diff/free/total)",
            spendTime, spendTime / Math.pow(10, 6), totalMemory - freeMemory, freeMemory, totalMemory);
}

Full code is here.

@anmolnar anmolnar closed this Jan 30, 2019
@asdf2014 asdf2014 deleted the asdf2014:coll_stuff branch Jan 31, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants