summaryrefslogtreecommitdiffstats
path: root/dx
diff options
context:
space:
mode:
authorPiotr Jastrzebski <haaawk@google.com>2015-03-11 14:43:01 +0000
committerPiotr Jastrzebski <haaawk@google.com>2015-03-16 11:12:21 +0000
commite83f6ae84fecf0f4c3d1ecf02c908566d001fcff (patch)
tree0d158e47c93d5916fc32ede2e6085bd1613d1757 /dx
parent76856e8ee8d3234b448499ecdb9e11d81e2efc4d (diff)
downloadtoolchain_jack-e83f6ae84fecf0f4c3d1ecf02c908566d001fcff.zip
toolchain_jack-e83f6ae84fecf0f4c3d1ecf02c908566d001fcff.tar.gz
toolchain_jack-e83f6ae84fecf0f4c3d1ecf02c908566d001fcff.tar.bz2
Stop creating CstString bytes eagerly.
Profiler showed that during a Music app compilation we create 1,364,332 instances of CstStrings. They require allocation of 395MB which is 6.4% of all memory allocated. It requires 5,457,328 allocations. Most of them is done in stringToUtf8Bytes. Fortunately only 157,495 instances actually uses the |bytes| field. This means that if we create |bytes| lazily then we can save a lot of memory and cpu (stringToUtf8Bytes is also a cpu hotspot). This change makes |bytes| in CtsString lazily computed. For Music app the memory allocation is reduced by 353MB which is 5.6% of all memory allocated. It also saves us 3,788,436 allocations. Change-Id: I8f2980be07da29d31fc639543dfb9b3966c8cd4e
Diffstat (limited to 'dx')
-rw-r--r--dx/src/com/android/jack/dx/rop/cst/CstString.java53
1 files changed, 36 insertions, 17 deletions
diff --git a/dx/src/com/android/jack/dx/rop/cst/CstString.java b/dx/src/com/android/jack/dx/rop/cst/CstString.java
index 94d547c..a23f7e0 100644
--- a/dx/src/com/android/jack/dx/rop/cst/CstString.java
+++ b/dx/src/com/android/jack/dx/rop/cst/CstString.java
@@ -33,8 +33,11 @@ public final class CstString extends TypedConstant {
/** {@code non-null;} the UTF-8 value as a string */
private final String string;
- /** {@code non-null;} the UTF-8 value as bytes */
- private final ByteArray bytes;
+ /** {@code null-ok;} the UTF-8 value as bytes */
+ private ByteArray bytes;
+
+ /** the number of bytes in UTF-8 value */
+ private int bytesCount = -1;
/**
* Converts a string into its MUTF-8 form. MUTF-8 differs from normal UTF-8
@@ -43,30 +46,40 @@ public final class CstString extends TypedConstant {
* @param string {@code non-null;} the string to convert
* @return {@code non-null;} the UTF-8 bytes for it
*/
- public static byte[] stringToUtf8Bytes(String string) {
+ private static byte[] stringToUtf8Bytes(String string, int utf8Len) {
int len = string.length();
- byte[] bytes = new byte[len * 3]; // Avoid having to reallocate.
+ byte[] bytes = new byte[utf8Len];
int outAt = 0;
for (int i = 0; i < len; i++) {
char c = string.charAt(i);
if ((c != 0) && (c < 0x80)) {
- bytes[outAt] = (byte) c;
- outAt++;
+ bytes[outAt++] = (byte) c;
} else if (c < 0x800) {
- bytes[outAt] = (byte) (((c >> 6) & 0x1f) | 0xc0);
- bytes[outAt + 1] = (byte) ((c & 0x3f) | 0x80);
- outAt += 2;
+ bytes[outAt++] = (byte) (((c >> 6) & 0x1f) | 0xc0);
+ bytes[outAt++] = (byte) ((c & 0x3f) | 0x80);
} else {
- bytes[outAt] = (byte) (((c >> 12) & 0x0f) | 0xe0);
- bytes[outAt + 1] = (byte) (((c >> 6) & 0x3f) | 0x80);
- bytes[outAt + 2] = (byte) ((c & 0x3f) | 0x80);
- outAt += 3;
+ bytes[outAt++] = (byte) (((c >> 12) & 0x0f) | 0xe0);
+ bytes[outAt++] = (byte) (((c >> 6) & 0x3f) | 0x80);
+ bytes[outAt++] = (byte) ((c & 0x3f) | 0x80);
}
}
- byte[] result = new byte[outAt];
- System.arraycopy(bytes, 0, result, 0, outAt);
+ return bytes;
+ }
+
+ private static int stringToUtf8BytesCount(String string) {
+ int result = 0;
+ for (int i = 0, len = string.length(); i < len; ++i) {
+ char c = string.charAt(i);
+ if ((c != 0) && (c < 0x80)) {
+ result++;
+ } else if (c < 0x800) {
+ result += 2;
+ } else {
+ result += 3;
+ }
+ }
return result;
}
@@ -189,7 +202,6 @@ public final class CstString extends TypedConstant {
}
this.string = string.intern();
- this.bytes = new ByteArray(stringToUtf8Bytes(string));
}
/**
@@ -203,6 +215,7 @@ public final class CstString extends TypedConstant {
}
this.bytes = bytes;
+ this.bytesCount = bytes.size();
this.string = utf8BytesToString(bytes).intern();
}
@@ -355,6 +368,9 @@ public final class CstString extends TypedConstant {
* @return {@code non-null;} an array of the UTF-8 bytes
*/
public ByteArray getBytes() {
+ if (bytes == null) {
+ bytes = new ByteArray(stringToUtf8Bytes(string, getUtf8Size()));
+ }
return bytes;
}
@@ -365,7 +381,10 @@ public final class CstString extends TypedConstant {
* @return {@code >= 0;} the UTF-8 size
*/
public int getUtf8Size() {
- return bytes.size();
+ if (bytesCount == -1) {
+ bytesCount = stringToUtf8BytesCount(string);
+ }
+ return bytesCount;
}
/**