src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/PropertyHashMap.java
author hannesw
Wed, 21 Mar 2018 16:55:34 +0100
changeset 49275 c639a6b33c5c
parent 47351 fff3970bd14f
permissions -rw-r--r--
8199869: Missing copyright headers in nashorn source code Reviewed-by: sundar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     1
/*
16151
97c1e756ae1e 8005663: Update copyright year to 2013
jlaskey
parents: 16147
diff changeset
     2
 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     4
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    10
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    15
 * accompanied this code).
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    16
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    20
 *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    23
 * questions.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    24
 */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    25
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    26
package jdk.nashorn.internal.runtime;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    27
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    28
import java.util.Arrays;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    29
import java.util.Collection;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    30
import java.util.Collections;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    31
import java.util.HashSet;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    32
import java.util.Map;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    33
import java.util.Set;
47351
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
    34
import jdk.nashorn.internal.runtime.options.Options;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    35
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    36
/**
47351
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
    37
 * Immutable hash map implementation for properties. Properties are keyed on strings
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
    38
 * or symbols (ES6). Copying and cloning is avoided by relying on immutability.
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    39
 * <p>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    40
 * When adding an element to a hash table, only the head of a bin list is updated, thus
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    41
 * an add only requires the cloning of the bins array and adding an element to the head
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    42
 * of the bin list.  Similarly for removal, only a portion of a bin list is updated.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    43
 * <p>
47351
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
    44
 * For large tables with hundreds or thousands of elements, even just cloning the bins
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
    45
 * array when adding properties is an expensive operation. For this case, we put new
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
    46
 * elements in a separate list called {@link ElementQueue}.
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
    47
 * The list component is merged into the hash table at regular intervals during element
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
    48
 * insertion to keep it from growing too long. Also, when a map with a queue component
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
    49
 * is queried repeatedly, the map will replace itself with a pure hash table version
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
    50
 * of itself to optimize lookup performance.
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
    51
 * <p>
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    52
 * A separate chronological list is kept for quick generation of keys and values, and,
47351
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
    53
 * for rehashing. For very small maps where the overhead of the hash table would
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
    54
 * outweigh its benefits we deliberately avoid creating a hash structure and use the
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
    55
 * chronological list alone for element storage.
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    56
 * <p>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    57
 * Details:
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    58
 * <p>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    59
 * The main goal is to be able to retrieve properties from a map quickly, keying on
47351
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
    60
 * the property name (String or Symbol). A secondary, but important goal, is to keep
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
    61
 * maps immutable, so that a map can be shared by multiple objects in a context.
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    62
 * Sharing maps allows objects to be categorized as having similar properties, a
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    63
 * fact that call site guards rely on.  In this discussion, immutability allows us
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    64
 * to significantly reduce the amount of duplication we have in our maps.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    65
 * <p>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    66
 * The simplest of immutable maps is a basic singly linked list.  New properties
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    67
 * are simply added to the head of the list.  Ancestor maps are not affected by the
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    68
 * addition, since they continue to refer to their own head.  Searching is done by
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    69
 * walking linearly though the elements until a match is found, O(N).
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    70
 * <p>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    71
 * A hash map can be thought of as an optimization of a linked list map, where the
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    72
 * linked list is broken into fragments based on hashCode(key) .  An array is use
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    73
 * to quickly reference these fragments, indexing on hashCode(key) mod tableSize
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    74
 * (tableSize is typically a power of 2 so that the mod is a fast masking
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    75
 * operation.)  If the size of the table is sufficient large, then search time
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    76
 * approaches O(1).  In fact, most bins in a hash table are typically empty or
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    77
 * contain a one element list.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    78
 * <p>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    79
 * For immutable hash maps, we can think of the hash map as an array of the shorter
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    80
 * linked list maps.  If we add an element to the head of one of those lists,  it
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    81
 * doesn't affect any ancestor maps.  Thus adding an element to an immutable hash
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    82
 * map only requires cloning the array and inserting an element at the head of one
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    83
 * of the bins.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    84
 * <p>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    85
 * Using Java HashMaps we don't have enough control over the entries to allow us to
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    86
 * implement this technique, so we are forced to clone the entire hash map.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    87
 * <p>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    88
 * Removing elements is done similarly.  We clone the array and then only modify
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    89
 * the bin containing the removed element.  More often than not, the list contains
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    90
 * only one element (or is very short), so this is not very costly.  When the list
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    91
 * has several items, we need to clone the list portion prior to the removed item.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    92
 * <p>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    93
 * Another requirement of property maps is that we need to be able to gather all
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    94
 * properties in chronological (add) order.  We have been using LinkedHashMap to
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    95
 * provide this.  For the implementation of immutable hash map, we use a singly
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    96
 * linked list that is linked in reverse chronological order.  This means we simply
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    97
 * add new entries to the head of the list.  If we need to work with the list in
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    98
 * forward order, it's simply a matter of allocating an array (size is known) and
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
    99
 * back filling in reverse order.  Removal of elements from the chronological list
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   100
 * is trickier.  LinkedHashMap uses a doubly linked list to give constant time
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   101
 * removal. Immutable hash maps can't do that and maintain immutability.  So we
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   102
 * manage the chronological list the same way we manage the bins, cloning up to the
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   103
 * point of removal.  Don't panic.  This cost is more than offset by the cost of
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   104
 * cloning an entire LinkedHashMap.  Plus removal is far more rare than addition.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   105
 * <p>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   106
 * One more optimization.  Maps with a small number of entries don't use the hash
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   107
 * map at all, the chronological list is used instead.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   108
 * <p>
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   109
 * So the benefits from immutable arrays are; fewer objects and less copying.  For
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   110
 * immutable hash map, when no removal is involved, the number of elements per
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   111
 * property is two (bin + chronological elements).  For LinkedHashMap it is one
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   112
 * (larger element) times the number of maps that refer to the property.  For
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   113
 * immutable hash map, addition is constant time.  For LinkedHashMap it's O(N+C)
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   114
 * since we have to clone the older map.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   115
 */
33690
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents: 25865
diff changeset
   116
public final class PropertyHashMap implements Map <Object, Property> {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   117
    /** Number of initial bins. Power of 2. */
16758
4f7379c41907 8011095: PropertyHashMap.rehash() does not grow enough
jlaskey
parents: 16205
diff changeset
   118
    private static final int INITIAL_BINS = 32;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   119
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   120
    /** Threshold before using bins. */
16758
4f7379c41907 8011095: PropertyHashMap.rehash() does not grow enough
jlaskey
parents: 16205
diff changeset
   121
    private static final int LIST_THRESHOLD = 8;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   122
47351
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   123
    /** Threshold before adding new elements to queue instead of directly adding to hash bins. */
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   124
    private static final int QUEUE_THRESHOLD = Options.getIntProperty("nashorn.propmap.queue.threshold", 500);
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   125
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   126
    /** Initial map. */
17513
b9a691fc1df5 8006220: Simplify PropertyMaps
jlaskey
parents: 16758
diff changeset
   127
    public static final PropertyHashMap EMPTY_HASHMAP = new PropertyHashMap();
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   128
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   129
    /** Number of properties in the map. */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   130
    private final int size;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   131
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   132
    /** Threshold before growing the bins. */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   133
    private final int threshold;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   134
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   135
    /** Reverse list of all properties. */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   136
    private final Element list;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   137
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   138
    /** Hash map bins. */
47351
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   139
    private Element[] bins;
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   140
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   141
    /** Queue for adding elements to large maps with delayed hashing. */
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   142
    private ElementQueue queue;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   143
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   144
    /** All properties as an array (lazy). */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   145
    private Property[] properties;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   146
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   147
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   148
     * Empty map constructor.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   149
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   150
    private PropertyHashMap() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   151
        this.size      = 0;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   152
        this.threshold = 0;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   153
        this.bins      = null;
47351
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   154
        this.queue     = null;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   155
        this.list      = null;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   156
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   157
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   158
    /**
47351
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   159
     * Constructor used internally to create new maps
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   160
     *
47351
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   161
     * @param map the new map
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   162
     */
47351
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   163
    private PropertyHashMap(final MapBuilder map) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   164
        this.size      = map.size;
47351
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   165
        if (map.qhead == null) {
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   166
            this.bins = map.bins;
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   167
            this.queue = null;
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   168
        } else {
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   169
            this.bins = null;
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   170
            this.queue = new ElementQueue(map.qhead, map.bins);
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   171
        }
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   172
        this.list      = map.list;
47351
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   173
        this.threshold = map.bins != null ? threeQuarters(map.bins.length) : 0;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   174
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   175
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   176
    /**
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 17513
diff changeset
   177
     * Clone a property map, replacing a property with a new one in the same place,
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 17513
diff changeset
   178
     * which is important for property iterations if a property changes types
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 17513
diff changeset
   179
     * @param property    old property
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 17513
diff changeset
   180
     * @param newProperty new property
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 17513
diff changeset
   181
     * @return new property map
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 17513
diff changeset
   182
     */
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 17513
diff changeset
   183
    public PropertyHashMap immutableReplace(final Property property, final Property newProperty) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 17513
diff changeset
   184
        assert property.getKey().equals(newProperty.getKey()) : "replacing properties with different keys: '" + property.getKey() + "' != '" + newProperty.getKey() + "'";
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 17513
diff changeset
   185
        assert findElement(property.getKey()) != null         : "replacing property that doesn't exist in map: '" + property.getKey() + "'";
47351
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   186
        final MapBuilder builder = newMapBuilder(size);
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   187
        builder.replaceProperty(property.getKey(), newProperty);
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   188
        return new PropertyHashMap(builder);
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 17513
diff changeset
   189
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 17513
diff changeset
   190
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 17513
diff changeset
   191
    /**
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   192
     * Clone a {@link PropertyHashMap} and add a {@link Property}.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   193
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   194
     * @param property {@link Property} to add.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   195
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   196
     * @return New {@link PropertyHashMap}.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   197
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   198
    public PropertyHashMap immutableAdd(final Property property) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   199
        final int newSize = size + 1;
47351
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   200
        MapBuilder builder = newMapBuilder(newSize);
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   201
        builder.addProperty(property);
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   202
        return new PropertyHashMap(builder);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   203
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   204
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   205
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   206
     * Clone a {@link PropertyHashMap} and add an array of properties.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   207
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   208
     * @param newProperties Properties to add.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   209
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   210
     * @return New {@link PropertyHashMap}.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   211
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   212
    public PropertyHashMap immutableAdd(final Property... newProperties) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   213
        final int newSize = size + newProperties.length;
47351
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   214
        MapBuilder builder = newMapBuilder(newSize);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   215
        for (final Property property : newProperties) {
47351
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   216
            builder.addProperty(property);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   217
        }
47351
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   218
        return new PropertyHashMap(builder);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   219
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   220
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   221
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   222
     * Clone a {@link PropertyHashMap} and add a collection of properties.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   223
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   224
     * @param newProperties Properties to add.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   225
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   226
     * @return New {@link PropertyHashMap}.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   227
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   228
    public PropertyHashMap immutableAdd(final Collection<Property> newProperties) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   229
        if (newProperties != null) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   230
            final int newSize = size + newProperties.size();
47351
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   231
            MapBuilder builder = newMapBuilder(newSize);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   232
            for (final Property property : newProperties) {
47351
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   233
                builder.addProperty(property);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   234
            }
47351
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   235
            return new PropertyHashMap(builder);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   236
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   237
        return this;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   238
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   239
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   240
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   241
     * Clone a {@link PropertyHashMap} and remove a {@link Property} based on its key.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   242
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   243
     * @param key Key of {@link Property} to remove.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   244
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   245
     * @return New {@link PropertyHashMap}.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   246
     */
33690
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents: 25865
diff changeset
   247
    public PropertyHashMap immutableRemove(final Object key) {
47351
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   248
        MapBuilder builder = newMapBuilder(size);
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   249
        builder.removeProperty(key);
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   250
        if (builder.size < size) {
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   251
            return builder.size != 0 ? new PropertyHashMap(builder) : EMPTY_HASHMAP;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   252
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   253
        return this;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   254
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   255
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   256
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   257
     * Find a {@link Property} in the {@link PropertyHashMap}.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   258
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   259
     * @param key Key of {@link Property} to find.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   260
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   261
     * @return {@link Property} matching key or {@code null} if not found.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   262
     */
33690
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents: 25865
diff changeset
   263
    public Property find(final Object key) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   264
        final Element element = findElement(key);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   265
        return element != null ? element.getProperty() : null;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   266
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   267
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   268
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   269
     * Return an array of properties in chronological order of adding.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   270
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   271
     * @return Array of all properties.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   272
     */
16205
93fda2507e35 8007286: Add JavaAdapter and importPackage to compatibility script
sundar
parents: 16179
diff changeset
   273
    Property[] getProperties() {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   274
        if (properties == null) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   275
            final Property[] array = new Property[size];
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   276
            int i = size;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   277
            for (Element element = list; element != null; element = element.getLink()) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   278
                array[--i] = element.getProperty();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   279
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   280
            properties = array;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   281
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   282
        return properties;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   283
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   284
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   285
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   286
     * Returns the bin index from the key.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   287
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   288
     * @param bins     The bins array.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   289
     * @param key      {@link Property} key.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   290
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   291
     * @return The bin index.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   292
     */
33690
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents: 25865
diff changeset
   293
    private static int binIndex(final Element[] bins, final Object key) {
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   294
        return  key.hashCode() & bins.length - 1;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   295
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   296
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   297
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   298
     * Calculate the number of bins needed to contain n properties.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   299
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   300
     * @param n Number of elements.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   301
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   302
     * @return Number of bins required.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   303
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   304
    private static int binsNeeded(final int n) {
16758
4f7379c41907 8011095: PropertyHashMap.rehash() does not grow enough
jlaskey
parents: 16205
diff changeset
   305
        // 50% padding
24720
75f8388b79df 8035836: Array performance improvements
lagergren
parents: 24719
diff changeset
   306
        return 1 << 32 - Integer.numberOfLeadingZeros(n + (n >>> 1) | INITIAL_BINS - 1);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   307
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   308
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   309
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   310
     * Used to calculate the current capacity of the bins.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   311
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   312
     * @param n Number of bin slots.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   313
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   314
     * @return 75% of n.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   315
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   316
    private static int threeQuarters(final int n) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   317
        return (n >>> 1) + (n >>> 2);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   318
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   319
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   320
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   321
     * Regenerate the bin table after changing the number of bins.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   322
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   323
     * @param list    // List of all properties.
16758
4f7379c41907 8011095: PropertyHashMap.rehash() does not grow enough
jlaskey
parents: 16205
diff changeset
   324
     * @param binSize // New size of bins.
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   325
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   326
     * @return Populated bins.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   327
     */
16758
4f7379c41907 8011095: PropertyHashMap.rehash() does not grow enough
jlaskey
parents: 16205
diff changeset
   328
    private static Element[] rehash(final Element list, final int binSize) {
4f7379c41907 8011095: PropertyHashMap.rehash() does not grow enough
jlaskey
parents: 16205
diff changeset
   329
        final Element[] newBins = new Element[binSize];
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   330
        for (Element element = list; element != null; element = element.getLink()) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   331
            final Property property = element.getProperty();
33690
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents: 25865
diff changeset
   332
            final Object   key      = property.getKey();
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 17513
diff changeset
   333
            final int      binIndex = binIndex(newBins, key);
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 17513
diff changeset
   334
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   335
            newBins[binIndex] = new Element(newBins[binIndex], property);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   336
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   337
        return newBins;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   338
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   339
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   340
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   341
     * Locate an element based on key.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   342
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   343
     * @param key {@link Element} key.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   344
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   345
     * @return {@link Element} matching key or {@code null} if not found.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   346
     */
33690
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents: 25865
diff changeset
   347
    private Element findElement(final Object key) {
47351
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   348
        if (queue != null) {
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   349
            return queue.find(key);
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   350
        } else if (bins != null) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   351
            final int binIndex = binIndex(bins, key);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   352
            return findElement(bins[binIndex], key);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   353
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   354
        return findElement(list, key);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   355
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   356
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   357
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   358
     * Locate an {@link Element} based on key from a specific list.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   359
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   360
     * @param elementList Head of {@link Element} list
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   361
     * @param key         {@link Element} key.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   362
     * @return {@link Element} matching key or {@code null} if not found.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   363
     */
33690
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents: 25865
diff changeset
   364
    private static Element findElement(final Element elementList, final Object key) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   365
        final int hashCode = key.hashCode();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   366
        for (Element element = elementList; element != null; element = element.getLink()) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   367
            if (element.match(key, hashCode)) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   368
                return element;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   369
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   370
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   371
        return null;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   372
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   373
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   374
    /**
47351
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   375
     * Create a {@code MapBuilder} to add new elements to.
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   376
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   377
     * @param newSize New size of {@link PropertyHashMap}.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   378
     *
47351
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   379
     * @return {@link MapBuilder} for the new size.
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   380
     */
47351
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   381
    private MapBuilder newMapBuilder(final int newSize) {
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   382
        if (bins == null && newSize < LIST_THRESHOLD) {
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   383
            return new MapBuilder(bins, list, size, false);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   384
        } else if (newSize > threshold) {
47351
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   385
            return new MapBuilder(rehash(list, binsNeeded(newSize)), list, size, true);
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   386
        } else if (shouldCloneBins(size, newSize)) {
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   387
            return new MapBuilder(cloneBins(), list, size, true);
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   388
        } else if (queue == null) {
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   389
            return new MapBuilder(bins, list, size, false);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   390
        } else {
47351
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   391
            return new MapBuilder(queue, list, size, false);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   392
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   393
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   394
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   395
    /**
47351
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   396
     * Create a cloned or new bins array and merge the elements in the queue into it if there are any.
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   397
     *
47351
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   398
     * @return the cloned bins array
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   399
     */
47351
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   400
    private Element[] cloneBins() {
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   401
        if (queue != null) {
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   402
            return queue.cloneAndMergeBins();
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   403
        }
47351
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   404
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   405
        return bins.clone();
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   406
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   407
47351
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   408
    /**
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   409
     * Used on insertion to determine whether the bins array should be cloned, or we should keep
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   410
     * using the ancestor's bins array and put new elements into the queue.
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   411
     *
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   412
     * @param oldSize the old map size
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   413
     * @param newSize the new map size
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   414
     * @return whether to clone the bins array
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   415
     */
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   416
    private boolean shouldCloneBins(final int oldSize, final int newSize) {
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   417
        // For maps with less than QUEUE_THRESHOLD elements we clone the bins array on every insertion.
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   418
        // Above that threshold we put new elements into the queue and only merge every 512  elements.
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   419
        return newSize < QUEUE_THRESHOLD || (newSize >>> 9) > (oldSize >>> 9);
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 17513
diff changeset
   420
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 17513
diff changeset
   421
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   422
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   423
     * Removes an {@link Element} from a specific list, avoiding duplication.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   424
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   425
     * @param list List to remove from.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   426
     * @param key  Key of {@link Element} to remove.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   427
     *
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   428
     * @return New list with {@link Element} removed.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   429
     */
33690
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents: 25865
diff changeset
   430
    private static Element removeFromList(final Element list, final Object key) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   431
        if (list == null) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   432
            return null;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   433
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   434
        final int hashCode = key.hashCode();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   435
        if (list.match(key, hashCode)) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   436
            return list.getLink();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   437
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   438
        final Element head = new Element(null, list.getProperty());
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   439
        Element previous = head;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   440
        for (Element element = list.getLink(); element != null; element = element.getLink()) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   441
            if (element.match(key, hashCode)) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   442
                previous.setLink(element.getLink());
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   443
                return head;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   444
            }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   445
            final Element next = new Element(null, element.getProperty());
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   446
            previous.setLink(next);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   447
            previous = next;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   448
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   449
        return list;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   450
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   451
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 17513
diff changeset
   452
    // for element x. if x get link matches,
33690
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents: 25865
diff changeset
   453
    private static Element replaceInList(final Element list, final Object key, final Property property) {
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 17513
diff changeset
   454
        assert list != null;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 17513
diff changeset
   455
        final int hashCode = key.hashCode();
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 17513
diff changeset
   456
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 17513
diff changeset
   457
        if (list.match(key, hashCode)) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 17513
diff changeset
   458
            return new Element(list.getLink(), property);
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 17513
diff changeset
   459
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 17513
diff changeset
   460
24734
da070553a8e1 8038799: Guard and unbox boxed primitives types on setting them in Properties to avoid megamorphisism
lagergren
parents: 24720
diff changeset
   461
        final Element head = new Element(null, list.getProperty());
da070553a8e1 8038799: Guard and unbox boxed primitives types on setting them in Properties to avoid megamorphisism
lagergren
parents: 24720
diff changeset
   462
        Element previous = head;
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 17513
diff changeset
   463
        for (Element element = list.getLink(); element != null; element = element.getLink()) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 17513
diff changeset
   464
            if (element.match(key, hashCode)) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 17513
diff changeset
   465
                previous.setLink(new Element(element.getLink(), property));
24734
da070553a8e1 8038799: Guard and unbox boxed primitives types on setting them in Properties to avoid megamorphisism
lagergren
parents: 24720
diff changeset
   466
                return head;
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 17513
diff changeset
   467
            }
24734
da070553a8e1 8038799: Guard and unbox boxed primitives types on setting them in Properties to avoid megamorphisism
lagergren
parents: 24720
diff changeset
   468
            final Element next = new Element(null, element.getProperty());
da070553a8e1 8038799: Guard and unbox boxed primitives types on setting them in Properties to avoid megamorphisism
lagergren
parents: 24720
diff changeset
   469
            previous.setLink(next);
da070553a8e1 8038799: Guard and unbox boxed primitives types on setting them in Properties to avoid megamorphisism
lagergren
parents: 24720
diff changeset
   470
            previous = next;
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 17513
diff changeset
   471
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 17513
diff changeset
   472
        return list;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 17513
diff changeset
   473
    }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 17513
diff changeset
   474
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 17513
diff changeset
   475
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   476
    /*
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   477
     * Map implementation
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   478
     */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   479
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   480
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   481
    public int size() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   482
        return size;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   483
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   484
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   485
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   486
    public boolean isEmpty() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   487
        return size == 0;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   488
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   489
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   490
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   491
    public boolean containsKey(final Object key) {
33690
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents: 25865
diff changeset
   492
        assert key instanceof String || key instanceof Symbol;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   493
        return findElement(key) != null;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   494
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   495
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   496
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   497
    public boolean containsValue(final Object value) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   498
        if (value instanceof Property) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   499
            final Property property = (Property) value;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   500
            final Element element = findElement(property.getKey());
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   501
            return element != null && element.getProperty().equals(value);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   502
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   503
        return false;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   504
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   505
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   506
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   507
    public Property get(final Object key) {
33690
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents: 25865
diff changeset
   508
        assert key instanceof String || key instanceof Symbol;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   509
        final Element element = findElement(key);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   510
        return element != null ? element.getProperty() : null;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   511
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   512
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   513
    @Override
33690
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents: 25865
diff changeset
   514
    public Property put(final Object key, final Property value) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   515
        throw new UnsupportedOperationException("Immutable map.");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   516
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   517
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   518
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   519
    public Property remove(final Object key) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   520
        throw new UnsupportedOperationException("Immutable map.");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   521
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   522
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   523
    @Override
33690
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents: 25865
diff changeset
   524
    public void putAll(final Map<? extends Object, ? extends Property> m) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   525
        throw new UnsupportedOperationException("Immutable map.");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   526
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   527
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   528
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   529
    public void clear() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   530
        throw new UnsupportedOperationException("Immutable map.");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   531
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   532
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   533
    @Override
33690
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents: 25865
diff changeset
   534
    public Set<Object> keySet() {
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents: 25865
diff changeset
   535
        final HashSet<Object> set = new HashSet<>();
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   536
        for (Element element = list; element != null; element = element.getLink()) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   537
            set.add(element.getKey());
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   538
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   539
        return Collections.unmodifiableSet(set);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   540
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   541
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   542
    @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   543
    public Collection<Property> values() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   544
        return Collections.unmodifiableList(Arrays.asList(getProperties()));
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   545
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   546
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   547
    @Override
33690
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents: 25865
diff changeset
   548
    public Set<Entry<Object, Property>> entrySet() {
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents: 25865
diff changeset
   549
        final HashSet<Entry<Object, Property>> set = new HashSet<>();
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   550
        for (Element element = list; element != null; element = element.getLink()) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   551
            set.add(element);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   552
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   553
        return Collections.unmodifiableSet(set);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   554
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   555
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   556
    /**
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   557
     * List map element.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   558
     */
33690
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents: 25865
diff changeset
   559
    static final class Element implements Entry<Object, Property> {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   560
        /** Link for list construction. */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   561
        private Element link;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   562
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   563
        /** Element property. */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   564
        private final Property property;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   565
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   566
        /** Element key. Kept separate for performance.) */
33690
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents: 25865
diff changeset
   567
        private final Object key;
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   568
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   569
        /** Element key hash code. */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   570
        private final int hashCode;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   571
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   572
        /*
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   573
         * Constructors
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   574
         */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   575
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   576
        Element(final Element link, final Property property) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   577
            this.link     = link;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   578
            this.property = property;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   579
            this.key      = property.getKey();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   580
            this.hashCode = this.key.hashCode();
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   581
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   582
33690
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents: 25865
diff changeset
   583
        boolean match(final Object otherKey, final int otherHashCode) {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   584
            return this.hashCode == otherHashCode && this.key.equals(otherKey);
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   585
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   586
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   587
        /*
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   588
         * Entry implmentation.
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   589
         */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   590
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   591
        @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   592
        public boolean equals(final Object other) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   593
            assert property != null && other != null;
16179
5b8bcfd712d3 8006517: PropertyHashMap.Element.equals() compares to Property
jlaskey
parents: 16151
diff changeset
   594
            return other instanceof Element && property.equals(((Element)other).property);
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   595
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   596
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   597
        @Override
33690
46a1bc24cf2c 8141702: Add support for Symbol property keys
hannesw
parents: 25865
diff changeset
   598
        public Object getKey() {
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   599
            return key;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   600
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   601
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   602
        @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   603
        public Property getValue() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   604
            return property;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   605
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   606
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   607
        @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   608
        public int hashCode() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   609
            return hashCode;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   610
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   611
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   612
        @Override
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   613
        public Property setValue(final Property value) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   614
            throw new UnsupportedOperationException("Immutable map.");
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   615
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   616
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 17513
diff changeset
   617
        @Override
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 17513
diff changeset
   618
        public String toString() {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 17513
diff changeset
   619
            final StringBuffer sb = new StringBuffer();
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 17513
diff changeset
   620
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 17513
diff changeset
   621
            sb.append('[');
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 17513
diff changeset
   622
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 17513
diff changeset
   623
            Element elem = this;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 17513
diff changeset
   624
            do {
24734
da070553a8e1 8038799: Guard and unbox boxed primitives types on setting them in Properties to avoid megamorphisism
lagergren
parents: 24720
diff changeset
   625
                sb.append(elem.getValue());
24719
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 17513
diff changeset
   626
                elem = elem.link;
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 17513
diff changeset
   627
                if (elem != null) {
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 17513
diff changeset
   628
                    sb.append(" -> ");
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 17513
diff changeset
   629
                }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 17513
diff changeset
   630
            } while (elem != null);
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 17513
diff changeset
   631
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 17513
diff changeset
   632
            sb.append(']');
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 17513
diff changeset
   633
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 17513
diff changeset
   634
            return sb.toString();
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 17513
diff changeset
   635
        }
f726e9d67629 8035820: Optimistic recompilation
attila
parents: 17513
diff changeset
   636
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   637
        /*
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   638
         * Accessors
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   639
         */
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   640
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   641
        Element getLink() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   642
            return link;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   643
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   644
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   645
        void setLink(final Element link) {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   646
            this.link = link;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   647
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   648
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   649
        Property getProperty() {
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   650
            return property;
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   651
        }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   652
    }
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   653
47351
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   654
    /**
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   655
     * A hybrid map/list structure to add elements to the map without the need to clone and rehash the main table.
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   656
     * This is used for large maps to reduce the overhead of adding elements. Instances of this class can replace
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   657
     * themselves with a pure hash map version of themselves to optimize query performance.
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   658
     */
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   659
    private class ElementQueue {
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   660
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   661
        /** List of elements not merged into bins */
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   662
        private final Element qhead;
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   663
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   664
        /** Our own bins array. Differs from original PropertyHashMap bins when queue is merged. */
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   665
        private final Element[] qbins;
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   666
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   667
        /** Count searches to trigger merging of queue into bins. */
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   668
        int searchCount = 0;
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   669
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   670
        ElementQueue(final Element qhead, final Element[] qbins) {
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   671
            this.qhead = qhead;
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   672
            this.qbins = qbins;
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   673
        }
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   674
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   675
        Element find(final Object key) {
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   676
            final int binIndex = binIndex(qbins, key);
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   677
            final Element element = findElement(qbins[binIndex], key);
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   678
            if (element != null) {
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   679
                return element;
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   680
            }
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   681
            if (qhead != null) {
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   682
                if (++searchCount > 2) {
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   683
                    // Merge the queue into the hash bins if this map is queried more than a few times
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   684
                    final Element[] newBins = cloneAndMergeBins();
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   685
                    assert newBins != qbins;
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   686
                    PropertyHashMap.this.queue = new ElementQueue(null, newBins);
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   687
                    return PropertyHashMap.this.queue.find(key);
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   688
                }
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   689
                return findElement(qhead, key);
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   690
            }
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   691
            return null;
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   692
        }
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   693
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   694
        /**
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   695
         * Create a cloned or new bins array and merge the elements in the queue into it if there are any.
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   696
         *
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   697
         * @return the cloned bins array
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   698
         */
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   699
        private Element[] cloneAndMergeBins() {
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   700
            if (qhead == null) {
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   701
                return qbins;
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   702
            }
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   703
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   704
            final Element[] newBins = qbins.clone();
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   705
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   706
            for (Element element = qhead; element != null; element = element.getLink()) {
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   707
                final Property property = element.getProperty();
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   708
                final Object key = property.getKey();
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   709
                final int binIndex = binIndex(newBins, key);
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   710
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   711
                newBins[binIndex] = new Element(newBins[binIndex], property);
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   712
            }
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   713
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   714
            return newBins;
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   715
        }
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   716
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   717
    }
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   718
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   719
    /**
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   720
     * A builder class used for adding, replacing, or removing elements.
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   721
     */
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   722
    private static class MapBuilder {
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   723
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   724
        /** Bins array - may be shared with map that created us. */
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   725
        private Element[] bins;
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   726
        /** Whether our bins are shared */
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   727
        private boolean hasOwnBins;
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   728
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   729
        /** Queue of unmerged elements */
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   730
        private Element qhead;
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   731
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   732
        /** Full property list. */
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   733
        private Element list;
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   734
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   735
        /** Number of properties. */
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   736
        private int size;
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   737
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   738
        MapBuilder(final Element[] bins, final Element list, final int size, final boolean hasOwnBins) {
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   739
            this.bins  = bins;
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   740
            this.hasOwnBins = hasOwnBins;
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   741
            this.list  = list;
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   742
            this.qhead = null;
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   743
            this.size  = size;
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   744
        }
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   745
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   746
        MapBuilder(final ElementQueue queue, final Element list, final int size, final boolean hasOwnBins) {
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   747
            this.bins  = queue.qbins;
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   748
            this.hasOwnBins = hasOwnBins;
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   749
            this.list  = list;
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   750
            this.qhead = queue.qhead;
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   751
            this.size  = size;
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   752
        }
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   753
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   754
        /**
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   755
         * Add a {@link Property}. Removes duplicates if necessary.
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   756
         *
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   757
         * @param property {@link Property} to add.
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   758
         */
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   759
        private void addProperty(final Property property) {
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   760
            final Object key = property.getKey();
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   761
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   762
            if (bins != null) {
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   763
                final int binIndex = binIndex(bins, key);
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   764
                if (findElement(bins[binIndex], key) != null) {
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   765
                    ensureOwnBins();
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   766
                    bins[binIndex] = removeExistingElement(bins[binIndex], key);
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   767
                } else if (findElement(qhead, key) != null) {
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   768
                    qhead = removeExistingElement(qhead, key);
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   769
                }
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   770
                if (hasOwnBins) {
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   771
                    bins[binIndex] = new Element(bins[binIndex], property);
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   772
                } else {
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   773
                    qhead = new Element(qhead, property);
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   774
                }
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   775
            } else {
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   776
                if (findElement(list, key) != null) {
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   777
                    list = removeFromList(list, key);
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   778
                    size--;
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   779
                }
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   780
            }
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   781
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   782
            list = new Element(list, property);
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   783
            size++;
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   784
        }
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   785
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   786
        /**
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   787
         * Replace an existing {@link Property} with a new one with the same key.
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   788
         *
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   789
         * @param key the property key
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   790
         * @param property the property to replace the old one with
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   791
         */
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   792
        private void replaceProperty(final Object key, final Property property) {
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   793
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   794
            if (bins != null) {
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   795
                final int binIndex = binIndex(bins, key);
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   796
                Element bin = bins[binIndex];
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   797
                if (findElement(bin, key) != null) {
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   798
                    ensureOwnBins();
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   799
                    bins[binIndex] = replaceInList(bin, key, property);
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   800
                } else if (qhead != null) {
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   801
                    qhead = replaceInList(qhead, key, property);
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   802
                }
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   803
            }
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   804
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   805
            list = replaceInList(list, key, property);
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   806
        }
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   807
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   808
        /**
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   809
         * Remove a {@link Property} based on its key.
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   810
         *
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   811
         * @param key Key of {@link Property} to remove.
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   812
         */
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   813
        void removeProperty(final Object key) {
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   814
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   815
            if (bins != null) {
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   816
                final int binIndex = binIndex(bins, key);
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   817
                final Element bin = bins[binIndex];
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   818
                if (findElement(bin, key) != null) { ;
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   819
                    if (size >= LIST_THRESHOLD) {
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   820
                        ensureOwnBins();
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   821
                        bins[binIndex] = removeFromList(bin, key);
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   822
                    } else {
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   823
                        // Go back to list-only representation for small maps
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   824
                        bins = null;
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   825
                        qhead = null;
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   826
                    }
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   827
                } else if (findElement(qhead, key) != null) {
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   828
                    qhead = removeFromList(qhead, key);
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   829
                }
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   830
            }
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   831
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   832
            list = removeFromList(list, key);
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   833
            size--;
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   834
        }
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   835
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   836
        /**
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   837
         * Removes an element known to exist from an element list and the main {@code list} and decreases {@code size}.
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   838
         *
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   839
         * @param element the element list
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   840
         * @param key the key to remove
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   841
         * @return the new element list
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   842
         */
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   843
        private Element removeExistingElement(Element element, Object key) {
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   844
            size--;
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   845
            list = removeFromList(list, key);
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   846
            return removeFromList(element, key);
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   847
        }
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   848
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   849
        /**
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   850
         * Make sure we own the bins we have, cloning them if necessary.
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   851
         */
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   852
        private void ensureOwnBins() {
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   853
            if (!hasOwnBins) {
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   854
                bins = bins.clone();
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   855
            }
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   856
            hasOwnBins = true;
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   857
        }
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   858
    }
fff3970bd14f 8068513: Adding elements to a javascript 'object' (a map) is slow
hannesw
parents: 47216
diff changeset
   859
16147
e63b63819133 8005403: Open-source Nashorn
jlaskey
parents:
diff changeset
   860
}