Skip to content

Commit 1171aef

Browse files
committed
Simplification: don't copy identity ops
If during simplification the output/mutable input ops are identity ops, then we do not need a copy op to move data between them.
1 parent 6230d45 commit 1171aef

File tree

2 files changed

+33
-26
lines changed

2 files changed

+33
-26
lines changed

scijava-ops-engine/src/main/java/org/scijava/ops/engine/matcher/simplify/SimplificationUtils.java

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -228,31 +228,32 @@ public static SimplifiedOpInfo simplifyInfo(OpEnvironment env, OpInfo info) {
228228
) {
229229
int ioIndex = SimplificationUtils.findMutableArgIndex(Types.raw(info
230230
.opType()));
231-
// If IO index is -1, output is returned - no need to copy.
232-
if (ioIndex == -1) {
233-
return null;
231+
if (ioIndex > -1 && !(isIdentity(inFocusers.get(ioIndex)) && isIdentity(outSimplifier))) {
232+
// Otherwise, we need an Op to convert the simple output back into the
233+
// pre-focused input
234+
// Determine simple output type.
235+
var simpleOut = outType(info.outputType(), outSimplifier);
236+
// Determine unfocused input type.
237+
var focuserInfo = Ops.info(inFocusers.get(ioIndex));
238+
Map<TypeVariable<?>, Type> typeAssigns = new HashMap<>();
239+
GenericAssignability.inferTypeVariables( //
240+
new Type[] { focuserInfo.outputType() }, //
241+
new Type[] { info.inputTypes().get(ioIndex) }, //
242+
typeAssigns //
243+
);
244+
var unfocusedInput = Nil.of(Types.mapVarToTypes( //
245+
focuserInfo.inputTypes().get(0), //
246+
typeAssigns //
247+
));
248+
// Match a copier
249+
return Ops.rich(env.unary("engine.copy", h) //
250+
.inType(simpleOut) //
251+
.outType(unfocusedInput) //
252+
.computer());
234253
}
235-
// Otherwise, we need an Op to convert the simple output back into the
236-
// pre-focused input
237-
// Determine simple output type.
238-
var simpleOut = outType(info.outputType(), outSimplifier);
239-
// Determine unfocused input type.
240-
var focuserInfo = Ops.info(inFocusers.get(ioIndex));
241-
Map<TypeVariable<?>, Type> typeAssigns = new HashMap<>();
242-
GenericAssignability.inferTypeVariables( //
243-
new Type[] { focuserInfo.outputType() }, //
244-
new Type[] { info.inputTypes().get(ioIndex) }, //
245-
typeAssigns //
246-
);
247-
var unfocusedInput = Nil.of(Types.mapVarToTypes( //
248-
focuserInfo.inputTypes().get(0), //
249-
typeAssigns //
250-
));
251-
// Match a copier
252-
return Ops.rich(env.unary("engine.copy", h) //
253-
.inType(simpleOut) //
254-
.outType(unfocusedInput) //
255-
.computer());
254+
// No mutable parameter and/or pathway is an identity pathway, so no copy
255+
// op necessary
256+
return null;
256257
}
257258

258259
/**

scijava-ops-engine/src/main/java/org/scijava/ops/engine/matcher/simplify/SimplifiedOpRequest.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@
5252
import org.scijava.types.Types;
5353
import org.scijava.types.inference.GenericAssignability;
5454

55+
import static org.scijava.ops.engine.matcher.simplify.SimplificationUtils.isIdentity;
56+
5557
public class SimplifiedOpRequest implements OpRequest {
5658

5759
/** Name of the op, or null for any name. */
@@ -104,8 +106,12 @@ public SimplifiedOpRequest(OpRequest req, OpEnvironment env) {
104106
type = SimplificationUtils.retypeOpType(req.getType(), inTypes, outType);
105107
// TODO: Fix
106108
int mutableIndex = SimplificationUtils.findMutableArgIndex(Types.raw(type));
107-
this.outputCopier = mutableIndex == -1 ? null : simplifierCopyOp(env, req
108-
.getArgs()[mutableIndex]);
109+
if (mutableIndex >= 0 && !(isIdentity(inSimplifiers.get(mutableIndex))
110+
&& isIdentity(outFocuser))) {
111+
this.outputCopier = simplifierCopyOp(env, req.getArgs()[mutableIndex]);
112+
} else {
113+
this.outputCopier = null;
114+
}
109115
}
110116

111117
private Type[] simpleArgs(Type[] args, List<RichOp<Function<?, ?>>> ops) {

0 commit comments

Comments
 (0)