Skip to content
This repository
Browse code

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

…ct ensure even byte allocations.... what was I thinking.
  • Loading branch information...
commit 13bef501abc9fc71238a2991b5e54480225e7b6b 1 parent 73c5682
Rolf authored September 09, 2012

Showing 1 changed file with 7 additions and 4 deletions. Show diff stats Hide diff stats

  1. 11  core/src/java/org/jdom2/ContentList.java
11  core/src/java/org/jdom2/ContentList.java
@@ -416,11 +416,14 @@ void ensureCapacity(final int minCapacity) {
416 416
 		} else if (minCapacity < elementData.length) {
417 417
 			return;
418 418
 		}
419  
-		// most JVM's allocate memory in multiples of 'double-words', on
420  
-		// 64-bit it's 16-bytes, on 32-bit it's 8 bytes which all means it makes
421  
-		// sense to increment the capacity in even values.
  419
+		// use algorithm Wilf suggests which is essentially the same
  420
+		// as algorithm as ArrayList.ensureCapacity....
  421
+		// typically the minCapacity is only slightly larger than
  422
+		// the current capacity.... so grow from the current capacity
  423
+		// with a double-check.
  424
+		final int newcap = ((size * 3) / 2) + 1;
422 425
 		elementData = ArrayCopy.copyOf(elementData, 
423  
-				((minCapacity + INITIAL_ARRAY_SIZE) >>> 1) << 1);
  426
+				(newcap < minCapacity ? minCapacity : newcap));
424 427
 	}
425 428
 
426 429
 	/**

0 notes on commit 13bef50

Please sign in to comment.
Something went wrong with that request. Please try again.