Skip to content

[GR-64192] Native Image Builder Memory Footprint Optimizations #11077

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Copyright (c) 2025, 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.test;

import java.util.NoSuchElementException;

import org.graalvm.collections.EconomicSet;
import org.junit.Assert;
import org.junit.Test;

public class EmptyEconomicSetTest {

@Test
public void testIsEmpty() {
Assert.assertTrue(EconomicSet.emptySet().isEmpty());
}

@Test
public void testSizeZero() {
Assert.assertEquals(0, EconomicSet.emptySet().size());
}

@Test(expected = IllegalArgumentException.class)
public void testAdd() {
EconomicSet.emptySet().add(1);
}

@Test(expected = IllegalArgumentException.class)
public void testRemove() {
EconomicSet.emptySet().remove(1);
}

@Test(expected = IllegalArgumentException.class)
public void testClear() {
EconomicSet.emptySet().clear();
}

@Test
public void testContains() {
Assert.assertFalse(EconomicSet.emptySet().contains(1));
}

@Test
public void testIteratorAlwaysEmpty() {
Assert.assertFalse(EconomicSet.emptySet().iterator().hasNext());
}

@Test(expected = NoSuchElementException.class)
public void testIteratorThrowsException() {
EconomicSet.emptySet().iterator().next();
}
}
1 change: 1 addition & 0 deletions sdk/src/org.graalvm.collections/snapshot.sigtest
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ meth public static <%0 extends java.lang.Object> org.graalvm.collections.Economi
meth public static <%0 extends java.lang.Object> org.graalvm.collections.EconomicSet<{%%0}> create(org.graalvm.collections.Equivalence,int)
meth public static <%0 extends java.lang.Object> org.graalvm.collections.EconomicSet<{%%0}> create(org.graalvm.collections.Equivalence,org.graalvm.collections.UnmodifiableEconomicSet<{%%0}>)
meth public static <%0 extends java.lang.Object> org.graalvm.collections.EconomicSet<{%%0}> create(org.graalvm.collections.UnmodifiableEconomicSet<{%%0}>)
meth public static org.graalvm.collections.EconomicSet emptySet()
meth public void addAll(java.lang.Iterable<{org.graalvm.collections.EconomicSet%0}>)
meth public void addAll(java.util.Iterator<{org.graalvm.collections.EconomicSet%0}>)
meth public void addAll(org.graalvm.collections.EconomicSet<{org.graalvm.collections.EconomicSet%0}>)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 2025, 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 @@ -206,4 +206,14 @@ static <E> EconomicSet<E> create(Equivalence strategy, int initialCapacity) {
static <E> EconomicSet<E> create(Equivalence strategy, UnmodifiableEconomicSet<E> c) {
return EconomicMapImpl.create(strategy, c, true);
}

/**
* Return an empty, unmodifiable {@link EconomicSet}.
*
* @since 25.0
*/
@SuppressWarnings("unchecked")
static <E> EconomicSet<E> emptySet() {
return (EconomicSet<E>) EmptySet.EMPTY_SET;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright (c) 2025, 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;

/**
* Singleton instance for empty set. Reuses empty iterator from {@link EmptyMap}.
*/
class EmptySet {
static final EconomicSet<Object> EMPTY_SET = new EconomicSet<>() {
@Override
public boolean add(Object element) {
throw new IllegalArgumentException("Cannot modify the always-empty set");
}

@Override
public void remove(Object element) {
throw new IllegalArgumentException("Cannot modify the always-empty set");
}

@Override
public void clear() {
throw new IllegalArgumentException("Cannot modify the always-empty set");
}

@Override
public boolean contains(Object element) {
return false;
}

@Override
public int size() {
return 0;
}

@Override
public boolean isEmpty() {
return true;
}

@Override
public Iterator<Object> iterator() {
return EmptyMap.EMPTY_ITERATOR;
}
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import com.oracle.graal.pointsto.api.HostVM;
import com.oracle.graal.pointsto.api.PointstoOptions;
import com.oracle.graal.pointsto.constraints.UnsupportedFeatures;
import com.oracle.graal.pointsto.flow.AlwaysEnabledPredicateFlow;
import com.oracle.graal.pointsto.flow.AnyPrimitiveSourceTypeFlow;
import com.oracle.graal.pointsto.flow.FieldTypeFlow;
import com.oracle.graal.pointsto.flow.FormalParamTypeFlow;
Expand Down Expand Up @@ -103,9 +104,9 @@ public abstract class PointsToAnalysis extends AbstractAnalysisEngine {
*/
private final boolean trackPrimitiveValues;
private final AnalysisType longType;
private final AnalysisType voidType;
private final boolean usePredicates;
private AnyPrimitiveSourceTypeFlow anyPrimitiveSourceTypeFlow;
private AlwaysEnabledPredicateFlow alwaysEnabledPredicateFlow;

protected final boolean trackTypeFlowInputs;
protected final boolean reportAnalysisStatistics;
Expand All @@ -127,12 +128,13 @@ public PointsToAnalysis(OptionValues options, AnalysisUniverse universe, HostVM

this.objectType = metaAccess.lookupJavaType(Object.class);
this.longType = metaAccess.lookupJavaType(long.class);
this.voidType = metaAccess.lookupJavaType(void.class);

this.trackPrimitiveValues = PointstoOptions.TrackPrimitiveValues.getValue(options);
this.usePredicates = PointstoOptions.UsePredicates.getValue(options);
this.anyPrimitiveSourceTypeFlow = new AnyPrimitiveSourceTypeFlow(null, longType);
this.anyPrimitiveSourceTypeFlow.enableFlow(null);
this.alwaysEnabledPredicateFlow = new AlwaysEnabledPredicateFlow();

/*
* Make sure the all-instantiated type flow is created early. We do not have any
* instantiated types yet, so the state is empty at first.
Expand Down Expand Up @@ -292,6 +294,7 @@ public boolean trackConcreteAnalysisObjects(@SuppressWarnings("unused") Analysis
public void cleanupAfterAnalysis() {
super.cleanupAfterAnalysis();
anyPrimitiveSourceTypeFlow = null;
alwaysEnabledPredicateFlow = null;
unsafeLoads = null;
unsafeStores = null;

Expand All @@ -310,10 +313,6 @@ public AnalysisType getLongType() {
return longType;
}

public AnalysisType getVoidType() {
return voidType;
}

public AnalysisType getObjectArrayType() {
return metaAccess.lookupJavaType(Object[].class);
}
Expand All @@ -331,6 +330,10 @@ public AnyPrimitiveSourceTypeFlow getAnyPrimitiveSourceTypeFlow() {
return anyPrimitiveSourceTypeFlow;
}

public AlwaysEnabledPredicateFlow getAlwaysEnabledPredicateFlow() {
return alwaysEnabledPredicateFlow;
}

@Override
public Iterable<AnalysisType> getAllSynchronizedTypes() {
return getAllInstantiatedTypes();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ public MethodTypeFlowBuilder(PointsToAnalysis bb, PointsToAnalysisMethod method,
this.graphKind = graphKind;
if (bb.trackPrimitiveValues()) {
this.alwaysEnabled = bb.usePredicates()
? TypeFlowBuilder.create(bb, method, null, PointsToAnalysis.syntheticSourcePosition(method), AlwaysEnabledPredicateFlow.class, AlwaysEnabledPredicateFlow::new)
? TypeFlowBuilder.create(bb, method, null, PointsToAnalysis.syntheticSourcePosition(method), AlwaysEnabledPredicateFlow.class, bb::getAlwaysEnabledPredicateFlow)
: null;
this.anyPrimitiveSourceTypeFlowBuilder = TypeFlowBuilder.create(bb, method, alwaysEnabled, null, AnyPrimitiveSourceTypeFlow.class, bb::getAnyPrimitiveSourceTypeFlow);
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -296,14 +296,14 @@ public void addPredicated(PointsToAnalysis bb, TypeFlow<?> predicatedFlow) {
}
}

private void removePredicated(TypeFlow<?> predicatedFlow) {
ConcurrentLightHashSet.removeElement(this, PREDICATED_FLOWS_UPDATER, predicatedFlow);
}

public Collection<TypeFlow<?>> getPredicatedFlows() {
return ConcurrentLightHashSet.getElements(this, PREDICATED_FLOWS_UPDATER);
}

public void clearPredicatedFlows() {
ConcurrentLightHashSet.clear(this, PREDICATED_FLOWS_UPDATER);
}

public boolean predicateAlreadyTriggered() {
return AtomicUtils.isSet(this, PREDICATE_TRIGGERED_UPDATER);
}
Expand Down Expand Up @@ -945,31 +945,34 @@ protected void onSaturated() {
private void notifySaturated(PointsToAnalysis bb) {
for (TypeFlow<?> use : getUses()) {
notifyUseOfSaturation(bb, use);
removeUse(use);
}
clearUses();
for (TypeFlow<?> observer : getObservers()) {
notifyObserverOfSaturation(bb, observer);
removeObserver(observer);
}
clearObservers();
}

/** This flow will swap itself out at all uses and observers. */
protected void swapOut(PointsToAnalysis bb, TypeFlow<?> newFlow) {
assert isSaturated() : "This operation should only be called on saturated flows:" + this;
for (TypeFlow<?> use : getUses()) {
swapAtUse(bb, newFlow, use);
newFlow.addUse(bb, use);
}
clearUses();
for (TypeFlow<?> observer : getObservers()) {
swapAtObserver(bb, newFlow, observer);
observer.replacedObservedWith(bb, newFlow);
}
clearObservers();
/*
* Before performing the swap, make sure addPredicated will immediately enable any newly
* added predicated flows.
*/
AtomicUtils.atomicMark(this, PREDICATE_TRIGGERED_UPDATER);
for (TypeFlow<?> predicatedFlow : getPredicatedFlows()) {
swapAtPredicated(bb, newFlow, predicatedFlow);
newFlow.addPredicated(bb, predicatedFlow);
}
clearPredicatedFlows();
}

protected void swapAtUse(PointsToAnalysis bb, TypeFlow<?> newFlow, TypeFlow<?> use) {
Expand All @@ -983,11 +986,6 @@ protected void swapAtObserver(PointsToAnalysis bb, TypeFlow<?> newFlow, TypeFlow
observer.replacedObservedWith(bb, newFlow);
}

private void swapAtPredicated(PointsToAnalysis bb, TypeFlow<?> newFlow, TypeFlow<?> predicatedFlow) {
removePredicated(predicatedFlow);
newFlow.addPredicated(bb, predicatedFlow);
}

/**
* Notifies this flow that its input has saturated, but only runs the
* {@link TypeFlow#onInputSaturated}} if this flow is enabled. Otherwise, the execution of
Expand Down
Loading
Loading