From e4dbc27d73e6be753a071f379afa1e57d59e0ead Mon Sep 17 00:00:00 2001 From: "Maxim.Mossienko" Date: Thu, 5 Jun 2014 23:11:03 +0200 Subject: [PATCH] fix problem with extending sorted set by one and attempting to increase random access capacity in case of value removal --- .../containers/ChangeBufferingList.java | 17 ++++++++++------- .../util/indexing/containers/SortedIdSet.java | 17 ++++++++++------- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/platform/lang-impl/src/com/intellij/util/indexing/containers/ChangeBufferingList.java b/platform/lang-impl/src/com/intellij/util/indexing/containers/ChangeBufferingList.java index 2f259fe62fb0..bbf2832d27a7 100644 --- a/platform/lang-impl/src/com/intellij/util/indexing/containers/ChangeBufferingList.java +++ b/platform/lang-impl/src/com/intellij/util/indexing/containers/ChangeBufferingList.java @@ -78,10 +78,10 @@ public class ChangeBufferingList implements Cloneable { } public void remove(int value) { - ensureCapacity(1); if (DEBUG) checkSet.remove(value); RandomAccessIntContainer intContainer = randomAccessContainer; if (intContainer == null) { + ensureCapacity(1); addChange(-value); } else { @@ -197,22 +197,25 @@ public class ChangeBufferingList implements Cloneable { if (changes == null) { changes = new int[Math.max(3, diff)]; } else if (length + diff > changes.length) { - int nextArraySize = Math.min( - Math.max(changes.length < 1024 ? changes.length << 1 : changes.length + changes.length / 5, length + diff), - MAX_FILES - ); - int[] newChanges = new int[nextArraySize]; + int[] newChanges = new int[calcNextArraySize(changes.length, length + diff)]; System.arraycopy(changes, 0, newChanges, 0, length); changes = newChanges; } } + static int calcNextArraySize(int currentSize, int wantedSize) { + return Math.min( + Math.max(currentSize < 1024 ? currentSize << 1 : currentSize + currentSize / 5, wantedSize), + MAX_FILES + ); + } + public boolean isEmpty() { if (randomAccessContainer == null) { if (changes == null) return true; if (removals == 0) return length == 0; } - // todo we can + // todo we can calculate isEmpty in more cases (without container) RandomAccessIntContainer intContainer = getRandomAccessContainer(); return intContainer.size() == 0; } diff --git a/platform/lang-impl/src/com/intellij/util/indexing/containers/SortedIdSet.java b/platform/lang-impl/src/com/intellij/util/indexing/containers/SortedIdSet.java index 341ca4160720..5405bbc2cf98 100644 --- a/platform/lang-impl/src/com/intellij/util/indexing/containers/SortedIdSet.java +++ b/platform/lang-impl/src/com/intellij/util/indexing/containers/SortedIdSet.java @@ -200,16 +200,19 @@ public class SortedIdSet implements Cloneable, RandomAccessIntContainer { } public RandomAccessIntContainer ensureContainerCapacity(int count) { - if (mySize + count > ChangeBufferingList.MAX_FILES) { + int newSize = mySetLength + count; + if (newSize < mySet.length) return this; + if (newSize > ChangeBufferingList.MAX_FILES) { return new IdBitSet(this, count); } - int newSize = mySetLength + count; + + newSize = ChangeBufferingList.calcNextArraySize(mySet.length, newSize); assert newSize < Short.MAX_VALUE; - if (newSize >= mySet.length) { - int[] newSet = new int[newSize]; // todo slightly increase size and compact - System.arraycopy(mySet, 0, newSet, 0, mySetLength); - mySet = newSet; - } + + int[] newSet = new int[newSize]; // todo slightly increase size and compact + System.arraycopy(mySet, 0, newSet, 0, mySetLength); + mySet = newSet; + return this; }