From 827e19271a8808f1ebdc3f442899d3a70b93505e Mon Sep 17 00:00:00 2001 From: pbacsko Date: Sat, 11 Sep 2021 01:01:37 +0200 Subject: [PATCH] =?UTF-8?q?HADOOP-17901.=20Performance=20degradation=20in?= =?UTF-8?q?=20Text.append()=20after=20HADOOP-1=E2=80=A6=20(#3411)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/org/apache/hadoop/io/Text.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/Text.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/Text.java index 49151002ba..f39b1b721f 100644 --- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/Text.java +++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/Text.java @@ -268,8 +268,7 @@ public void set(byte[] utf8, int start, int len) { */ public void append(byte[] utf8, int start, int len) { byte[] original = bytes; - int capacity = Math.max(length + len, length + (length >> 1)); - if (ensureCapacity(capacity)) { + if (ensureCapacity(length + len)) { System.arraycopy(original, 0, bytes, 0, length); } System.arraycopy(utf8, start, bytes, length, len); @@ -302,7 +301,10 @@ public void clear() { */ private boolean ensureCapacity(final int capacity) { if (bytes.length < capacity) { - bytes = new byte[capacity]; + // Try to expand the backing array by the factor of 1.5x + // (by taking the current size + diving it by half) + int targetSize = Math.max(capacity, bytes.length + (bytes.length >> 1)); + bytes = new byte[targetSize]; return true; } return false;