Faster free bitmap search

This commit is contained in:
DBotThePony 2024-02-17 23:43:39 +07:00
parent 86ad24ccdb
commit 486e8834d6
Signed by: DBot
GPG Key ID: DCC23B5715498507

View File

@ -5,7 +5,6 @@ import it.unimi.dsi.fastutil.ints.IntArraySet
import it.unimi.dsi.fastutil.ints.IntOpenHashSet
import it.unimi.dsi.fastutil.objects.Object2IntAVLTreeMap
import it.unimi.dsi.fastutil.objects.ObjectArrayList
import it.unimi.dsi.fastutil.objects.ObjectArraySet
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet
import ru.dbotthepony.kommons.util.CarriedExecutor
import ru.dbotthepony.kommons.util.KOptional
@ -451,23 +450,16 @@ class BTreeDB6 private constructor(override val file: File, private var reader:
return result
}
if (after != INVALID_BLOCK_INDEX) {
if (after != INVALID_BLOCK_INDEX && !occupiedBlocksBitmap[after + 1]) {
// if we can allocate enough blocks after specified index, allocate there (reduce fragmentation)
var correct = true
val findOccupied = occupiedBlocksBitmap.nextSetBit(after + 1)
val find = after + 1
for (i2 in 0 until size) {
if (occupiedBlocksBitmap[i2 + find]) {
correct = false
break
}
}
if (correct) {
if (findOccupied == -1 || findOccupied - find >= size) {
val blocks = ArrayList<Block>(size)
occupiedBlocksBitmap.set(find, find + size)
for (i2 in 0 until size) {
occupiedBlocksBitmap[i2 + find] = true
val block = Block(i2 + find)
block.nextBlock = INVALID_BLOCK_INDEX
block.type = type
@ -482,29 +474,24 @@ class BTreeDB6 private constructor(override val file: File, private var reader:
var i = 0
while (true) {
val find = occupiedBlocksBitmap.nextClearBit(i)
var correct = true
val findFree = occupiedBlocksBitmap.nextClearBit(i)
val findOccupied = occupiedBlocksBitmap.nextSetBit(findFree)
for (i2 in 0 until size) {
if (occupiedBlocksBitmap[i2 + find]) {
correct = false
i = find + i2 + 1
break
}
}
if (correct) {
if (findOccupied == -1 || findOccupied - findFree >= size) {
val blocks = ArrayList<Block>(size)
occupiedBlocksBitmap.set(findFree, findFree + size)
for (i2 in 0 until size) {
occupiedBlocksBitmap[i2 + find] = true
val block = Block(i2 + find)
val block = Block(i2 + findFree)
block.nextBlock = INVALID_BLOCK_INDEX
block.type = type
blocks.add(block)
}
return blocks
} else {
i = findOccupied + 1
}
}
}