Skip to content

Commit 8fc5d9d

Browse files
committed
Use cleaner for handle blocks on context disposal.
1 parent 0fa6d98 commit 8fc5d9d

File tree

10 files changed

+32
-26
lines changed

10 files changed

+32
-26
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Performance:
2222

2323
* Reimplement `Float#to_s` for better performance (#1584, @aardvark179).
2424
* Improve reference processing by making C object free functions and other finalizers more lightweight (@aardvark179).
25+
* Improve performance of `RSTRING_PTR` for interned strings (@aardvark179).
2526

2627
Changes:
2728

src/main/java/org/truffleruby/RubyLanguage.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,9 @@ public final class RubyLanguage extends TruffleLanguage<RubyContext> {
182182

183183
public static final TruffleLogger LOGGER = TruffleLogger.getLogger(TruffleRuby.LANGUAGE_ID);
184184

185-
/** This is a truly empty frame descriptor and should only by dummy root nodes which require no variables any other
186-
* root nodes should should use {#link
187-
* {@link TranslatorEnvironment#newFrameDescriptorBuilder(org.truffleruby.parser.ParentFrameDescriptor, boolean)}}. */
185+
/** This is a truly empty frame descriptor and should only by dummy root nodes which require no variables. Any other
186+
* root nodes should should use
187+
* {@link TranslatorEnvironment#newFrameDescriptorBuilder(org.truffleruby.parser.ParentFrameDescriptor, boolean)}. */
188188
public static final FrameDescriptor EMPTY_FRAME_DESCRIPTOR = new FrameDescriptor(Nil.INSTANCE);
189189

190190
/** We need an extra indirection added to ContextThreadLocal due to multiple Fibers of different Ruby Threads

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1212,11 +1212,11 @@ protected NativeRope toNative(RubyString string,
12121212
nativeRope = (NativeRope) currentRope;
12131213
} else {
12141214
nativeRope = new NativeRope(
1215+
getLanguage(),
12151216
bytesNode.execute(currentRope),
12161217
currentRope.getEncoding(),
12171218
characterLengthNode.execute(currentRope),
1218-
codeRangeNode.execute(currentRope),
1219-
getLanguage());
1219+
codeRangeNode.execute(currentRope));
12201220
string.setRope(nativeRope);
12211221
}
12221222

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public void freeAllBlocksInMap(RubyLanguage language) {
148148
}
149149
HandleBlock block = ref.get();
150150
if (block != null) {
151-
allocator.addFreeBlock(block.base);
151+
block.cleanable.clean();
152152
}
153153
}
154154
}

src/main/java/org/truffleruby/core/DataObjectFinalizationService.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
import com.oracle.truffle.api.profiles.ConditionProfile;
3232
import com.oracle.truffle.api.frame.VirtualFrame;
3333

34-
/** Finalizers are implemented with phantom references and reference queues, and are run in a dedicated Ruby thread. */
34+
/** C-ext data finalizers are implemented with phantom references and reference queues, and are run in a dedicated Ruby
35+
* thread. */
3536
public class DataObjectFinalizationService extends ReferenceProcessingService<DataObjectFinalizerReference> {
3637

3738
// We need a base node here, it should extend ruby base root node and implement internal root node.

src/main/java/org/truffleruby/core/rope/NativeRope.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ public class NativeRope extends Rope {
2828
private final Pointer pointer;
2929

3030
public NativeRope(
31+
RubyLanguage language,
3132
byte[] bytes,
3233
Encoding encoding,
3334
int characterLength,
34-
CodeRange codeRange,
35-
RubyLanguage language) {
36-
this(allocateNativePointer(bytes, language), bytes.length, encoding, characterLength, codeRange);
35+
CodeRange codeRange) {
36+
this(allocateNativePointer(language, bytes), bytes.length, encoding, characterLength, codeRange);
3737
}
3838

3939
private NativeRope(Pointer pointer, int byteLength, Encoding encoding, int characterLength, CodeRange codeRange) {
@@ -45,7 +45,7 @@ private NativeRope(Pointer pointer, int byteLength, Encoding encoding, int chara
4545
this.pointer = pointer;
4646
}
4747

48-
private static Pointer allocateNativePointer(byte[] bytes, RubyLanguage language) {
48+
private static Pointer allocateNativePointer(RubyLanguage language, byte[] bytes) {
4949
final Pointer pointer = Pointer.mallocAutoRelease(bytes.length + 1, language);
5050
pointer.writeBytes(0, bytes, 0, bytes.length);
5151
pointer.writeByte(bytes.length, (byte) 0);

src/main/java/org/truffleruby/core/string/ImmutableRubyString.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,20 @@ public boolean isNative() {
5858
return nativeRope != null;
5959
}
6060

61-
public synchronized NativeRope getNativeRope(RubyLanguage language) {
61+
public NativeRope getNativeRope(RubyLanguage language) {
6262
if (nativeRope == null) {
63-
nativeRope = createNativeRope(language);
63+
return createNativeRope(language);
6464
}
6565
return nativeRope;
6666
}
6767

6868
@TruffleBoundary
69-
private NativeRope createNativeRope(RubyLanguage language) {
70-
return new NativeRope(rope.getBytes(), rope.getEncoding(), rope.characterLength(), rope.getCodeRange(),
71-
language);
69+
private synchronized NativeRope createNativeRope(RubyLanguage language) {
70+
if (nativeRope == null) {
71+
nativeRope = new NativeRope(language, rope.getBytes(), rope.getEncoding(), rope.characterLength(),
72+
rope.getCodeRange());
73+
}
74+
return nativeRope;
7275
}
7376

7477
// region RubyStringLibrary messages

src/main/java/org/truffleruby/extra/ffi/Pointer.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ public static Pointer malloc(long size) {
3939
}
4040

4141
/** Includes {@link #enableAutorelease(RubyLanguage)} and avoids locking for it */
42-
public static Pointer mallocAutoRelease(long size, RubyLanguage langauge) {
43-
return new Pointer(UNSAFE.allocateMemory(size), size, langauge);
42+
public static Pointer mallocAutoRelease(long size, RubyLanguage language) {
43+
return new Pointer(UNSAFE.allocateMemory(size), size, language);
4444
}
4545

4646
/** Allocates memory and produces a pointer to it. Clears the memory before returning it. Use {@link #malloc} if you
@@ -52,8 +52,8 @@ public static Pointer calloc(long size) {
5252
}
5353

5454
/** Includes {@link #enableAutorelease(RubyLanguage)} and avoids locking for it */
55-
public static Pointer callocAutoRelease(long size, RubyLanguage langauge) {
56-
final Pointer pointer = mallocAutoRelease(size, langauge);
55+
public static Pointer callocAutoRelease(long size, RubyLanguage language) {
56+
final Pointer pointer = mallocAutoRelease(size, language);
5757
pointer.writeBytes(0, size, (byte) 0);
5858
return pointer;
5959
}
@@ -65,7 +65,7 @@ public static Pointer callocAutoRelease(long size, RubyLanguage langauge) {
6565
private AutoReleaseState autoReleaseState = null;
6666

6767
/** This is needed because we need a mutable object to hold the pointer address so that it can be freed or marked as
68-
* not to be freed if auto-release is disabled. THis can't be the address field on the pointer itself as that would
68+
* not to be freed if auto-release is disabled. This can't be the address field on the pointer itself as that would
6969
* prevent the pointer from being collected, and we can't retrieve this from the cleaner as it is effectively
7070
* opaque. */
7171
private static class AutoReleaseState implements Runnable {
@@ -96,10 +96,10 @@ public Pointer(long address, long size) {
9696
this.size = size;
9797
}
9898

99-
private Pointer(long address, long size, RubyLanguage langauge) {
99+
private Pointer(long address, long size, RubyLanguage language) {
100100
this.address = address;
101101
this.size = size;
102-
enableAutoreleaseUnsynchronized(langauge);
102+
enableAutoreleaseUnsynchronized(language);
103103
}
104104

105105
public boolean isNull() {

src/main/java/org/truffleruby/language/objects/shared/ShareInternalFieldsNode.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
import com.oracle.truffle.api.library.CachedLibrary;
3131
import com.oracle.truffle.api.profiles.ConditionProfile;
3232

33-
/** Share the internal fields of an object, accessible by its Layout */
33+
/** Share the plain Java fields which may contain objets for subclasses of RubyDynamicObject.
34+
* {@link RubyDynamicObject#metaClass} is handled by {@link ShareObjectNode}. */
3435
@ImportStatic({ ShapeCachingGuards.class, ArrayGuards.class })
3536
public abstract class ShareInternalFieldsNode extends RubyBaseNode {
3637

@@ -85,7 +86,7 @@ protected void shareCachedQueue(RubyQueue object,
8586

8687
@Specialization
8788
protected void shareCachedBasicObject(RubyBasicObject object) {
88-
/* No internal fields for RubyBasicObject */
89+
/* No extra Java fields for RubyBasicObject */
8990
}
9091

9192
@Specialization(

src/main/java/org/truffleruby/language/objects/shared/WriteBarrierNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ protected void writeBarrierFinalizer(FinalizerReference ref) {
9191

9292
@Specialization
9393
@TruffleBoundary
94-
protected void writeBarrierFinalizer(DataObjectFinalizerReference ref) {
94+
protected void writeBarrierDataFinalizer(DataObjectFinalizerReference ref) {
9595
SharedObjects.writeBarrier(getLanguage(), ref.callable);
9696
SharedObjects.writeBarrier(getLanguage(), ref.dataHolder);
9797
}

0 commit comments

Comments
 (0)