Skip to content

Commit 92d8e87

Browse files
committed
[GR-36892] Ensure cext initialisation objects are in separate handle blocks.
PullRequest: truffleruby/3168
2 parents 90ddb06 + c30950f commit 92d8e87

File tree

6 files changed

+29
-1
lines changed

6 files changed

+29
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ Performance:
3434
* Switch to `Arrays.mismatch()` in string comparison for better performance (@aardvark179).
3535
* Removed extra array allocations for method calls in the interpreter to improve warmup performance (@aardvark179).
3636
* Optimize `Dir[]` by sorting entries as they are found and grouping syscalls (#2092, @aardvark179).
37+
* Reduce memory footprint by tracking `VALUE`s created during C extension init separately (@aardvark179).
3738

3839
Changes:
3940

lib/cext/ABI_version.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
10
1+
11

src/main/c/cext/ruby.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ void rb_tr_init(void *ruby_cext) {
4141
rb_tr_longwrap = polyglot_invoke(rb_tr_cext, "rb_tr_wrap_function");
4242
rb_tr_force_native = polyglot_invoke(rb_tr_cext, "rb_tr_force_native_function");
4343

44+
polyglot_invoke(rb_tr_cext, "cext_start_new_handle_block");
4445
rb_tr_init_exception();
4546
rb_tr_init_global_constants();
4647
rb_argv0 = rb_gv_get("$0");
48+
polyglot_invoke(rb_tr_cext, "cext_start_new_handle_block");
4749
}

src/main/java/org/truffleruby/cext/CExtNodes.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,15 @@ protected Object publicSendWithoutLock(
252252
}
253253
}
254254

255+
@CoreMethod(names = "cext_start_new_handle_block", onSingleton = true)
256+
public abstract static class StartNewHandleBlockNode extends CoreMethodArrayArgumentsNode {
257+
@Specialization
258+
protected boolean isCExtLockOwned() {
259+
ValueWrapperManager.allocateNewBlock(getContext(), getLanguage());
260+
return true;
261+
}
262+
}
263+
255264
@CoreMethod(names = "cext_lock_owned?", onSingleton = true)
256265
public abstract static class IsCExtLockOwnedNode extends CoreMethodArrayArgumentsNode {
257266
@Specialization

src/main/java/org/truffleruby/cext/ValueWrapperManager.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,19 @@ public static AllocateHandleNode create() {
346346
}
347347
}
348348

349+
public static HandleBlock allocateNewBlock(RubyContext context, RubyLanguage language) {
350+
HandleBlockHolder holder = getBlockHolder(context, language);
351+
HandleBlock block = holder.handleBlock;
352+
353+
if (block != null) {
354+
context.getMarkingService().queueForMarking(block);
355+
}
356+
block = (holder.handleBlock = new HandleBlock(context, language.handleBlockAllocator));
357+
context.getValueWrapperManager().addToBlockMap(block, context, language);
358+
359+
return block;
360+
}
361+
349362
public static boolean isTaggedLong(long handle) {
350363
return (handle & LONG_TAG) == LONG_TAG;
351364
}

src/main/java/org/truffleruby/language/loader/RequireNode.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.oracle.truffle.api.source.Source;
2424
import org.graalvm.collections.Pair;
2525
import org.truffleruby.RubyLanguage;
26+
import org.truffleruby.cext.ValueWrapperManager;
2627
import org.truffleruby.core.array.ArrayUtils;
2728
import org.truffleruby.core.cast.BooleanCastNode;
2829
import org.truffleruby.core.rope.Rope;
@@ -282,13 +283,15 @@ private void requireCExtension(String feature, String expandedPath, Node current
282283

283284
requireMetric("before-execute-" + feature);
284285
try {
286+
ValueWrapperManager.allocateNewBlock(getContext(), getLanguage());
285287
InteropNodes
286288
.execute(
287289
initFunction,
288290
ArrayUtils.EMPTY_ARRAY,
289291
initFunctionInteropLibrary,
290292
TranslateInteropExceptionNode.getUncached());
291293
} finally {
294+
ValueWrapperManager.allocateNewBlock(getContext(), getLanguage());
292295
requireMetric("after-execute-" + feature);
293296
}
294297
}

0 commit comments

Comments
 (0)