Skip to content

Commit

Permalink
[GR-25050] Optimize run time representation of image heap maps.
Browse files Browse the repository at this point in the history
PullRequest: graal/8155
  • Loading branch information
cstancu committed Feb 13, 2021
2 parents f3ef06f + 8fa32c6 commit ea51548
Show file tree
Hide file tree
Showing 13 changed files with 404 additions and 211 deletions.
18 changes: 18 additions & 0 deletions sdk/src/org.graalvm.collections/snapshot.sigtest
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,24 @@ meth public void putAll(org.graalvm.collections.EconomicMap<{org.graalvm.collect
meth public void putAll(org.graalvm.collections.UnmodifiableEconomicMap<? extends {org.graalvm.collections.EconomicMap%0},? extends {org.graalvm.collections.EconomicMap%1}>)
meth public {org.graalvm.collections.EconomicMap%1} putIfAbsent({org.graalvm.collections.EconomicMap%0},{org.graalvm.collections.EconomicMap%1})

CLSS public org.graalvm.collections.EconomicMapWrap<%0 extends java.lang.Object, %1 extends java.lang.Object>
cons public init(java.util.Map<{org.graalvm.collections.EconomicMapWrap%0},{org.graalvm.collections.EconomicMapWrap%1}>)
intf org.graalvm.collections.EconomicMap<{org.graalvm.collections.EconomicMapWrap%0},{org.graalvm.collections.EconomicMapWrap%1}>
meth public boolean containsKey({org.graalvm.collections.EconomicMapWrap%0})
meth public boolean isEmpty()
meth public int size()
meth public java.lang.Iterable<{org.graalvm.collections.EconomicMapWrap%0}> getKeys()
meth public java.lang.Iterable<{org.graalvm.collections.EconomicMapWrap%1}> getValues()
meth public org.graalvm.collections.MapCursor<{org.graalvm.collections.EconomicMapWrap%0},{org.graalvm.collections.EconomicMapWrap%1}> getEntries()
meth public void clear()
meth public void replaceAll(java.util.function.BiFunction<? super {org.graalvm.collections.EconomicMapWrap%0},? super {org.graalvm.collections.EconomicMapWrap%1},? extends {org.graalvm.collections.EconomicMapWrap%1}>)
meth public {org.graalvm.collections.EconomicMapWrap%1} get({org.graalvm.collections.EconomicMapWrap%0})
meth public {org.graalvm.collections.EconomicMapWrap%1} put({org.graalvm.collections.EconomicMapWrap%0},{org.graalvm.collections.EconomicMapWrap%1})
meth public {org.graalvm.collections.EconomicMapWrap%1} putIfAbsent({org.graalvm.collections.EconomicMapWrap%0},{org.graalvm.collections.EconomicMapWrap%1})
meth public {org.graalvm.collections.EconomicMapWrap%1} removeKey({org.graalvm.collections.EconomicMapWrap%0})
supr java.lang.Object
hfds map

CLSS public abstract interface org.graalvm.collections.EconomicSet<%0 extends java.lang.Object>
intf org.graalvm.collections.UnmodifiableEconomicSet<{org.graalvm.collections.EconomicSet%0}>
meth public abstract boolean add({org.graalvm.collections.EconomicSet%0})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The Universal Permissive License (UPL), Version 1.0
Expand Down Expand Up @@ -40,7 +40,6 @@
*/
package org.graalvm.collections;

import java.util.Iterator;
import java.util.Map;
import java.util.function.BiFunction;

Expand Down Expand Up @@ -229,101 +228,6 @@ static <K, V> EconomicMap<K, V> create(Equivalence strategy, int initialCapacity
* @since 19.0
*/
static <K, V> EconomicMap<K, V> wrapMap(Map<K, V> map) {
return new EconomicMap<K, V>() {

@Override
public V get(K key) {
V result = map.get(key);
return result;
}

@Override
public V put(K key, V value) {
V result = map.put(key, value);
return result;
}

@Override
public V putIfAbsent(K key, V value) {
V result = map.putIfAbsent(key, value);
return result;
}

@Override
public int size() {
int result = map.size();
return result;
}

@Override
public boolean containsKey(K key) {
return map.containsKey(key);
}

@Override
public void clear() {
map.clear();
}

@Override
public V removeKey(K key) {
V result = map.remove(key);
return result;
}

@Override
public Iterable<V> getValues() {
return map.values();
}

@Override
public Iterable<K> getKeys() {
return map.keySet();
}

@Override
public boolean isEmpty() {
return map.isEmpty();
}

@Override
public MapCursor<K, V> getEntries() {
Iterator<java.util.Map.Entry<K, V>> iterator = map.entrySet().iterator();
return new MapCursor<K, V>() {

private Map.Entry<K, V> current;

@Override
public boolean advance() {
boolean result = iterator.hasNext();
if (result) {
current = iterator.next();
}

return result;
}

@Override
public K getKey() {
return current.getKey();
}

@Override
public V getValue() {
return current.getValue();
}

@Override
public void remove() {
iterator.remove();
}
};
}

@Override
public void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) {
map.replaceAll(function);
}
};
return new EconomicMapWrap<>(map);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
/*
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The Universal Permissive License (UPL), Version 1.0
*
* Subject to the condition set forth below, permission is hereby granted to any
* person obtaining a copy of this software, associated documentation and/or
* data (collectively the "Software"), free of charge and under any and all
* copyright rights in the Software, and any and all patent rights owned or
* freely licensable by each licensor hereunder covering either (i) the
* unmodified Software as contributed to or provided by such licensor, or (ii)
* the Larger Works (as defined below), to deal in both
*
* (a) the Software, and
*
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
* one is included with the Software each a "Larger Work" to which the Software
* is contributed by such licensors),
*
* without restriction, including without limitation the rights to copy, create
* derivative works of, display, perform, and distribute the Software and make,
* use, sell, offer for sale, import, export, have made, and have sold the
* Software and the Larger Work(s), and to sublicense the foregoing rights on
* either these or other terms.
*
* This license is subject to the following condition:
*
* The above copyright notice and either this complete permission notice or at a
* minimum a reference to the UPL must be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.graalvm.collections;

import java.util.Iterator;
import java.util.Map;
import java.util.function.BiFunction;

/**
* Wraps an existing {@link Map} as an {@link EconomicMap}.
*
* @since 21.1
*/
public class EconomicMapWrap<K, V> implements EconomicMap<K, V> {

private final Map<K, V> map;

/** @since 21.1 */
public EconomicMapWrap(Map<K, V> map) {
this.map = map;
}

/** @since 21.1 */
@Override
public V get(K key) {
V result = map.get(key);
return result;
}

/** @since 21.1 */
@Override
public V put(K key, V value) {
V result = map.put(key, value);
return result;
}

/** @since 21.1 */
@Override
public V putIfAbsent(K key, V value) {
V result = map.putIfAbsent(key, value);
return result;
}

/** @since 21.1 */
@Override
public int size() {
int result = map.size();
return result;
}

/** @since 21.1 */
@Override
public boolean containsKey(K key) {
return map.containsKey(key);
}

/** @since 21.1 */
@Override
public void clear() {
map.clear();
}

/** @since 21.1 */
@Override
public V removeKey(K key) {
V result = map.remove(key);
return result;
}

/** @since 21.1 */
@Override
public Iterable<V> getValues() {
return map.values();
}

/** @since 21.1 */
@Override
public Iterable<K> getKeys() {
return map.keySet();
}

/** @since 21.1 */
@Override
public boolean isEmpty() {
return map.isEmpty();
}

/** @since 21.1 */
@Override
public MapCursor<K, V> getEntries() {
Iterator<Map.Entry<K, V>> iterator = map.entrySet().iterator();
return new MapCursor<K, V>() {

private Map.Entry<K, V> current;

@Override
public boolean advance() {
boolean result = iterator.hasNext();
if (result) {
current = iterator.next();
}

return result;
}

@Override
public K getKey() {
return current.getKey();
}

@Override
public V getValue() {
return current.getValue();
}

@Override
public void remove() {
iterator.remove();
}
};
}

/** @since 21.1 */
@Override
public void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) {
map.replaceAll(function);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,31 @@
*/
package com.oracle.svm.core.hub;

import java.util.HashMap;
import java.util.Map;

import org.graalvm.collections.EconomicMap;
import org.graalvm.nativeimage.ImageSingletons;
import org.graalvm.nativeimage.Platform;
import org.graalvm.nativeimage.Platforms;
import org.graalvm.nativeimage.hosted.Feature;

import com.oracle.svm.core.annotate.AutomaticFeature;
import com.oracle.svm.core.util.ImageHeapMap;

public final class ClassForNameSupport {

private final Map<String, Class<?>> knownClasses = new HashMap<>();
static ClassForNameSupport singleton() {
return ImageSingletons.lookup(ClassForNameSupport.class);
}

/** The map used to collect registered classes. */
private final EconomicMap<String, Class<?>> knownClasses = ImageHeapMap.create();

@Platforms(Platform.HOSTED_ONLY.class)
public static void registerClass(Class<?> clazz) {
ImageSingletons.lookup(ClassForNameSupport.class).knownClasses.put(clazz.getName(), clazz);
singleton().knownClasses.put(clazz.getName(), clazz);
}

public static Class<?> forNameOrNull(String className, boolean initialize) {
Class<?> result = ImageSingletons.lookup(ClassForNameSupport.class).knownClasses.get(className);
Class<?> result = singleton().knownClasses.get(className);
if (result == null) {
return null;
}
Expand All @@ -64,7 +68,7 @@ public static Class<?> forName(String className, boolean initialize) throws Clas
}

@AutomaticFeature
class ClassForNameFeature implements Feature {
final class ClassForNameFeature implements Feature {
@Override
public void afterRegistration(AfterRegistrationAccess access) {
ImageSingletons.add(ClassForNameSupport.class, new ClassForNameSupport());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,9 @@ public void connect() throws IOException {
return;
}
connected = true;
Resources.ResourcesSupport support = ImageSingletons.lookup(Resources.ResourcesSupport.class);
// remove "resource:" from url to get the resource name
String resName = url.toString().substring(1 + JavaNetSubstitutions.RESOURCE_PROTOCOL.length());
final List<byte[]> bytes = support.resources.get(resName);
final List<byte[]> bytes = Resources.get(resName);
if (bytes == null || bytes.size() < 1) {
throw new FileNotFoundException(url.toString());
}
Expand Down
Loading

0 comments on commit ea51548

Please sign in to comment.