From 801b822972726c31637b235459f2b7a0aea93b29 Mon Sep 17 00:00:00 2001 From: Zhang Chao <80152403@qq.com> Date: Tue, 4 Jun 2024 13:03:09 +0800 Subject: [PATCH] Avoid unnecessary memory allocation in PackedLongValues#Iterator (#13439) We always allocate a long array of page size for a new PackedLongValues#Iterator instance, which is not necessary when packing a small number of values. this is more evident in the scenario of high-frequency flush operations --- lucene/CHANGES.txt | 3 ++- .../java/org/apache/lucene/util/packed/PackedLongValues.java | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt index 0544165a86f..b89aa865a81 100644 --- a/lucene/CHANGES.txt +++ b/lucene/CHANGES.txt @@ -249,7 +249,8 @@ Improvements Optimizations --------------------- -(No changes) + +* GITHUB#13439: Avoid unnecessary memory allocation in PackedLongValues#Iterator. (Zhang Chao) Bug Fixes --------------------- diff --git a/lucene/core/src/java/org/apache/lucene/util/packed/PackedLongValues.java b/lucene/core/src/java/org/apache/lucene/util/packed/PackedLongValues.java index cda57f0fb4c..e996c2d52e1 100644 --- a/lucene/core/src/java/org/apache/lucene/util/packed/PackedLongValues.java +++ b/lucene/core/src/java/org/apache/lucene/util/packed/PackedLongValues.java @@ -138,7 +138,7 @@ public class PackedLongValues extends LongValues implements Accountable { int currentCount; // number of entries of the current page Iterator() { - currentValues = new long[pageMask + 1]; + currentValues = new long[(int) Math.min(size, pageMask + 1)]; vOff = pOff = 0; fillBlock(); }