Skip to content

Commit

Permalink
[Truffle] Short cut the encoding compatibility check when doing Strin…
Browse files Browse the repository at this point in the history
…g#+ and specialise for the common case of UTF8 (literal) + USASCII (runtime) in either order.
chrisseaton committed Apr 14, 2016

Verified

This commit was signed with the committer’s verified signature.
makenowjust Hiroya Fujinami
1 parent b12ab7c commit 9320450
Showing 1 changed file with 34 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -42,6 +42,7 @@
import com.oracle.truffle.api.source.SourceSection;
import org.jcodings.Encoding;
import org.jcodings.specific.ASCIIEncoding;
import org.jcodings.specific.USASCIIEncoding;
import org.jcodings.specific.UTF8Encoding;
import org.jruby.truffle.RubyContext;
import org.jruby.truffle.core.CoreClass;
@@ -143,14 +144,27 @@ public AddNode(RubyContext context, SourceSection sourceSection) {
return ToStrNodeGen.create(getContext(), getSourceSection(), other);
}

@Specialization(guards = "isRubyString(other)")
public DynamicObject add(DynamicObject string, DynamicObject other) {
final Rope left = rope(string);
final Rope right = rope(other);
@Specialization(guards = {"isRubyString(other)", "getEncoding(string) == getEncoding(other)"})
public DynamicObject addSameEncoding(DynamicObject string, DynamicObject other) {
return add(string, other, getEncoding(string));
}

@Specialization(guards = {"isRubyString(other)", "getEncoding(string) != getEncoding(other)", "isUTF8AndUSASCII(string, other)"})
public DynamicObject addUTF8AndUSASCII(DynamicObject string, DynamicObject other) {
return add(string, other, UTF8Encoding.INSTANCE);
}

@Specialization(guards = {"isRubyString(other)", "getEncoding(string) != getEncoding(other)", "!isUTF8AndUSASCII(string, other)"})
public DynamicObject addDifferentEncodings(DynamicObject string, DynamicObject other) {
final Encoding enc = StringOperations.checkEncoding(getContext(), string, other, this);
return add(string, other, enc);
}

private DynamicObject add(DynamicObject string, DynamicObject other, Encoding encoding) {
final Rope left = rope(string);
final Rope right = rope(other);

final Rope concatRope = makeConcatNode.executeMake(left, right, enc);
final Rope concatRope = makeConcatNode.executeMake(left, right, encoding);

final DynamicObject ret = Layouts.STRING.createString(coreLibrary().getStringFactory(), concatRope);

@@ -160,6 +174,21 @@ public DynamicObject add(DynamicObject string, DynamicObject other) {
return ret;
}

protected Encoding getEncoding(DynamicObject string) {
return Layouts.STRING.getRope(string).getEncoding();
}

protected boolean isUTF8AndUSASCII(DynamicObject string, DynamicObject other) {
final Encoding stringEncoding = getEncoding(string);
final Encoding otherEncoding = getEncoding(other);

if (stringEncoding != UTF8Encoding.INSTANCE && otherEncoding != UTF8Encoding.INSTANCE) {
return false;
}

return stringEncoding == USASCIIEncoding.INSTANCE && otherEncoding == USASCIIEncoding.INSTANCE;
}

}

@CoreMethod(names = "*", required = 1, lowerFixnumParameters = 0, taintFromSelf = true)

0 comments on commit 9320450

Please sign in to comment.