Skip to content

Commit e2e09dc

Browse files
gselzerhinerm
authored andcommitted
LazilyGenerated -> Synthesized
Now they're generated eagerly - saves about 10 seconds on my build time!
1 parent d791d4f commit e2e09dc

6 files changed

Lines changed: 64 additions & 193 deletions

File tree

‎scijava/scijava-ops-engine/src/main/java/org/scijava/ops/engine/struct/ClassParameterMemberParser.java‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import java.util.Arrays;
3737
import java.util.List;
3838

39-
import org.scijava.ops.api.Ops;
4039
import org.scijava.ops.engine.exceptions.impl.FunctionalTypeOpException;
4140
import org.scijava.struct.MemberParser;
4241
import org.scijava.struct.Structs;
@@ -69,7 +68,7 @@ public List<SynthesizedParameterMember<?>> parse(Class<?> source, Type structTyp
6968

7069
// obtain a parameterData
7170
Class<?> fIface = FunctionalInterfaces.findFrom(source);
72-
ParameterData paramData = new LazilyGeneratedMethodParameterData(opMethod, fIface);
71+
ParameterData paramData = new SynthesizedMethodParameterData(opMethod, fIface);
7372

7473
try {
7574
FunctionalParameters.parseFunctionalParameters(items, source, paramData);

‎scijava/scijava-ops-engine/src/main/java/org/scijava/ops/engine/struct/FieldParameterMemberParser.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public List<SynthesizedParameterMember<?>> parse(FieldInstance source, Type stru
4646
if (source == null) return null;
4747

4848
// obtain a parameterData (preferably one that scrapes the javadoc)
49-
ParameterData paramData = new LazilyGeneratedFieldParameterData(source);
49+
ParameterData paramData = new SynthesizedFieldParameterData(source);
5050

5151
final ArrayList<SynthesizedParameterMember<?>> items = new ArrayList<>();
5252

‎scijava/scijava-ops-engine/src/main/java/org/scijava/ops/engine/struct/MethodParameterMemberParser.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public List<SynthesizedParameterMember<?>> parse(Method source, Type structType)
5050
source.setAccessible(true);
5151

5252
// obtain a parameterData
53-
ParameterData paramData = new LazilyGeneratedMethodParameterData(source, Types.raw(structType));
53+
ParameterData paramData = new SynthesizedMethodParameterData(source, Types.raw(structType));
5454

5555
final ArrayList<SynthesizedParameterMember<?>> items = new ArrayList<>();
5656

scijava/scijava-ops-engine/src/main/java/org/scijava/ops/engine/struct/LazilyGeneratedFieldParameterData.java renamed to scijava/scijava-ops-engine/src/main/java/org/scijava/ops/engine/struct/SynthesizedFieldParameterData.java

Lines changed: 32 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,11 @@
3232
import java.lang.reflect.Field;
3333
import java.lang.reflect.Method;
3434
import java.util.ArrayList;
35-
import java.util.HashMap;
3635
import java.util.List;
37-
import java.util.Map;
38-
import java.util.stream.Collectors;
3936

40-
import org.scijava.function.Producer;
41-
import org.scijava.ops.api.Ops;
4237
import org.scijava.ops.engine.exceptions.impl.NullablesOnMultipleMethodsException;
4338
import org.scijava.struct.FunctionalMethodType;
39+
import org.scijava.struct.ItemIO;
4440
import org.scijava.types.inference.FunctionalInterfaces;
4541

4642
/**
@@ -52,91 +48,66 @@
5248
*
5349
* @author Gabriel Selzer
5450
*/
55-
public class LazilyGeneratedFieldParameterData implements ParameterData {
56-
57-
private static final Map<FieldInstance, MethodParamInfo> paramDataMap =
58-
new HashMap<>();
51+
public class SynthesizedFieldParameterData implements ParameterData {
5952

6053
private final FieldInstance fieldInstance;
54+
private final int numInputs;
6155

62-
public LazilyGeneratedFieldParameterData(FieldInstance fieldInstance) {
56+
public SynthesizedFieldParameterData(FieldInstance fieldInstance) {
6357
this.fieldInstance = fieldInstance;
58+
this.numInputs = FunctionalInterfaces //
59+
.functionalMethodOf(fieldInstance.field().getType())
60+
.getParameterCount();
6461
}
6562

66-
public static MethodParamInfo getInfo(List<FunctionalMethodType> fmts,
67-
FieldInstance fieldInstance)
68-
{
69-
if (!paramDataMap.containsKey(fieldInstance)) generateFieldParamInfo(fmts,
70-
fieldInstance);
71-
return paramDataMap.get(fieldInstance);
72-
}
73-
74-
public static synchronized void generateFieldParamInfo(
75-
List<FunctionalMethodType> fmts, FieldInstance fieldInstance)
63+
@Override
64+
public List<SynthesizedParameterMember<?>> synthesizeMembers(
65+
List<FunctionalMethodType> fmts)
7666
{
77-
if (paramDataMap.containsKey(fieldInstance)) return;
78-
79-
Method sam = FunctionalInterfaces.functionalMethodOf(fieldInstance.field()
80-
.getType());
81-
// There is always one output, but we need the number of inputs
82-
long numIns = sam.getParameterCount();
83-
84-
// determine the Op inputs/outputs
85-
Boolean[] paramNullability = getParameterNullability(fieldInstance
86-
.instance(), fieldInstance.field(), (int) numIns);
8767

88-
paramDataMap.put(fieldInstance, synthesizedMethodParamInfo(fmts,
89-
paramNullability));
68+
// determine the optionality of each input
69+
Boolean[] optionality = getParameterNullability(fieldInstance
70+
.instance(), fieldInstance.field(), numInputs);
71+
// determine the name of each fmt
72+
List<String> names = getParameterNames(fmts);
73+
// map fmts to members
74+
int p = 0;
75+
List<SynthesizedParameterMember<?>> members = new ArrayList<>(fmts.size());
76+
for (FunctionalMethodType fmt: fmts) {
77+
String name = names.get(p);
78+
boolean optional = fmt.itemIO() != ItemIO.OUTPUT && optionality[p++];
79+
members.add(new SynthesizedParameterMember<>(fmt, name, !optional, ""));
80+
}
81+
return members;
9082
}
9183

92-
private static MethodParamInfo synthesizedMethodParamInfo(
93-
List<FunctionalMethodType> fmts, Boolean[] paramNullability)
84+
private List<String> getParameterNames(
85+
List<FunctionalMethodType> fmts)
9486
{
95-
Map<FunctionalMethodType, String> fmtNames = new HashMap<>(fmts.size());
96-
Map<FunctionalMethodType, String> fmtDescriptions = new HashMap<>(fmts
97-
.size());
98-
Map<FunctionalMethodType, Boolean> fmtNullability = new HashMap<>(fmts
99-
.size());
100-
87+
List<String> fmtNames = new ArrayList<>(fmts.size());
10188
int ins, outs, containers, mutables;
10289
ins = outs = containers = mutables = 1;
103-
int nullableIndex = 0;
10490
for (FunctionalMethodType fmt : fmts) {
105-
fmtDescriptions.put(fmt, "");
10691
switch (fmt.itemIO()) {
10792
case INPUT:
108-
fmtNames.put(fmt, "input" + ins++);
109-
fmtNullability.put(fmt, paramNullability[nullableIndex++]);
93+
fmtNames.add("input" + ins++);
11094
break;
11195
case OUTPUT:
112-
fmtNames.put(fmt, "output" + outs++);
96+
fmtNames.add("output" + outs++);
11397
break;
11498
case CONTAINER:
115-
fmtNames.put(fmt, "container" + containers++);
99+
fmtNames.add("container" + containers++);
116100
break;
117101
case MUTABLE:
118-
fmtNames.put(fmt, "mutable" + mutables++);
102+
fmtNames.add("mutable" + mutables++);
119103
break;
120104
default:
121105
throw new RuntimeException("Unexpected ItemIO type encountered!");
122106
}
123107
}
124-
return new MethodParamInfo(fmtNames, fmtDescriptions, fmtNullability);
125-
}
126-
127-
@Override
128-
public List<SynthesizedParameterMember<?>> synthesizeMembers(
129-
List<FunctionalMethodType> fmts)
130-
{
131-
Producer<MethodParamInfo> p = //
132-
() -> LazilyGeneratedFieldParameterData.getInfo(fmts, fieldInstance);
133-
134-
return fmts.stream() //
135-
.map(fmt -> new SynthesizedParameterMember<>(fmt, p)) //
136-
.collect(Collectors.toList());
108+
return fmtNames;
137109
}
138110

139-
// Helper methods
140111
private static Boolean[] getParameterNullability(Object instance, Field field,
141112
int opParams)
142113
{

scijava/scijava-ops-engine/src/main/java/org/scijava/ops/engine/struct/LazilyGeneratedMethodParameterData.java renamed to scijava/scijava-ops-engine/src/main/java/org/scijava/ops/engine/struct/SynthesizedMethodParameterData.java

Lines changed: 22 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,13 @@
3131

3232
import java.lang.reflect.Method;
3333
import java.lang.reflect.Parameter;
34-
import java.util.HashMap;
34+
import java.util.ArrayList;
3535
import java.util.List;
36-
import java.util.Map;
37-
import java.util.stream.Collectors;
3836

39-
import org.scijava.function.Producer;
40-
import org.scijava.ops.api.Ops;
4137
import org.scijava.ops.engine.exceptions.impl.NullablesOnMultipleMethodsException;
4238
import org.scijava.ops.spi.OpDependency;
4339
import org.scijava.struct.FunctionalMethodType;
44-
import org.scijava.types.inference.FunctionalInterfaces;
40+
import org.scijava.struct.ItemIO;
4541

4642
/**
4743
* Lazily generates the parameter data for a {@link List} of
@@ -52,119 +48,57 @@
5248
*
5349
* @author Gabriel Selzer
5450
*/
55-
public class LazilyGeneratedMethodParameterData implements ParameterData {
56-
57-
private static final Map<Method, MethodParamInfo> paramDataMap =
58-
new HashMap<>();
51+
public class SynthesizedMethodParameterData implements ParameterData {
5952

6053
private final Method m;
6154
private final Class<?> opType;
6255

63-
public LazilyGeneratedMethodParameterData(Method m, Class<?> opType) {
56+
public SynthesizedMethodParameterData(Method m, Class<?> opType) {
6457
this.m = m;
6558
this.opType = opType;
6659
}
6760

68-
public static MethodParamInfo getInfo(List<FunctionalMethodType> fmts,
69-
Method m, Class<?> opType)
70-
{
71-
if (!paramDataMap.containsKey(m)) generateMethodParamInfo(fmts, m, opType);
72-
MethodParamInfo info = paramDataMap.get(m);
73-
if (!info.containsAll(fmts)) updateMethodParamInfo(info, fmts, m, opType);
74-
return info;
75-
}
76-
77-
private static void updateMethodParamInfo(MethodParamInfo info,
78-
List<FunctionalMethodType> fmts, Method m, Class<?> opType)
79-
{
80-
Map<FunctionalMethodType, String> fmtNames = info.getFmtNames();
81-
Map<FunctionalMethodType, String> fmtDescriptions = info
82-
.getFmtDescriptions();
83-
Map<FunctionalMethodType, Boolean> fmtNullability = info
84-
.getFmtNullability();
85-
// determine the Op inputs/outputs
86-
long numOpParams = m.getParameterCount();
87-
long numReturns = m.getReturnType() == void.class ? 0 : 1;
88-
Boolean[] paramNullability = getParameterNullability(m, opType,
89-
(int) numOpParams);
90-
91-
addSynthesizedMethodParamInfo(fmtNames, fmtDescriptions, fmts,
92-
fmtNullability, paramNullability);
93-
}
94-
95-
public static synchronized void generateMethodParamInfo(
96-
List<FunctionalMethodType> fmts, Method m, Class<?> opType)
97-
{
98-
if (paramDataMap.containsKey(m)) return;
99-
100-
// determine the Op inputs/outputs
101-
long numOpParams = m.getParameterCount();
102-
long numReturns = m.getReturnType() == void.class ? 0 : 1;
103-
104-
opType = FunctionalInterfaces.findFrom(opType);
105-
Boolean[] paramNullability = getParameterNullability(m, opType,
106-
(int) numOpParams);
107-
108-
paramDataMap.put(m, synthesizedMethodParamInfo(fmts, paramNullability));
109-
}
110-
111-
private static void addSynthesizedMethodParamInfo(
112-
Map<FunctionalMethodType, String> fmtNames,
113-
Map<FunctionalMethodType, String> fmtDescriptions,
114-
List<FunctionalMethodType> fmts,
115-
Map<FunctionalMethodType, Boolean> fmtNullability,
116-
Boolean[] paramNullability)
61+
private List<String> getParameterNames(
62+
List<FunctionalMethodType> fmts)
11763
{
64+
List<String> fmtNames = new ArrayList<>(fmts.size());
11865
int ins, outs, containers, mutables;
11966
ins = outs = containers = mutables = 1;
120-
int nullableIndex = 0;
12167
for (FunctionalMethodType fmt : fmts) {
122-
fmtDescriptions.put(fmt, "");
12368
switch (fmt.itemIO()) {
12469
case INPUT:
125-
fmtNames.put(fmt, "input" + ins++);
126-
fmtNullability.put(fmt, paramNullability[nullableIndex++]);
70+
fmtNames.add("input" + ins++);
12771
break;
12872
case OUTPUT:
129-
fmtNames.put(fmt, "output" + outs++);
73+
fmtNames.add("output" + outs++);
13074
break;
13175
case CONTAINER:
132-
fmtNames.put(fmt, "container" + containers++);
76+
fmtNames.add("container" + containers++);
13377
break;
13478
case MUTABLE:
135-
fmtNames.put(fmt, "mutable" + mutables++);
79+
fmtNames.add("mutable" + mutables++);
13680
break;
13781
default:
13882
throw new RuntimeException("Unexpected ItemIO type encountered!");
13983
}
14084
}
141-
}
142-
143-
private static MethodParamInfo synthesizedMethodParamInfo(
144-
List<FunctionalMethodType> fmts, Boolean[] paramNullability)
145-
{
146-
Map<FunctionalMethodType, String> fmtNames = new HashMap<>(fmts.size());
147-
Map<FunctionalMethodType, String> fmtDescriptions = new HashMap<>(fmts
148-
.size());
149-
Map<FunctionalMethodType, Boolean> fmtNullability = new HashMap<>(fmts
150-
.size());
151-
152-
addSynthesizedMethodParamInfo(fmtNames, fmtDescriptions, fmts,
153-
fmtNullability, paramNullability);
154-
155-
return new MethodParamInfo(fmtNames, fmtDescriptions, fmtNullability);
85+
return fmtNames;
15686
}
15787

15888
@Override
15989
public List<SynthesizedParameterMember<?>> synthesizeMembers(
16090
List<FunctionalMethodType> fmts)
16191
{
162-
Producer<MethodParamInfo> p = //
163-
() -> LazilyGeneratedMethodParameterData.getInfo(fmts, m, opType);
164-
165-
return fmts.stream() //
166-
.map(fmt -> new SynthesizedParameterMember<>(fmt, p)) //
167-
.collect(Collectors.toList());
92+
Boolean[] optionality = getParameterNullability(m, opType, m.getParameterCount());
93+
List<String> names = getParameterNames(fmts);
94+
int p = 0;
95+
List<SynthesizedParameterMember<?>> members = new ArrayList<>(fmts.size());
96+
for (FunctionalMethodType fmt: fmts) {
97+
String name = names.get(p);
98+
boolean optional = fmt.itemIO() != ItemIO.OUTPUT && optionality[p++];
99+
members.add(new SynthesizedParameterMember<>(fmt, name, !optional, ""));
100+
}
101+
return members;
168102
}
169103

170104
private static Boolean[] getParameterNullability(Method m, Class<?> opType,

0 commit comments

Comments
 (0)