Skip to content

Commit 8ec20cd

Browse files
committed
Merge remote-tracking branch 'SwingURM/optimize-fluidstack-copy' into dev
2 parents d0a9e3f + 1626de8 commit 8ec20cd

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

src/main/java/crazypants/enderio/conduit/liquid/EnderLiquidConduitNetwork.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ public void rewind() {
6565

6666
boolean filling;
6767

68+
// Reusable FluidStack to avoid allocations in fillFrom loop
69+
private FluidStack reusableFluidStack;
70+
6871
public EnderLiquidConduitNetwork(AbstractEnderLiquidConduit.Type type) {
6972
super(AbstractEnderLiquidConduit.class, ILiquidConduit.class);
7073
this.type = type;
@@ -144,8 +147,13 @@ public int fillFrom(NetworkTank tank, FluidStack resource, boolean doFill) {
144147
if (resource == null || tank == null || !matchedFilter(resource, tank.con, tank.conDir, true)) {
145148
return 0;
146149
}
147-
resource = resource.copy();
148-
resource.amount = Math.min(resource.amount, type.getMaxIoPerTick());
150+
151+
// Only copy if we need to modify amount
152+
int maxIo = type.getMaxIoPerTick();
153+
if (resource.amount > maxIo) {
154+
resource = resource.copy();
155+
resource.amount = maxIo;
156+
}
149157
int filled = 0;
150158
int remaining = resource.amount;
151159
// TODO: Only change starting pos of iterator is doFill is true so a false then true returns the same
@@ -158,13 +166,19 @@ public int fillFrom(NetworkTank tank, FluidStack resource, boolean doFill) {
158166
&& target.acceptsOuput
159167
&& target.isValid()
160168
&& matchedFilter(resource, target.con, target.conDir, false)) {
161-
int vol = target.externalTank.fill(target.tankDir, resource.copy(), doFill);
169+
// Use reused FluidStack instead of copying each iteration
170+
if (reusableFluidStack == null
171+
|| !FluidStack.areFluidStackTagsEqual(resource, reusableFluidStack)) {
172+
reusableFluidStack = resource.copy();
173+
} else {
174+
reusableFluidStack.amount = remaining;
175+
}
176+
int vol = target.externalTank.fill(target.tankDir, reusableFluidStack, doFill);
162177
remaining -= vol;
163178
filled += vol;
164179
if (remaining <= 0) {
165180
break;
166181
}
167-
resource.amount = remaining;
168182
}
169183
}
170184
if (!tank.con.isRoundRobin(tank.conDir)) iterator.rewind();

0 commit comments

Comments
 (0)