-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improved performance of WeightedListProcessor
- Loading branch information
1 parent
2132860
commit c99404d
Showing
3 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
src/main/java/implementslegend/mod/vaultfaster/FastWeightedList.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
|
||
|
||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/implementslegend/mod/vaultfaster/mixin/MixinWeightedProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters