Permalink
Browse files

Remove ill-conceived +2 logic for ensureCapacity which does not in fa…

…ct ensure even byte allocations.... what was I thinking.
  • Loading branch information...
rolfl committed Sep 9, 2012
1 parent 73c5682 commit 13bef501abc9fc71238a2991b5e54480225e7b6b
Showing with 7 additions and 4 deletions.
  1. +7 −4 core/src/java/org/jdom2/ContentList.java
@@ -416,11 +416,14 @@ void ensureCapacity(final int minCapacity) {
} else if (minCapacity < elementData.length) {
return;
}
- // most JVM's allocate memory in multiples of 'double-words', on
- // 64-bit it's 16-bytes, on 32-bit it's 8 bytes which all means it makes
- // sense to increment the capacity in even values.
+ // use algorithm Wilf suggests which is essentially the same
+ // as algorithm as ArrayList.ensureCapacity....
+ // typically the minCapacity is only slightly larger than
+ // the current capacity.... so grow from the current capacity
+ // with a double-check.
+ final int newcap = ((size * 3) / 2) + 1;
elementData = ArrayCopy.copyOf(elementData,
- ((minCapacity + INITIAL_ARRAY_SIZE) >>> 1) << 1);
+ (newcap < minCapacity ? minCapacity : newcap));
}
/**

0 comments on commit 13bef50

Please sign in to comment.