Skip to content

Commit

Permalink
[Truffle] Added a new GetRubyEncodingNode to have a fast path for con…
Browse files Browse the repository at this point in the history
…verting jcodings encodings to Ruby encodings.
  • Loading branch information
nirvdrum committed Jan 3, 2017
1 parent 8d5b31d commit aca2d0e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
8 changes: 4 additions & 4 deletions test/truffle/compiler/pe/core/encoding_pe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
example "Encoding::ISO_2022_JP.dummy?", true
example "Encoding::UTF_8.dummy?", false

tagged example "Encoding.compatible?('abc', 'def')", Encoding::UTF_8
tagged example "Encoding.compatible?(Encoding::UTF_8, Encoding::US_ASCII)", Encoding::UTF_8
tagged example "Encoding.compatible?(Encoding::UTF_8, Encoding::ASCII_8BIT)", nil
tagged example "Encoding.compatible?('abc', Encoding::US_ASCII)", Encoding::UTF_8
example "Encoding.compatible?('abc', 'def')", Encoding::UTF_8
example "Encoding.compatible?(Encoding::UTF_8, Encoding::US_ASCII)", Encoding::UTF_8
example "Encoding.compatible?(Encoding::UTF_8, Encoding::ASCII_8BIT)", nil
example "Encoding.compatible?('abc', Encoding::US_ASCII)", Encoding::UTF_8
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,41 @@ protected static boolean isAsciiCompatible(DynamicObject encoding) {
}
}

@NodeChild("encoding")
public abstract static class GetRubyEncodingNode extends RubyNode {

public static GetRubyEncodingNode create() {
return EncodingNodesFactory.GetRubyEncodingNodeGen.create(null);
}

public abstract DynamicObject executeGetRubyEncoding(Encoding encoding);

@Specialization(guards = "isSameEncoding(encoding, cachedRubyEncoding)",
limit = "getCacheLimit()")
protected DynamicObject getRubyEncodingCached(Encoding encoding,
@Cached("getRubyEncodingUncached(encoding)") DynamicObject cachedRubyEncoding) {
return cachedRubyEncoding;
}

@Specialization(contains = "getRubyEncodingCached")
protected DynamicObject getRubyEncodingUncached(Encoding encoding) {
if (encoding == null) {
throw new UnsupportedOperationException("cannot convert null Java encoding to a Ruby encoding");

This comment has been minimized.

Copy link
@eregon

eregon Jan 3, 2017

Member

This should either have a BranchProfile or the method have a TruffleBoundary or become an assert, otherwise all the exception throwing code will compile (that's also the reason ArrayList.get() doesn't compile well with Truffle, it would need a profile for out-of-bounds).

}

return getContext().getEncodingManager().getRubyEncoding(encoding);
}

protected boolean isSameEncoding(Encoding encoding, DynamicObject rubyEncoding) {
return encoding == Layouts.ENCODING.getEncoding(rubyEncoding);
}

This comment has been minimized.

Copy link
@eregon

eregon Jan 3, 2017

Member

This is slightly less optimal than caching on the Encoding instance, but it likely doesn't matter much.


protected int getCacheLimit() {
return getContext().getOptions().ENCODING_LOADED_CLASSES_CACHE;
}

}

@NodeChildren({ @NodeChild("first"), @NodeChild("second") })
public static abstract class NegotiateCompatibleEncodingNode extends RubyNode {

Expand Down Expand Up @@ -300,10 +335,12 @@ protected int getCacheLimit() {
@CoreMethod(names = "compatible?", onSingleton = true, required = 2)
public abstract static class CompatibleQueryNode extends CoreMethodArrayArgumentsNode {

@Child private GetRubyEncodingNode getRubyEncodingNode;
@Child private NegotiateCompatibleEncodingNode negotiateCompatibleEncodingNode;

public CompatibleQueryNode(SourceIndexLength sourceSection) {
super(sourceSection);
getRubyEncodingNode = EncodingNodesFactory.GetRubyEncodingNodeGen.create();
negotiateCompatibleEncodingNode = NegotiateCompatibleEncodingNode.create();
}

Expand All @@ -318,7 +355,7 @@ protected DynamicObject isCompatible(Object first, Object second,
return nil();
}

return getContext().getEncodingManager().getRubyEncoding(negotiatedEncoding);
return getRubyEncodingNode.executeGetRubyEncoding(negotiatedEncoding);
}

protected int getCacheLimit() {
Expand Down

0 comments on commit aca2d0e

Please sign in to comment.