From 179ed2b7d826bfc8826c17d9088283e6156a6346 Mon Sep 17 00:00:00 2001 From: Jeff Sharkey Date: Mon, 4 Jan 2021 21:42:19 -0700 Subject: [PATCH] Fix FastDataOutput performance bug. The underlying CharsetUtils methods accept the total length of the underlying byte[] array; the bug in the existing logic was attempting to pass the remaining length, which caused much more aggressive one-off buffer allocation when not needed. Bug: 171832118 Test: atest FrameworksCoreTests:com.android.internal.util.FastDataTest Test: atest FrameworksCoreTests:android.util.CharsetUtilsTest Test: atest FrameworksCoreTests:android.util.XmlTest Test: atest FrameworksCoreTests:android.util.BinaryXmlTest Change-Id: If257f75446d2f99313eecc3c11822d770f920687 --- core/java/com/android/internal/util/FastDataOutput.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/core/java/com/android/internal/util/FastDataOutput.java b/core/java/com/android/internal/util/FastDataOutput.java index 83d26e1812288..cf5b2966d8891 100644 --- a/core/java/com/android/internal/util/FastDataOutput.java +++ b/core/java/com/android/internal/util/FastDataOutput.java @@ -115,8 +115,7 @@ public class FastDataOutput implements DataOutput, Flushable, Closeable { // Magnitude of this returned value indicates the number of bytes // required to encode the string; sign indicates success/failure - int len = CharsetUtils.toModifiedUtf8Bytes(s, mBufferPtr, mBufferPos + 2, - mBufferCap - mBufferPos - 2); + int len = CharsetUtils.toModifiedUtf8Bytes(s, mBufferPtr, mBufferPos + 2, mBufferCap); if (Math.abs(len) > MAX_UNSIGNED_SHORT) { throw new IOException("Modified UTF-8 length too large: " + len); }