Skip to content

Commit

Permalink
Showing 31 changed files with 213 additions and 164 deletions.
12 changes: 6 additions & 6 deletions truffle/src/main/java/org/jruby/truffle/core/EncodingNodes.java
Original file line number Diff line number Diff line change
@@ -30,9 +30,9 @@
import org.jruby.truffle.runtime.RubyContext;
import org.jruby.truffle.language.control.RaiseException;
import org.jruby.truffle.runtime.layouts.Layouts;
import org.jruby.truffle.runtime.rope.CodeRange;
import org.jruby.truffle.runtime.rope.Rope;
import org.jruby.util.ByteList;
import org.jruby.util.StringSupport;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
@@ -271,18 +271,18 @@ public static Encoding compatibleEncodingForStrings(DynamicObject first, Dynamic
if (!firstEncoding.isAsciiCompatible() || !secondEncoding.isAsciiCompatible()) return null;

if (firstRope.getCodeRange() != secondRope.getCodeRange()) {
if (firstRope.getCodeRange() == StringSupport.CR_7BIT) return secondEncoding;
if (secondRope.getCodeRange() == StringSupport.CR_7BIT) return firstEncoding;
if (firstRope.getCodeRange() == CodeRange.CR_7BIT) return secondEncoding;
if (secondRope.getCodeRange() == CodeRange.CR_7BIT) return firstEncoding;
}
if (secondRope.getCodeRange() == StringSupport.CR_7BIT) return firstEncoding;
if (firstRope.getCodeRange() == StringSupport.CR_7BIT) return secondEncoding;
if (secondRope.getCodeRange() == CodeRange.CR_7BIT) return firstEncoding;
if (firstRope.getCodeRange() == CodeRange.CR_7BIT) return secondEncoding;

return null;
}

@TruffleBoundary
private static boolean isAsciiOnly(Rope rope) {
return rope.getEncoding().isAsciiCompatible() && rope.getCodeRange() == StringSupport.CR_7BIT;
return rope.getEncoding().isAsciiCompatible() && rope.getCodeRange() == CodeRange.CR_7BIT;
}

protected Encoding extractEncoding(DynamicObject string) {
Original file line number Diff line number Diff line change
@@ -12,7 +12,7 @@

import com.oracle.truffle.api.object.DynamicObject;
import org.jruby.truffle.runtime.layouts.Layouts;
import org.jruby.util.StringSupport;
import org.jruby.truffle.runtime.rope.CodeRange;

public class RegexpGuards {

@@ -25,7 +25,7 @@ public static boolean isRegexpLiteral(DynamicObject regexp) {
}

public static boolean isValidEncoding(DynamicObject string) {
return StringOperations.codeRange(string) != StringSupport.CR_BROKEN;
return StringOperations.codeRange(string) != CodeRange.CR_BROKEN;
}

}
5 changes: 2 additions & 3 deletions truffle/src/main/java/org/jruby/truffle/core/RegexpNodes.java
Original file line number Diff line number Diff line change
@@ -39,15 +39,14 @@
import org.jruby.truffle.runtime.RubyCallStack;
import org.jruby.truffle.runtime.RubyContext;
import org.jruby.truffle.runtime.layouts.Layouts;
import org.jruby.truffle.runtime.rope.CodeRange;
import org.jruby.truffle.runtime.rope.Rope;
import org.jruby.truffle.runtime.rope.RopeOperations;
import org.jruby.util.*;

import java.nio.charset.StandardCharsets;
import java.util.Iterator;

import static org.jruby.util.StringSupport.CR_7BIT;

@CoreClass(name = "Regexp")
public abstract class RegexpNodes {

@@ -487,7 +486,7 @@ public QuoteNode(RubyContext context, SourceSection sourceSection) {
@Specialization(guards = "isRubyString(raw)")
public DynamicObject quoteString(DynamicObject raw) {
final Rope rope = StringOperations.rope(raw);
boolean isAsciiOnly = rope.getEncoding().isAsciiCompatible() && rope.getCodeRange() == CR_7BIT;
boolean isAsciiOnly = rope.getEncoding().isAsciiCompatible() && rope.getCodeRange() == CodeRange.CR_7BIT;
return createString(org.jruby.RubyRegexp.quote19(StringOperations.getByteListReadOnly(raw), isAsciiOnly));
}

79 changes: 42 additions & 37 deletions truffle/src/main/java/org/jruby/truffle/core/RopeNodes.java
Original file line number Diff line number Diff line change
@@ -30,6 +30,7 @@
import org.jruby.truffle.nodes.RubyNode;
import org.jruby.truffle.runtime.RubyContext;
import org.jruby.truffle.runtime.rope.AsciiOnlyLeafRope;
import org.jruby.truffle.runtime.rope.CodeRange;
import org.jruby.truffle.runtime.rope.ConcatRope;
import org.jruby.truffle.runtime.rope.InvalidLeafRope;
import org.jruby.truffle.runtime.rope.LeafRope;
@@ -39,6 +40,10 @@
import org.jruby.truffle.runtime.rope.ValidLeafRope;
import org.jruby.util.StringSupport;

import static org.jruby.truffle.runtime.rope.CodeRange.CR_7BIT;
import static org.jruby.truffle.runtime.rope.CodeRange.CR_BROKEN;
import static org.jruby.truffle.runtime.rope.CodeRange.CR_VALID;

public abstract class RopeNodes {

@NodeChildren({
@@ -130,8 +135,8 @@ public Rope substringConcatRope(ConcatRope base, int offset, int byteLength,
}

private Rope makeSubstring(Rope base, int offset, int byteLength, ConditionProfile is7BitProfile) {
if (is7BitProfile.profile(base.getCodeRange() == StringSupport.CR_7BIT)) {
return new SubstringRope(base, offset, byteLength, byteLength, StringSupport.CR_7BIT);
if (is7BitProfile.profile(base.getCodeRange() == CR_7BIT)) {
return new SubstringRope(base, offset, byteLength, byteLength, CR_7BIT);
}

return makeSubstringNon7Bit(base, offset, byteLength);
@@ -140,7 +145,7 @@ private Rope makeSubstring(Rope base, int offset, int byteLength, ConditionProfi
@TruffleBoundary
private Rope makeSubstringNon7Bit(Rope base, int offset, int byteLength) {
final long packedLengthAndCodeRange = RopeOperations.calculateCodeRangeAndLength(base.getEncoding(), base.getBytes(), offset, offset + byteLength);
final int codeRange = StringSupport.unpackArg(packedLengthAndCodeRange);
final CodeRange codeRange = CodeRange.fromInt(StringSupport.unpackArg(packedLengthAndCodeRange));
final int characterLength = StringSupport.unpackResult(packedLengthAndCodeRange);

/*
@@ -208,7 +213,7 @@ public Rope concatLeaves(LeafRope left, LeafRope right, Encoding encoding,
System.arraycopy(left.getBytes(), 0, bytes, 0, left.byteLength());
System.arraycopy(right.getBytes(), 0, bytes, left.byteLength(), right.byteLength());

final int codeRange = commonCodeRange(left.getCodeRange(), right.getCodeRange(), sameCodeRangeProfile, brokenCodeRangeProfile);
final CodeRange codeRange = commonCodeRange(left.getCodeRange(), right.getCodeRange(), sameCodeRangeProfile, brokenCodeRangeProfile);

return makeLeafRopeNode.executeMake(bytes, encoding, codeRange);
}
@@ -263,19 +268,19 @@ public Rope concat(Rope left, Rope right, Encoding encoding,
depth);
}

private int commonCodeRange(int first, int second,
private CodeRange commonCodeRange(CodeRange first, CodeRange second,
ConditionProfile sameCodeRangeProfile,
ConditionProfile brokenCodeRangeProfile) {
if (sameCodeRangeProfile.profile(first == second)) {
return first;
}

if (brokenCodeRangeProfile.profile((first == StringSupport.CR_BROKEN) || (second == StringSupport.CR_BROKEN))) {
return StringSupport.CR_BROKEN;
if (brokenCodeRangeProfile.profile((first == CR_BROKEN) || (second == CR_BROKEN))) {
return CR_BROKEN;
}

// If we get this far, one must be CR_7BIT and the other must be CR_VALID, so promote to the more general code range.
return StringSupport.CR_VALID;
return CR_VALID;
}

private boolean isSingleByteOptimizable(Rope left, Rope right, ConditionProfile isLeftSingleByteOptimizableProfile) {
@@ -315,22 +320,22 @@ public MakeLeafRopeNode(RubyContext context, SourceSection sourceSection) {
super(context, sourceSection);
}

public abstract LeafRope executeMake(byte[] bytes, Encoding encoding, int codeRange);
public abstract LeafRope executeMake(byte[] bytes, Encoding encoding, CodeRange codeRange);

@Specialization(guards = "is7Bit(codeRange)")
public LeafRope makeAsciiOnlyLeafRope(byte[] bytes, Encoding encoding, int codeRange) {
public LeafRope makeAsciiOnlyLeafRope(byte[] bytes, Encoding encoding, CodeRange codeRange) {
return new AsciiOnlyLeafRope(bytes, encoding);
}

@Specialization(guards = { "isValid(codeRange)", "isFixedWidth(encoding)" })
public LeafRope makeValidLeafRopeFixedWidthEncoding(byte[] bytes, Encoding encoding, int codeRange) {
public LeafRope makeValidLeafRopeFixedWidthEncoding(byte[] bytes, Encoding encoding, CodeRange codeRange) {
final int characterLength = bytes.length / encoding.minLength();

return new ValidLeafRope(bytes, encoding, characterLength);
}

@Specialization(guards = { "isValid(codeRange)", "!isFixedWidth(encoding)" })
public LeafRope makeValidLeafRope(byte[] bytes, Encoding encoding, int codeRange) {
public LeafRope makeValidLeafRope(byte[] bytes, Encoding encoding, CodeRange codeRange) {
// Exctracted from StringSupport.strLength.

int characterLength = 0;
@@ -355,12 +360,12 @@ public LeafRope makeValidLeafRope(byte[] bytes, Encoding encoding, int codeRange
}

@Specialization(guards = "isBroken(codeRange)")
public LeafRope makeInvalidLeafRope(byte[] bytes, Encoding encoding, int codeRange) {
public LeafRope makeInvalidLeafRope(byte[] bytes, Encoding encoding, CodeRange codeRange) {
return new InvalidLeafRope(bytes, encoding);
}

@Specialization(guards = { "isUnknown(codeRange)", "isEmpty(bytes)" })
public LeafRope makeUnknownLeafRopeEmpty(byte[] bytes, Encoding encoding, int codeRange,
public LeafRope makeUnknownLeafRopeEmpty(byte[] bytes, Encoding encoding, CodeRange codeRange,
@Cached("createBinaryProfile()") ConditionProfile isUTF8,
@Cached("createBinaryProfile()") ConditionProfile isUSAscii,
@Cached("createBinaryProfile()") ConditionProfile isAscii8Bit,
@@ -385,75 +390,75 @@ public LeafRope makeUnknownLeafRopeEmpty(byte[] bytes, Encoding encoding, int co
}

@Specialization(guards = { "isUnknown(codeRange)", "!isEmpty(bytes)", "isBinaryString(encoding)" })
public LeafRope makeUnknownLeafRopeBinary(byte[] bytes, Encoding encoding, int codeRange,
public LeafRope makeUnknownLeafRopeBinary(byte[] bytes, Encoding encoding, CodeRange codeRange,
@Cached("createBinaryProfile()") ConditionProfile discovered7BitProfile) {
int newCodeRange = StringSupport.CR_7BIT;
CodeRange newCodeRange = CR_7BIT;
for (int i = 0; i < bytes.length; i++) {
if (bytes[i] < 0) {
newCodeRange = StringSupport.CR_VALID;
newCodeRange = CR_VALID;
break;
}
}

if (discovered7BitProfile.profile(newCodeRange == StringSupport.CR_7BIT)) {
if (discovered7BitProfile.profile(newCodeRange == CR_7BIT)) {
return new AsciiOnlyLeafRope(bytes, encoding);
}

return new ValidLeafRope(bytes, encoding, bytes.length);
}

@Specialization(guards = { "isUnknown(codeRange)", "!isEmpty(bytes)", "!isBinaryString(encoding)", "isAsciiCompatible(encoding)" })
public LeafRope makeUnknownLeafRopeAsciiCompatible(byte[] bytes, Encoding encoding, int codeRange,
public LeafRope makeUnknownLeafRopeAsciiCompatible(byte[] bytes, Encoding encoding, CodeRange codeRange,
@Cached("createBinaryProfile()") ConditionProfile discovered7BitProfile,
@Cached("createBinaryProfile()") ConditionProfile discoveredValidProfile) {
final long packedLengthAndCodeRange = StringSupport.strLengthWithCodeRangeAsciiCompatible(encoding, bytes, 0, bytes.length);
final int newCodeRange = StringSupport.unpackArg(packedLengthAndCodeRange);
final CodeRange newCodeRange = CodeRange.fromInt(StringSupport.unpackArg(packedLengthAndCodeRange));
final int characterLength = StringSupport.unpackResult(packedLengthAndCodeRange);

if (discovered7BitProfile.profile(newCodeRange == StringSupport.CR_7BIT)) {
if (discovered7BitProfile.profile(newCodeRange == CR_7BIT)) {
return new AsciiOnlyLeafRope(bytes, encoding);
}

if (discoveredValidProfile.profile(newCodeRange == StringSupport.CR_VALID)) {
if (discoveredValidProfile.profile(newCodeRange == CR_VALID)) {
return new ValidLeafRope(bytes, encoding, characterLength);
}

return new InvalidLeafRope(bytes, encoding);
}

@Specialization(guards = { "isUnknown(codeRange)", "!isEmpty(bytes)", "!isBinaryString(encoding)", "!isAsciiCompatible(encoding)" })
public LeafRope makeUnknownLeafRope(byte[] bytes, Encoding encoding, int codeRange,
public LeafRope makeUnknownLeafRope(byte[] bytes, Encoding encoding, CodeRange codeRange,
@Cached("createBinaryProfile()") ConditionProfile discovered7BitProfile,
@Cached("createBinaryProfile()") ConditionProfile discoveredValidProfile) {
final long packedLengthAndCodeRange = StringSupport.strLengthWithCodeRangeNonAsciiCompatible(encoding, bytes, 0, bytes.length);
final int newCodeRange = StringSupport.unpackArg(packedLengthAndCodeRange);
final CodeRange newCodeRange = CodeRange.fromInt(StringSupport.unpackArg(packedLengthAndCodeRange));
final int characterLength = StringSupport.unpackResult(packedLengthAndCodeRange);

if (discovered7BitProfile.profile(newCodeRange == StringSupport.CR_7BIT)) {
if (discovered7BitProfile.profile(newCodeRange == CR_7BIT)) {
return new AsciiOnlyLeafRope(bytes, encoding);
}

if (discoveredValidProfile.profile(newCodeRange == StringSupport.CR_VALID)) {
if (discoveredValidProfile.profile(newCodeRange == CR_VALID)) {
return new ValidLeafRope(bytes, encoding, characterLength);
}

return new InvalidLeafRope(bytes, encoding);
}

protected static boolean is7Bit(int codeRange) {
return codeRange == StringSupport.CR_7BIT;
protected static boolean is7Bit(CodeRange codeRange) {
return codeRange == CR_7BIT;
}

protected static boolean isValid(int codeRange) {
return codeRange == StringSupport.CR_VALID;
protected static boolean isValid(CodeRange codeRange) {
return codeRange == CR_VALID;
}

protected static boolean isBroken(int codeRange) {
return codeRange == StringSupport.CR_BROKEN;
protected static boolean isBroken(CodeRange codeRange) {
return codeRange == CR_BROKEN;
}

protected static boolean isUnknown(int codeRange) {
return codeRange == StringSupport.CR_UNKNOWN;
protected static boolean isUnknown(CodeRange codeRange) {
return codeRange == CodeRange.CR_UNKNOWN;
}

protected static boolean isBinaryString(Encoding encoding) {
@@ -501,7 +506,7 @@ public DynamicObject debugPrintLeafRope(LeafRope rope, int currentLevel, boolean
bytesAreNull,
rope.byteLength(),
rope.characterLength(),
StringSupport.codeRangeAsString(rope.getCodeRange()),
rope.getCodeRange(),
rope.depth()));

return nil();
@@ -521,7 +526,7 @@ public DynamicObject debugPrintSubstringRope(SubstringRope rope, int currentLeve
bytesAreNull,
rope.byteLength(),
rope.characterLength(),
StringSupport.codeRangeAsString(rope.getCodeRange()),
rope.getCodeRange(),
rope.getOffset(),
rope.depth()));

@@ -544,7 +549,7 @@ public DynamicObject debugPrintConcatRope(ConcatRope rope, int currentLevel, boo
bytesAreNull,
rope.byteLength(),
rope.characterLength(),
StringSupport.codeRangeAsString(rope.getCodeRange()),
rope.getCodeRange(),
rope.depth(),
rope.getLeft().depth(),
rope.getRight().depth()));
6 changes: 3 additions & 3 deletions truffle/src/main/java/org/jruby/truffle/core/RopeTable.java
Original file line number Diff line number Diff line change
@@ -12,9 +12,9 @@

import com.oracle.truffle.api.CompilerDirectives;
import org.jcodings.Encoding;
import org.jruby.truffle.runtime.rope.CodeRange;
import org.jruby.truffle.runtime.rope.Rope;
import org.jruby.truffle.runtime.rope.RopeOperations;
import org.jruby.util.StringSupport;

import java.lang.ref.WeakReference;
import java.util.Arrays;
@@ -28,7 +28,7 @@ public class RopeTable {
private final WeakHashMap<Key, WeakReference<Rope>> ropesTable = new WeakHashMap<>();

@CompilerDirectives.TruffleBoundary
public Rope getRope(byte[] bytes, Encoding encoding, int codeRange) {
public Rope getRope(byte[] bytes, Encoding encoding, CodeRange codeRange) {
final Key key = new Key(bytes, encoding);

lock.readLock().lock();
@@ -100,7 +100,7 @@ public boolean equals(Object o) {

@Override
public String toString() {
return RopeOperations.create(bytes, encoding, StringSupport.CR_UNKNOWN).toString();
return RopeOperations.create(bytes, encoding, CodeRange.CR_UNKNOWN).toString();
}

}
Original file line number Diff line number Diff line change
@@ -26,12 +26,12 @@ public StringCodeRangeableWrapper(DynamicObject string) {

@Override
public int getCodeRange() {
return StringOperations.getCodeRange(string);
return StringOperations.getCodeRange(string).toInt();
}

@Override
public int scanForCodeRange() {
return StringOperations.codeRange(string);
return StringOperations.codeRange(string).toInt();
}

@Override
Original file line number Diff line number Diff line change
@@ -14,8 +14,8 @@
import org.jcodings.specific.UTF8Encoding;
import org.jruby.truffle.nodes.RubyGuards;
import org.jruby.truffle.runtime.layouts.Layouts;
import org.jruby.truffle.runtime.rope.CodeRange;
import org.jruby.truffle.runtime.rope.Rope;
import org.jruby.util.StringSupport;

public class StringGuards {

@@ -26,7 +26,7 @@ public static boolean isSingleByteOptimizable(DynamicObject string) {

public static boolean is7Bit(DynamicObject string) {
assert RubyGuards.isRubyString(string);
return StringOperations.getCodeRange(string) == StringSupport.CR_7BIT;
return StringOperations.getCodeRange(string) == CodeRange.CR_7BIT;
}

public static boolean isAsciiCompatible(DynamicObject string) {
@@ -49,7 +49,7 @@ public static boolean isValidOr7BitEncoding(DynamicObject string) {
assert RubyGuards.isRubyString(string);
final Rope rope = StringOperations.rope(string);

return (rope.getCodeRange() == StringSupport.CR_VALID) || (rope.getCodeRange() == StringSupport.CR_7BIT);
return (rope.getCodeRange() == CodeRange.CR_VALID) || (rope.getCodeRange() == CodeRange.CR_7BIT);
}

public static boolean isFixedWidthEncoding(DynamicObject string) {
@@ -69,6 +69,6 @@ public static boolean isEmpty(DynamicObject string) {

public static boolean isBrokenCodeRange(DynamicObject string) {
assert RubyGuards.isRubyString(string);
return StringOperations.codeRange(string) == StringSupport.CR_BROKEN;
return StringOperations.codeRange(string) == CodeRange.CR_BROKEN;
}
}
Loading

0 comments on commit cb827e9

Please sign in to comment.