test/micro/org/openjdk/bench/java/util/HashMapBench.java
author rehn
Fri, 29 Nov 2019 12:09:25 +0100
changeset 59325 3636bab5e81e
parent 53710 49adf961fcb1
permissions -rw-r--r--
8234086: VM operation can be simplified Reviewed-by: kbarrett, dholmes, dcubed
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
53710
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
     1
/*
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
     2
 * Copyright (c) 2018, Red Hat, Inc. All rights reserved.
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
     3
 *
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
     4
 * This code is free software; you can redistribute it and/or modify it
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
     5
 * under the terms of the GNU General Public License version 2 only, as
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
     6
 * published by the Free Software Foundation.
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
     7
 *
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
     8
 * This code is distributed in the hope that it will be useful, but WITHOUT
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
     9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    10
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    11
 * version 2 for more details (a copy is included in the LICENSE file that
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    12
 * accompanied this code).
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    13
 *
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License version
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    15
 * 2 along with this work; if not, write to the Free Software Foundation,
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    16
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    17
 *
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    18
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    19
 * or visit www.oracle.com if you need additional information or have any
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    20
 * questions.
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    21
 */
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    22
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    23
package org.openjdk.bench.java.util;
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    24
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    25
import org.openjdk.jmh.annotations.Benchmark;
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    26
import org.openjdk.jmh.annotations.BenchmarkMode;
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    27
import org.openjdk.jmh.annotations.Mode;
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    28
import org.openjdk.jmh.annotations.OutputTimeUnit;
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    29
import org.openjdk.jmh.annotations.Param;
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    30
import org.openjdk.jmh.annotations.Scope;
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    31
import org.openjdk.jmh.annotations.Setup;
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    32
import org.openjdk.jmh.annotations.State;
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    33
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    34
import java.util.HashMap;
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    35
import java.util.LinkedHashMap;
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    36
import java.util.Map;
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    37
import java.util.concurrent.ThreadLocalRandom;
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    38
import java.util.concurrent.TimeUnit;
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    39
import java.util.function.Supplier;
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    40
import java.util.stream.IntStream;
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    41
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    42
import static java.util.stream.Collectors.toMap;
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    43
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    44
@BenchmarkMode(Mode.AverageTime)
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    45
@OutputTimeUnit(TimeUnit.MILLISECONDS)
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    46
@State(Scope.Thread)
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    47
public class HashMapBench {
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    48
    private Supplier<Map<Integer, Integer>> mapSupplier;
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    49
    private Map<Integer, Integer> bigMapToAdd;
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    50
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    51
    @Param("1000000")
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    52
    private int size;
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    53
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    54
    @Param
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    55
    private MapType mapType;
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    56
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    57
    public enum MapType {
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    58
        HASH_MAP,
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    59
        LINKED_HASH_MAP,
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    60
    }
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    61
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    62
    @Setup
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    63
    public void setup() {
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    64
        switch (mapType) {
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    65
        case HASH_MAP:
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    66
            mapSupplier = () -> new HashMap<>();
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    67
            break;
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    68
        case LINKED_HASH_MAP:
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    69
            mapSupplier = () -> new LinkedHashMap<>();
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    70
            break;
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    71
        default:
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    72
            throw new AssertionError();
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    73
        }
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    74
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    75
        ThreadLocalRandom rnd = ThreadLocalRandom.current();
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    76
        this.bigMapToAdd = IntStream.range(0, size).boxed()
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    77
            .collect(toMap(i -> 7 + i * 128, i -> rnd.nextInt()));
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    78
    }
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    79
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    80
    @Benchmark
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    81
    public int putAllWithBigMapToNonEmptyMap() {
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    82
        Map<Integer, Integer> map = mapSupplier.get();
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    83
        map.put(-1, -1);
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    84
        map.putAll(bigMapToAdd);
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    85
        return map.size();
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    86
    }
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    87
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    88
    @Benchmark
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    89
    public int putAllWithBigMapToEmptyMap() {
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    90
        Map<Integer, Integer> map = mapSupplier.get();
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    91
        map.putAll(bigMapToAdd);
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    92
        return map.size();
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    93
    }
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    94
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    95
    @Benchmark
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    96
    public int put() {
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    97
        Map<Integer, Integer> map = mapSupplier.get();
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    98
        for (int k : bigMapToAdd.keySet()) {
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
    99
            map.put(k, bigMapToAdd.get(k));
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
   100
        }
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
   101
        return map.size();
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
   102
    }
49adf961fcb1 8210280: Unnecessary reallocation when invoking HashMap.putAll()
dl
parents:
diff changeset
   103
}