-
Notifications
You must be signed in to change notification settings - Fork 323
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
Enhance Managed_Resource
to allow implementation of in-memory caches
#11577
Open
JaroslavTulach
wants to merge
27
commits into
develop
Choose a base branch
from
wip/jtulach/ReferenceManager11485
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+480
−152
Open
Changes from 6 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
5266a4b
Using Ref.new allow_gc=True to implement in-memory caches
JaroslavTulach d803b28
Hide operations with References behind @TruffleBoundary
JaroslavTulach 38a20b4
Work with v and not this.value
JaroslavTulach 0f437fc
Merging with latest develop
JaroslavTulach 404e3cc
Fixing typo
JaroslavTulach 5629e20
Using Ref.new to cache reference to EnsoHTTPResponseCache
JaroslavTulach b1d4b37
Removing (unused) support for Name.Special
JaroslavTulach 0e42e9d
Fixing moved import
JaroslavTulach 0b4e1f8
Providing access to bodyNode in the builtin methods
JaroslavTulach f5f1614
Enabling allow_gc in the caches
JaroslavTulach dad2bb6
Note in changelog
JaroslavTulach 85e8b6d
Can no longer invoke Managed_Resource.with when Managed_Resource.fina…
JaroslavTulach e56b867
Backing out the Ref changes
JaroslavTulach 3ca29aa
Merge tag '2024.5.1-nightly.2024.11.27' into wip/jtulach/ReferenceMan…
JaroslavTulach da2d438
Ref.new takes only one argument (again)
JaroslavTulach ae83008
Commenting out releaseAll call for now
JaroslavTulach 276a885
Allow system controlled Managed_Resource
JaroslavTulach 205811c
Merging with most recent develop
JaroslavTulach 40aedfc
on_missing behavior for managed resources that get access after being…
JaroslavTulach 2e91135
Updating micro-distribution with the new Managed_Resource builtins
JaroslavTulach 786f156
Moving the GC related parts of RefTest to ManagedResourceTest
JaroslavTulach 934ec5f
Better note in changelog
JaroslavTulach a3c07e0
Making public so the Fetch_Spec passes OK
JaroslavTulach a7890d2
Using Managed_Resource inside of EnsoSecretHelper
JaroslavTulach a1f7e6f
Radek demands use of False
JaroslavTulach 873bac3
Return DataflowError on with of already finalized Managed_Resource
JaroslavTulach 068d223
Rename to scheduleFinalizationOfSystemReferences
JaroslavTulach File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
engine/runtime-integration-tests/src/test/java/org/enso/interpreter/runtime/RefTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
package org.enso.interpreter.runtime; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.assertNull; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
import java.lang.ref.Reference; | ||
import java.lang.ref.WeakReference; | ||
import org.enso.common.MethodNames; | ||
import org.enso.test.utils.ContextUtils; | ||
import org.graalvm.polyglot.Context; | ||
import org.graalvm.polyglot.Source; | ||
import org.graalvm.polyglot.Value; | ||
import org.junit.AfterClass; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
|
||
public class RefTest { | ||
private static Context ctx; | ||
private static EnsoContext ensoCtx; | ||
private static Value newRef; | ||
private static Value createRef; | ||
private static Value getRef; | ||
|
||
@BeforeClass | ||
public static void initCtx() throws Exception { | ||
ctx = ContextUtils.createDefaultContext(); | ||
ensoCtx = ContextUtils.leakContext(ctx); | ||
var code = | ||
""" | ||
import Standard.Base.Runtime.Ref.Ref | ||
|
||
new_ref obj = | ||
Ref.new obj | ||
|
||
create_ref obj allow_gc = | ||
Ref.new obj allow_gc | ||
|
||
get_ref ref = ref.get | ||
JaroslavTulach marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"""; | ||
var src = Source.newBuilder("enso", code, "gc.enso").build(); | ||
var gcEnso = ctx.eval(src); | ||
newRef = gcEnso.invokeMember(MethodNames.Module.EVAL_EXPRESSION, "new_ref"); | ||
createRef = gcEnso.invokeMember(MethodNames.Module.EVAL_EXPRESSION, "create_ref"); | ||
getRef = gcEnso.invokeMember(MethodNames.Module.EVAL_EXPRESSION, "get_ref"); | ||
} | ||
|
||
@AfterClass | ||
public static void closeCtx() throws Exception { | ||
ctx.close(); | ||
ctx = null; | ||
} | ||
|
||
@Test | ||
public void regularReference() throws Exception { | ||
var obj = new Object(); | ||
var ref = newRef.execute(obj); | ||
|
||
assertFalse("Value returned", ref.isNull()); | ||
assertEquals("Standard.Base.Runtime.Ref.Ref", ref.getMetaObject().getMetaQualifiedName()); | ||
|
||
var weakRef = new WeakReference<>(obj); | ||
obj = null; | ||
|
||
assertEquals("We get the object", weakRef.get(), getRef.execute(ref).asHostObject()); | ||
|
||
assertGC("Weak wasn't released", false, weakRef); | ||
assertFalse("Value was not GCed", getRef.execute(ref).isNull()); | ||
assertEquals("We get the object", weakRef.get(), getRef.execute(ref).asHostObject()); | ||
|
||
ensoCtx.getReferencesManager().releaseAll(); | ||
assertEquals( | ||
"releaseAll has no effect on regular reference", | ||
weakRef.get(), | ||
getRef.execute(ref).asHostObject()); | ||
} | ||
|
||
@Test | ||
public void garbageCollectableReference() throws Exception { | ||
var obj = new Object(); | ||
var ref = createRef.execute(obj, true); | ||
|
||
assertFalse("Value returned", ref.isNull()); | ||
assertEquals("Standard.Base.Runtime.Ref.Ref", ref.getMetaObject().getMetaQualifiedName()); | ||
assertEquals("We get the object", obj, getRef.execute(ref).asHostObject()); | ||
|
||
var weakRef = new WeakReference<>(obj); | ||
obj = null; | ||
assertGC("Weak reference can be GCed", true, weakRef); | ||
|
||
assertTrue("Value was GCed", getRef.execute(ref).isNull()); | ||
} | ||
|
||
@Test | ||
public void explicitlyReclaimableReference() throws Exception { | ||
var obj = new Object(); | ||
var ref = createRef.execute(obj, true); | ||
|
||
assertFalse("Value returned", ref.isNull()); | ||
assertEquals("Standard.Base.Runtime.Ref.Ref", ref.getMetaObject().getMetaQualifiedName()); | ||
assertEquals("We get the object", obj, getRef.execute(ref).asHostObject()); | ||
|
||
ensoCtx.getReferencesManager().releaseAll(); | ||
|
||
assertTrue("Value was GCed", getRef.execute(ref).isNull()); | ||
} | ||
|
||
private static void assertGC(String msg, boolean expectGC, Reference<?> ref) { | ||
JaroslavTulach marked this conversation as resolved.
Show resolved
Hide resolved
|
||
for (var i = 1; i < Integer.MAX_VALUE / 2; i *= 2) { | ||
if (ref.get() == null) { | ||
break; | ||
} | ||
System.gc(); | ||
} | ||
var obj = ref.get(); | ||
if (expectGC) { | ||
assertNull(msg + " ref still alive", obj); | ||
} else { | ||
assertNotNull(msg + " ref has been cleaned", obj); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
engine/runtime/src/main/java/org/enso/interpreter/runtime/ReferencesManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package org.enso.interpreter.runtime; | ||
|
||
import com.oracle.truffle.api.CompilerDirectives; | ||
import java.lang.ref.Reference; | ||
import java.lang.ref.ReferenceQueue; | ||
import java.lang.ref.SoftReference; | ||
import java.lang.ref.WeakReference; | ||
import java.util.Collection; | ||
import java.util.concurrent.ConcurrentLinkedQueue; | ||
|
||
/** Tracks soft and weak references and allow their cleanup. */ | ||
public final class ReferencesManager { | ||
private final EnsoContext ctx; | ||
private final Collection<Reference> refs = new ConcurrentLinkedQueue<>(); | ||
private final ReferenceQueue<Object> queue = new ReferenceQueue<>(); | ||
|
||
ReferencesManager(EnsoContext ctx) { | ||
this.ctx = ctx; | ||
} | ||
|
||
/** | ||
* Creates new reference to provided object and registers it in the manager. | ||
* | ||
* @param <T> class of the object to reference | ||
* @param obj the object to reference | ||
* @param type ({@code 1} use {@link SoftReference} or {@code 2} to use {@link WeakReference} | ||
* @return newly created reference to the provided object | ||
*/ | ||
@CompilerDirectives.TruffleBoundary | ||
public <T> Reference<T> create(T obj, int type) { | ||
JaroslavTulach marked this conversation as resolved.
Show resolved
Hide resolved
|
||
clearPendingReferences(); | ||
var r = | ||
switch (type) { | ||
case 1 -> new SoftReference<>(obj, queue); | ||
case 2 -> new WeakReference<>(obj, queue); | ||
default -> throw new IllegalStateException(); | ||
}; | ||
refs.add(r); | ||
return r; | ||
} | ||
|
||
/** Releases all the references. E.g. cleans all the cached values. */ | ||
@CompilerDirectives.TruffleBoundary | ||
public void releaseAll() { | ||
var arr = refs.toArray(Reference[]::new); | ||
for (var r : arr) { | ||
r.clear(); | ||
refs.remove(r); | ||
} | ||
} | ||
|
||
@CompilerDirectives.TruffleBoundary | ||
private void clearPendingReferences() { | ||
for (; ; ) { | ||
var r = queue.poll(); | ||
if (r == null) { | ||
break; | ||
} | ||
refs.remove(r); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we replace this with an atom type
reference_type
?e.g.
Then if we ever want to also add support for soft references, it is just the matter of adding yet another variant to
Reference_Type
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right now I am thinking about:
the
..Explicit
cleaning policy would solve @GregoryTravis'sWe would connect cleanup of
..Explicit
directly to refresh button click and no GC.Or we could map all above types to estimates of memory and recomputation time:
Never
- zero memory, maximal timeExplicit
- zero memory, zero timeSoon
- some memory, zero timeManaged
- some memory and some time