Merge "Bazel: bump to 8.6.0 and switch to pure Bzlmod"
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/PackBitmapIndexRemapper.java b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/PackBitmapIndexRemapper.java
index ffbc073..4d639a3 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/PackBitmapIndexRemapper.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/PackBitmapIndexRemapper.java
@@ -36,6 +36,8 @@ public class PackBitmapIndexRemapper
 	private final BitSet inflated;
 	private final int[] prevToNewMapping;
 
+	private final boolean reuseOldBitmapAsIs;
+
 	/**
 	 * A PackBitmapIndex that maps the positions in the prevBitmapIndex to the
 	 * ones in the newIndex.
@@ -65,6 +67,7 @@ private PackBitmapIndexRemapper(PackBitmapIndex newPackIndex) {
 		this.newPackIndex = newPackIndex;
 		this.inflated = null;
 		this.prevToNewMapping = null;
+		this.reuseOldBitmapAsIs = false;
 	}
 
 	private PackBitmapIndexRemapper(
@@ -73,10 +76,18 @@ private PackBitmapIndexRemapper(
 		this.newPackIndex = newPackIndex;
 		inflated = new BitSet(newPackIndex.getObjectCount());
 
-		prevToNewMapping = new int[oldPackIndex.getObjectCount()];
-		for (int pos = 0; pos < prevToNewMapping.length; pos++)
-			prevToNewMapping[pos] = newPackIndex.findPosition(
-					oldPackIndex.getObject(pos));
+		int[] prevToNewMappingTmp = new int[oldPackIndex.getObjectCount()];
+		boolean allPositionsMatch = true;
+		for (int pos = 0; pos < prevToNewMappingTmp.length; pos++) {
+			prevToNewMappingTmp[pos] = newPackIndex
+					.findPosition(oldPackIndex.getObject(pos));
+			if (prevToNewMappingTmp[pos] != pos) {
+				allPositionsMatch = false;
+			}
+		}
+		this.reuseOldBitmapAsIs = allPositionsMatch;
+		// We do not need it!
+		this.prevToNewMapping = allPositionsMatch ? null : prevToNewMappingTmp;
 	}
 
 	@Override
@@ -169,6 +180,10 @@ public EWAHCompressedBitmap getBitmap(AnyObjectId objectId) {
 		if (newPackIndex.findPosition(objectId) == -1)
 			return null;
 
+		if (reuseOldBitmapAsIs) {
+			return oldBitmap.getBitmap();
+		}
+
 		inflated.clear();
 		for (IntIterator i = oldBitmap.getBitmapWithoutCaching()
 				.intIterator(); i.hasNext();)