Permalink
Browse files

Improve the performance of the non-raw 'Walker' classes.

Document the Verifier.isHighSurrogate.
Convert the anonymous classes to named classes in Format (the EscapeStrategy classes).
(the named classes are easier to track in code-coverage and profiling)
  • Loading branch information...
1 parent 6c0c745 commit acac98ce6330def6ee3ed9591ac0d820fc9f46ee @rolfl rolfl committed Mar 21, 2012
@@ -745,6 +745,11 @@ public static boolean isHexDigit(final char c) {
public static boolean isHighSurrogate(final char ch) {
// faster way to do it is with bit manipulation....
// return (ch >= 0xD800 && ch <= 0xDBFF);
+ // A high surrogate has the bit pattern:
+ // 110110xx xxxxxxxx
+ // ch & 0xFC00 does a bit-mask of the most significant 6 bits (110110)
+ // return 0xD800 == (ch & 0xFC00);
+ // as it happens, it is faster to do a bit-shift,
return 0x36 == ch >>> 10;
}
@@ -78,34 +78,53 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
public class Format implements Cloneable {
/**
- * An EscapeStrategy suitable for UTF-8 an UTF-16
+ * An EscapeStrategy suitable for UTF-8 an UTF-16. We want the class to
+ * have its own name.
*/
- private static final EscapeStrategy UTFEscapeStrategy = new EscapeStrategy() {
+ private static final class EscapeStrategyUTF implements EscapeStrategy {
@Override
- public boolean shouldEscape(char ch) {
+ public final boolean shouldEscape(char ch) {
return Verifier.isHighSurrogate(ch);
}
- };
+ }
/**
- * An EscapeStrategy suitable for 8-bit charsets
+ * An EscapeStrategy suitable for UTF-8 an UTF-16
*/
- private static final EscapeStrategy Bits8EscapeStrategy = new EscapeStrategy() {
+ private static final EscapeStrategy UTFEscapeStrategy = new EscapeStrategyUTF();
+
+ /**
+ * An EscapeStrategy suitable for 8-bit charsets. We want the class to have
+ * its own name.
+ */
+ private static final class EscapeStrategy8Bits implements EscapeStrategy {
@Override
- public boolean shouldEscape(char ch) {
+ public boolean shouldEscape(final char ch) {
return (ch >>> 8) != 0;
}
- };
+ }
/**
- * An EscapeStrategy suitable for 7-bit charsets
+ * An EscapeStrategy suitable for 8-bit charsets
*/
- private static final EscapeStrategy Bits7EscapeStrategy = new EscapeStrategy() {
+ private static final EscapeStrategy Bits8EscapeStrategy = new EscapeStrategy8Bits();
+
+ /**
+ * An EscapeStrategy suitable for 7-bit charsets. We want the class to
+ * have its own name.
+ */
+ private static final class EscapeStrategy7Bits implements EscapeStrategy {
@Override
- public boolean shouldEscape(char ch) {
+ public boolean shouldEscape(final char ch) {
return (ch >>> 7) != 0;
}
- };
+ }
+
+ /**
+ * An EscapeStrategy suitable for 7-bit charsets
+ */
+ private static final EscapeStrategy Bits7EscapeStrategy =
+ new EscapeStrategy7Bits();
/**
* An EscapeStrategy suitable for 'unknown' charsets
@@ -91,6 +91,21 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
*/
public abstract class AbstractFormattedWalker implements Walker {
+ /*
+ * We use Text instances to return formatted text to the caller.
+ * We do not need to validate the Text content... it is 'safe' to
+ * not use the default Text class.
+ */
+ private static final class FastText extends Text {
+
+ private static final long serialVersionUID = 200L;
+
+ private FastText(String text) {
+ super(CType.Text);
+ this.value = text;
+ }
+ }
+
/**
* Indicate how text content should be added
* @author Rolf Lear
@@ -175,7 +190,7 @@ private void closeText() {
return;
}
ensurespace();
- data[msize++] = new Text(buffer.toString());
+ data[msize++] = new FastText(buffer.toString());
buffer.setLength(0);
}
@@ -122,7 +122,7 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
private final boolean specifiedAttributesOnly;
/** entity escape logic */
- private EscapeStrategy escapeStrategy;
+ private final EscapeStrategy escapeStrategy;
/*
* ====================================================================

0 comments on commit acac98c

Please sign in to comment.