SystemYou are an expert SpongePowered Mixin internals developer. You understand how mixins work at the bytecode level using ASM, and can debug, write, and optimize mixins.
You are an expert SpongePowered Mixin internals developer. You understand how mixins work at the bytecode level using ASM, and can debug, write, and optimize mixins.
User
What are the performance implications of mixin injection? How can you optimize mixin code for hot paths?
Assistant
Mixins have minimal overhead per-injection, but poorly written mixins in hot paths can cause significant performance issues.
Overhead analysis:
java
// @Inject overhead: minimal (just a method call + boolean check)// Before:publicvoidtick() {
// original code
}
// After @Inject at HEAD with cancellable:publicvoidtick() {
CallbackInfoci=newCallbackInfo("tick", "()V", this); // allocation!
MyMixin.onTick(this, ci);
if (ci.hasCancelled()) return; // branch// original code
}
Allocation problem in hot paths:
java
// BAD: Creating objects in tick() (60 times/second)@Inject(method = "tick", at = @At("HEAD"))privatevoidonTick(CallbackInfo ci) {
// This creates a new ArrayList EVERY tick!
List<ItemStack> items = newArrayList<>(this.inventory);
for (ItemStack stack : items) {
// process
}
}
// GOOD: Reuse allocations@Uniqueprivatefinal List<ItemStack> tempItems = newArrayList<>();
@Inject(method = "tick", at = @At("HEAD"))privatevoidonTick(CallbackInfo ci) {
tempItems.clear();
tempItems.addAll(this.inventory);
for (ItemStack stack : tempItems) {
// process
}
}
@Redirect overhead:
java
// @Redirect replaces a call - no additional overhead// Same as calling the replacement method directly// BAD: Expensive check in redirect@Redirect(method = "tick", at = @At(value = "INVOKE", target = "..."))privatefloatredirectHealth() {
// Registry lookup every tick!return Registry.ITEM.get(this.itemId).getHealth();
}
// GOOD: Cache the lookup@Uniqueprivate Item cachedItem;
@Uniqueprivateboolean itemCached;
@Redirect(method = "tick", at = @At(value = "INVOKE", target = "..."))privatefloatredirectHealth() {
if (!itemCached) {
cachedItem = Registry.ITEM.get(this.itemId);
itemCached = true;
}
return cachedItem.getHealth();
}
@ModifyVariable is free:
java
// @ModifyVariable just modifies a register - zero overhead@ModifyVariable(method = "tick", at = @At("HEAD"), ordinal = 0)privateintmodifySpeed(int original) {
return original * 2; // Single IADD instruction
}
Optimization strategies:
java
// 1. Use @ModifyVariable over @Inject when modifying values// 2. Use @WrapOperation over @Redirect for composability// 3. Avoid allocations in injected methods// 4. Cache repeated computations// 5. Use @Unique for mixin-specific fields// 6. Minimize injection points - fewer is better// 7. Profile with Spark before optimizing// Example: Efficient entity tick mixin@Mixin(Entity.class)publicclassEntityMixinOptimized {
@Uniqueprivateint cachedTickCount;
@ModifyVariable(method = "tick", at = @At("HEAD"), ordinal = 0)privateintoptimizeTickInterval(int original) {
// Runs once per tick, modifies the value directlyreturn Math.max(1, original / 2);
}
}
Performance measurement:
bash
# Use Spark to profile mixin overhead:
/spark profiler
# Look for mixin-injected methods in the flamegraph# Compare with baseline (no mixins)
The key insight: mixins themselves add negligible overhead. Performance issues come from what you put in the injected code, not the injection mechanism.