Skip to content

Commit

Permalink
performance optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
TeamDman committed Oct 7, 2024
1 parent 74e6e60 commit b6e6893
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,11 @@ default boolean matchesStack(Object stack) {
}

default boolean matchesCapabilityType(Object capability) {
return getResourceLimit()
.resourceIds()
.getReferencedResourceTypes()
.anyMatch(rt -> rt.matchesCapabilityType(capability));
for (ResourceType<?, ?, ?> resourceType : getResourceLimit().resourceIds().getReferencedResourceTypes()) {
if (resourceType.matchesCapabilityType(capability)) {
return true;
}
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ <STACK, ITEM, CAP> long getMaxTransferable(
);

default boolean matchesCapabilityType(Object capability) {
return getResourceLimit()
.resourceIds()
.getReferencedResourceTypes()
.anyMatch(rt -> rt.matchesCapabilityType(capability));
for (ResourceType<?, ?, ?> resourceType : getResourceLimit().resourceIds().getReferencedResourceTypes()) {
if (resourceType.matchesCapabilityType(capability)) {
return true;
}
}
return false;
}

default boolean matchesStack(Object stack) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import java.util.List;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class SimulateExploreAllPathsProgramBehaviour implements ProgramBehaviour {
Expand Down Expand Up @@ -182,11 +181,10 @@ public record Branch(
) implements ExecutionPathElement {
}

@SuppressWarnings("rawtypes")
public record IO(
IOStatement statement,
IOKind kind,
Set<ResourceType> usedResourceTypes,
Set<ResourceType<?,?,?>> usedResourceTypes,
Set<Label> usedLabels
) implements ExecutionPathElement {
public IO(IOStatement statement) {
Expand All @@ -196,7 +194,7 @@ public IO(IOStatement statement) {
statement instanceof InputStatement
? IOKind.INPUT
: (statement instanceof OutputStatement ? IOKind.OUTPUT : null),
statement.resourceLimits().getReferencedResourceTypes().collect(Collectors.toSet()),
statement.resourceLimits().getReferencedResourceTypes(),
new HashSet<>(statement.labelAccess().labels())
);
if (kind == null) {
Expand Down
30 changes: 14 additions & 16 deletions src/main/java/ca/teamdman/sfml/ast/InputStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
import net.minecraft.core.Direction;
import org.jetbrains.annotations.Nullable;

import java.util.*;
import java.util.ArrayDeque;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -75,31 +78,26 @@ public void gatherSlots(
};
}

// identify distinct resource types for capability gathering
Set<ResourceType> referencedResourceTypes = resourceLimits
.getReferencedResourceTypes()
.collect(Collectors.toSet());

if (!each) {
// log not each
context.getLogger().debug(x -> x.accept(LOG_PROGRAM_TICK_IO_STATEMENT_GATHER_SLOTS_NOT_EACH.get()));

// create a single matcher to be shared by all capabilities
List<IInputResourceTracker> inputTrackers = resourceLimits.createInputTrackers();
for (var type : referencedResourceTypes) {
for (var resourceType : resourceLimits.getReferencedResourceTypes()) {
// log gather for resource type
context
.getLogger()
.debug(x -> x.accept(LOG_PROGRAM_TICK_IO_STATEMENT_GATHER_SLOTS_FOR_RESOURCE_TYPE.get(
type.displayAsCapabilityClass(),
type.displayAsCapabilityClass()
resourceType.displayAsCapabilityClass(),
resourceType.displayAsCapabilityClass()
)));

// gather slots for each capability found for positions tagged by a provided label
Consumer<LimitedInputSlot<?, ?, ?>> finalSlotConsumer = slotConsumer;
type.forEachCapability(context, labelAccess, (label, pos, direction, cap) -> gatherSlotsForCap(
resourceType.forEachCapability(context, labelAccess, (label, pos, direction, cap) -> gatherSlotsForCap(
context,
(ResourceType<Object, Object, Object>) type,
(ResourceType<Object, Object, Object>) resourceType,
label, pos, direction, cap,
inputTrackers,
finalSlotConsumer
Expand All @@ -109,22 +107,22 @@ public void gatherSlots(
// log yes each
context.getLogger().debug(x -> x.accept(LOG_PROGRAM_TICK_IO_STATEMENT_GATHER_SLOTS_EACH.get()));

for (ResourceType type : referencedResourceTypes) {
for (var resourceType : resourceLimits.getReferencedResourceTypes()) {
// log gather for resource type
context
.getLogger()
.debug(x -> x.accept(LOG_PROGRAM_TICK_IO_STATEMENT_GATHER_SLOTS_FOR_RESOURCE_TYPE.get(
type.displayAsCapabilityClass(),
type.displayAsCapabilityClass()
resourceType.displayAsCapabilityClass(),
resourceType.displayAsCapabilityClass()
)));

// gather slots for each capability found for positions tagged by a provided label
Consumer<LimitedInputSlot<?, ?, ?>> finalSlotConsumer = slotConsumer;
type.forEachCapability(context, labelAccess, (label, pos, direction, cap) -> {
resourceType.forEachCapability(context, labelAccess, (label, pos, direction, cap) -> {
List<IInputResourceTracker> inputTrackers = resourceLimits.createInputTrackers();
gatherSlotsForCap(
context,
(ResourceType<Object, Object, Object>) type,
(ResourceType<Object, Object, Object>) resourceType,
label, pos, direction, cap,
inputTrackers,
finalSlotConsumer
Expand Down
23 changes: 10 additions & 13 deletions src/main/java/ca/teamdman/sfml/ast/OutputStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import java.util.Objects;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static ca.teamdman.sfm.common.localization.LocalizationKeys.*;

Expand Down Expand Up @@ -346,23 +345,21 @@ public void gatherSlots(
) {
context.getLogger().debug(x -> x.accept(LOG_PROGRAM_TICK_IO_STATEMENT_GATHER_SLOTS.get(toStringPretty())));

Stream<ResourceType> types = resourceLimits.getReferencedResourceTypes();

if (!each) {
context.getLogger().debug(x -> x.accept(LOG_PROGRAM_TICK_IO_STATEMENT_GATHER_SLOTS_NOT_EACH.get()));
// create a single list of trackers to be shared between all limited slots
List<IOutputResourceTracker> outputTracker = resourceLimits.createOutputTrackers();
for (var type : (Iterable<ResourceType>) types::iterator) {
for (var resourceType : resourceLimits.getReferencedResourceTypes()) {
context
.getLogger()
.debug(x -> x.accept(LOG_PROGRAM_TICK_IO_STATEMENT_GATHER_SLOTS_FOR_RESOURCE_TYPE.get(
type.displayAsCapabilityClass(),
type.displayAsCapabilityClass()
resourceType.displayAsCapabilityClass(),
resourceType.displayAsCapabilityClass()
)));
type.forEachCapability(context, labelAccess, (
resourceType.forEachCapability(context, labelAccess, (
(label, pos, direction, cap) -> gatherSlotsForCap(
context,
(ResourceType<Object, Object, Object>) type,
(ResourceType<Object, Object, Object>) resourceType,
label,
pos,
direction,
Expand All @@ -374,19 +371,19 @@ public void gatherSlots(
}
} else {
context.getLogger().debug(x -> x.accept(LOG_PROGRAM_TICK_IO_STATEMENT_GATHER_SLOTS_EACH.get()));
for (var type : (Iterable<ResourceType>) types::iterator) {
for (var resourceType : resourceLimits.getReferencedResourceTypes()) {
context
.getLogger()
.debug(x -> x.accept(LOG_PROGRAM_TICK_IO_STATEMENT_GATHER_SLOTS_FOR_RESOURCE_TYPE.get(
type.displayAsCapabilityClass(),
type.displayAsCapabilityClass()
resourceType.displayAsCapabilityClass(),
resourceType.displayAsCapabilityClass()
)));
type.forEachCapability(context, labelAccess, (label, pos, direction, cap) -> {
resourceType.forEachCapability(context, labelAccess, (label, pos, direction, cap) -> {
// create a new list of trackers for each limited slot
List<IOutputResourceTracker> outputTracker = resourceLimits.createOutputTrackers();
gatherSlotsForCap(
context,
(ResourceType<Object, Object, Object>) type,
(ResourceType<Object, Object, Object>) resourceType,
label,
pos,
direction,
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/ca/teamdman/sfml/ast/ResourceComparer.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public BoolExpr toBooleanExpression(SetOperator setOp, LabelAccess labelAccess,
type.forEachCapability(context, labelAccess, (label, pos, direction, cap) -> {
long inThisInv = 0;
for (var stack : (Iterable<STACK>) type.getStacksInSlots(cap, labelAccess.slots())::iterator) {
if (this.res.test(stack)) {
if (this.res.matchesStack(stack)) {
inThisInv += type.getAmount(stack);
overallCount.addAndGet(type.getAmount(stack));
}
Expand Down
18 changes: 8 additions & 10 deletions src/main/java/ca/teamdman/sfml/ast/ResourceIdSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
import net.minecraft.resources.ResourceLocation;
import org.jetbrains.annotations.Nullable;

import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Objects;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand All @@ -24,11 +21,12 @@ public ResourceIdSet(Collection<ResourceIdentifier<?, ?, ?>> contents) {
this(new LinkedHashSet<>(contents));
}

@SuppressWarnings("rawtypes")
public Stream<ResourceType> getReferencedResourceTypes() { // todo: cache maybe? check perf
return this.stream()
.map((ResourceIdentifier x) -> x.getResourceType())
.distinct();
public Set<ResourceType<?,?,?>> getReferencedResourceTypes() {
HashSet<ResourceType<?,?,?>> rtn = new HashSet<>(8);
for (ResourceIdentifier<?, ?, ?> resourceId : this.resourceIds) {
rtn.add(resourceId.getResourceType());
}
return rtn;
}

public boolean couldMatchMoreThanOne() {
Expand All @@ -51,7 +49,7 @@ public ResourceIdSet(

public @Nullable ResourceIdentifier<?, ?, ?> getMatchingFromStack(Object stack) {
for (ResourceIdentifier<?, ?, ?> entry : resourceIds) {
if (entry.test(stack)) {
if (entry.matchesStack(stack)) {
return entry;
}
}
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/ca/teamdman/sfml/ast/ResourceIdentifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

// resourceTypeName resourceNamespace, resourceTypeName name, resource resourceNamespace, resource name
// sfm:item:minecraft:stone
public class ResourceIdentifier<STACK, ITEM, CAP> implements ASTNode, Predicate<Object> {
public class ResourceIdentifier<STACK, ITEM, CAP> implements ASTNode {

public static final ResourceIdentifier<?, ?, ?> MATCH_ALL = new ResourceIdentifier<>(
".*",
Expand Down Expand Up @@ -111,8 +111,7 @@ public Optional<ResourceLocation> getLocation() {
}
}

@Override
public boolean test(Object other) {
public boolean matchesStack(Object other) {
ResourceType<STACK, ITEM, CAP> resourceType = getResourceType();
return resourceType != null && resourceType.matchesStack(this, other);
}
Expand Down
15 changes: 8 additions & 7 deletions src/main/java/ca/teamdman/sfml/ast/ResourceLimits.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
import ca.teamdman.sfm.common.resourcetype.ResourceType;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public record ResourceLimits(
List<ResourceLimit> resourceLimitList,
Expand Down Expand Up @@ -36,12 +37,12 @@ public ResourceLimits withExclusions(ResourceIdSet exclusions) {
return new ResourceLimits(resourceLimitList, exclusions);
}

@SuppressWarnings("rawtypes")
public Stream<ResourceType> getReferencedResourceTypes() {
return resourceLimitList()
.stream()
.flatMap(resourceLimits -> resourceLimits.resourceIds().getReferencedResourceTypes())
.distinct();
public Set<ResourceType<?,?,?>> getReferencedResourceTypes() {
Set<ResourceType<?,?,?>> rtn = new HashSet<>(8);
for (ResourceLimit resourceLimit : resourceLimitList) {
rtn.addAll(resourceLimit.resourceIds().getReferencedResourceTypes());
}
return rtn;
}

@Override
Expand Down

0 comments on commit b6e6893

Please sign in to comment.