Skip to content

Commit

Permalink
improved performance of WeightedListProcessor
Browse files Browse the repository at this point in the history
  • Loading branch information
ImplementsLegend committed Sep 25, 2024
1 parent 2132860 commit c99404d
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
51 changes: 51 additions & 0 deletions src/main/java/implementslegend/mod/vaultfaster/FastWeightedList.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package implementslegend.mod.vaultfaster

import iskallia.vault.core.random.RandomSource
import iskallia.vault.util.data.WeightedList
import java.util.random.RandomGenerator
import kotlin.random.Random

class FastWeightedList<T> private constructor(capacity:Int) {

var totalWeight:Float = 0f

var weights = FloatArray(capacity)
var values = ArrayList<T>(capacity)

val entries = weights.zip(values).filter { it.first!=0f }

constructor(w:iskallia.vault.core.util.WeightedList<T>):this(w.entries.size){
w.entries.sortedBy { it.value }.forEach {
mutableEntry ->
totalWeight+=mutableEntry.value.toFloat()
values+=mutableEntry.key
weights[values.size-1]=mutableEntry.value.toFloat()
}
}


constructor(w:WeightedList<T>):this(w.size){
w.sortedBy { it.weight }.forEachIndexed {
index, mutableEntry ->
totalWeight+=mutableEntry.weight.toFloat()
weights[index]=mutableEntry.weight.toFloat()
values[index]=mutableEntry.value
}
}


fun random(rng:RandomGenerator) = random(rng::nextFloat)
fun random(rng:RandomSource) = random(rng::nextFloat)
fun random(rng:Random) = random(rng::nextFloat)
fun random(rng:()->Float):T?{
if(totalWeight<=0f)return null
weights.foldIndexed(rng()*totalWeight){
idx,acc,weight->
if(acc<weight)return@random values[idx]
acc-weight
}
return values.last()
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package implementslegend.mod.vaultfaster.mixin;

import implementslegend.mod.vaultfaster.FastWeightedList;
import iskallia.vault.core.random.RandomSource;
import iskallia.vault.core.util.WeightedList;
import iskallia.vault.core.world.data.tile.PartialTile;
import iskallia.vault.core.world.processor.tile.TileProcessor;
import iskallia.vault.core.world.processor.tile.WeightedTileProcessor;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;

import java.util.Optional;

@Mixin(WeightedTileProcessor.class)
public class MixinWeightedProcessor {
private FastWeightedList<PartialTile> cached;

@Redirect(method = "process(Liskallia/vault/core/world/data/tile/PartialTile;Liskallia/vault/core/world/processor/ProcessorContext;)Liskallia/vault/core/world/data/tile/PartialTile;",at= @At(value = "INVOKE", target = "Liskallia/vault/core/util/WeightedList;getRandom(Liskallia/vault/core/random/RandomSource;)Ljava/util/Optional;"),remap = false)
private Optional<PartialTile> fastRandom(WeightedList<PartialTile> instance, RandomSource random){
var list = cached;
if(list==null){
list=new FastWeightedList<>(instance);
cached=list;
}
return Optional.ofNullable(list.random(random));
}
}
1 change: 1 addition & 0 deletions src/main/resources/vaultfaster.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"MixinTemplate",
"MixinTemplateProcessorModifier",
"MixinVaultLayout",
"MixinWeightedProcessor",
"NoBiomeDecorations",
"OctahedralGroupFix",
"PartialBlockIDAccessor",
Expand Down

0 comments on commit c99404d

Please sign in to comment.