Skip to content

Commit bf94b26

Browse files
committed
Perf
1 parent c35ebe8 commit bf94b26

File tree

12 files changed

+254
-89
lines changed

12 files changed

+254
-89
lines changed

src/main/java/com/networknt/schema/Schema.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,9 @@ public void validate(ExecutionContext executionContext, JsonNode jsonNode, JsonN
618618
}
619619
try {
620620
int currentErrors = executionContext.getErrors().size();
621-
for (KeywordValidator v : validators) {
621+
int size = validators.size();
622+
for (int x = 0; x < size; x++) {
623+
KeywordValidator v = validators.get(x);
622624
executionContext.evaluationPathAddLast(v.getKeyword());
623625
executionContext.evaluationSchemaPath.addLast(v.getKeyword());
624626
try {
@@ -633,7 +635,9 @@ public void validate(ExecutionContext executionContext, JsonNode jsonNode, JsonN
633635
// and all subschemas
634636
List<Annotation> annotations = executionContext.getAnnotations().asMap().get(instanceLocation);
635637
if (annotations != null) {
636-
for (Annotation annotation : annotations) {
638+
int annotationsSize = annotations.size();
639+
for (int x = 0; x < annotationsSize; x++) {
640+
Annotation annotation = annotations.get(x);
637641
if (annotation.getEvaluationPath().startsWith(executionContext.getEvaluationPath())) {
638642
annotation.setValid(false);
639643
}
@@ -1226,7 +1230,9 @@ private InputStreamSource getInputResource(AbsoluteIri input) {
12261230
InputStreamSource result = null;
12271231
List<ResourceLoader> resourceLoaders = this.getSchemaContext().getSchemaRegistry().getSchemaLoader()
12281232
.getResourceLoaders();
1229-
for (ResourceLoader loader : resourceLoaders) {
1233+
int size = resourceLoaders.size();
1234+
for (int x = 0; x < size; x++) {
1235+
ResourceLoader loader = resourceLoaders.get(x);
12301236
result = loader.getResource(input);
12311237
if (result != null) {
12321238
return result;
@@ -1565,7 +1571,9 @@ public void walk(ExecutionContext executionContext, JsonNode node, JsonNode root
15651571
}
15661572
try {
15671573
int currentErrors = executionContext.getErrors().size();
1568-
for (KeywordValidator validator : validators) {
1574+
int size = validators.size();
1575+
for (int x = 0; x < size; x++) {
1576+
KeywordValidator validator = validators.get(x);
15691577
try {
15701578
// Call all the pre-walk listeners. If at least one of the pre walk listeners
15711579
// returns SKIP, then skip the walk.
@@ -1625,7 +1633,10 @@ public void initializeValidators() {
16251633
*/
16261634
this.validatorsLoaded = true;
16271635
try {
1628-
for (final KeywordValidator validator : getValidators()) {
1636+
List<KeywordValidator> validators = getValidators();
1637+
int size = validators.size();
1638+
for (int x = 0; x < size; x++) {
1639+
KeywordValidator validator = validators.get(x);
16291640
validator.preloadSchema();
16301641
}
16311642
} catch (RuntimeException e) {

src/main/java/com/networknt/schema/keyword/AdditionalPropertiesValidator.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,9 @@ public void walk(ExecutionContext executionContext, JsonNode node, JsonNode root
172172
}
173173

174174
private boolean handledByPatternProperties(String pname) {
175-
for (RegularExpression pattern : this.patternProperties) {
175+
int size = this.patternProperties.size();
176+
for (int x = 0; x < size; x++) {
177+
RegularExpression pattern = this.patternProperties.get(x);
176178
if (pattern.matches(pname)) {
177179
return true;
178180
}

src/main/java/com/networknt/schema/keyword/AllOfValidator.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,9 @@ public void validate(ExecutionContext executionContext, JsonNode node, JsonNode
5959

6060
protected void validate(ExecutionContext executionContext, JsonNode node, JsonNode rootNode,
6161
NodePath instanceLocation, boolean walk) {
62-
int schemaIndex = 0;
63-
for (Schema schema : this.schemas) {
62+
int size = this.schemas.size();
63+
for (int schemaIndex = 0; schemaIndex < size; schemaIndex++) {
64+
Schema schema = this.schemas.get(schemaIndex);
6465
executionContext.evaluationPathAddLast(schemaIndex);
6566
try {
6667
if (!walk) {
@@ -71,7 +72,6 @@ protected void validate(ExecutionContext executionContext, JsonNode node, JsonNo
7172
} finally {
7273
executionContext.evaluationPathRemoveLast();
7374
}
74-
schemaIndex++;
7575
}
7676
}
7777

@@ -82,16 +82,16 @@ public void walk(ExecutionContext executionContext, JsonNode node, JsonNode root
8282
validate(executionContext, node, rootNode, instanceLocation, true);
8383
return;
8484
}
85-
int schemaIndex = 0;
86-
for (Schema schema : this.schemas) {
85+
int size = this.schemas.size();
86+
for (int schemaIndex = 0; schemaIndex < size; schemaIndex++) {
87+
Schema schema = this.schemas.get(schemaIndex);
8788
// Walk through the schema
8889
executionContext.evaluationPathAddLast(schemaIndex);
8990
try {
9091
schema.walk(executionContext, node, rootNode, instanceLocation, false);
9192
} finally {
9293
executionContext.evaluationPathRemoveLast();
9394
}
94-
schemaIndex++;
9595
}
9696
}
9797

src/main/java/com/networknt/schema/keyword/AnyOfValidator.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,10 @@ protected void validate(ExecutionContext executionContext, JsonNode node, JsonNo
7171
// Save flag as nested schema evaluation shouldn't trigger fail fast
7272
boolean failFast = executionContext.isFailFast();
7373
try {
74-
int schemaIndex = 0;
7574
executionContext.setFailFast(false);
76-
for (Schema schema : this.schemas) {
75+
int size = this.schemas.size();
76+
for (int schemaIndex = 0; schemaIndex < size; schemaIndex++) {
77+
Schema schema = this.schemas.get(schemaIndex);
7778
subSchemaErrors.clear(); // Reuse and clear for each run
7879
executionContext.evaluationPathAddLast(schemaIndex);
7980
try {
@@ -85,7 +86,6 @@ protected void validate(ExecutionContext executionContext, JsonNode node, JsonNo
8586
} finally {
8687
executionContext.evaluationPathRemoveLast();
8788
}
88-
schemaIndex++;
8989

9090
// check if any validation errors have occurred
9191
if (subSchemaErrors.isEmpty()) {
@@ -192,15 +192,15 @@ public void walk(ExecutionContext executionContext, JsonNode node, JsonNode root
192192
validate(executionContext, node, rootNode, instanceLocation, true);
193193
return;
194194
}
195-
int schemaIndex = 0;
196-
for (Schema schema : this.schemas) {
195+
int size = this.schemas.size();
196+
for (int schemaIndex = 0; schemaIndex < size; schemaIndex++) {
197+
Schema schema = this.schemas.get(schemaIndex);
197198
executionContext.evaluationPathAddLast(schemaIndex);
198199
try {
199200
schema.walk(executionContext, node, rootNode, instanceLocation, false);
200201
} finally {
201202
executionContext.evaluationPathRemoveLast();
202203
}
203-
schemaIndex++;
204204
}
205205
}
206206

src/main/java/com/networknt/schema/keyword/DependenciesValidator.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ public void validate(ExecutionContext executionContext, JsonNode node, JsonNode
7070
String pname = it.next();
7171
List<String> deps = propertyDeps.get(pname);
7272
if (deps != null && !deps.isEmpty()) {
73-
for (String field : deps) {
73+
int size = deps.size();
74+
for (int x = 0; x < size; x++) {
75+
String field = deps.get(x);
7476
if (node.get(field) == null) {
7577
executionContext.addError(error().instanceNode(node).property(pname).instanceLocation(instanceLocation)
7678
.evaluationPath(executionContext.getEvaluationPath()).locale(executionContext.getExecutionConfig().getLocale())

src/main/java/com/networknt/schema/keyword/EnumValidator.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,8 @@ protected ArrayNode processArrayNode(ArrayNode node) {
144144
return node;
145145
}
146146
ArrayNode a = node.deepCopy();
147-
for (int x = 0; x < a.size(); x++) {
147+
int size = a.size();
148+
for (int x = 0; x < size; x++) {
148149
JsonNode v = a.get(x);
149150
if (v.isNumber()) {
150151
v = processNumberNode(v);
@@ -161,7 +162,8 @@ protected ArrayNode processArrayNode(ArrayNode node) {
161162
* @return the node
162163
*/
163164
protected boolean hasNumber(ArrayNode node) {
164-
for (int x = 0; x < node.size(); x++) {
165+
int size = node.size();
166+
for (int x = 0; x < size; x++) {
165167
JsonNode v = node.get(x);
166168
if (v.isNumber()) {
167169
return true;

src/main/java/com/networknt/schema/keyword/IfValidator.java

Lines changed: 53 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.networknt.schema.Schema;
2323
import com.networknt.schema.SchemaLocation;
2424
import com.networknt.schema.path.NodePath;
25+
import com.networknt.schema.utils.Flag;
2526
import com.networknt.schema.SchemaContext;
2627

2728
import java.util.*;
@@ -30,37 +31,33 @@
3031
* {@link KeywordValidator} for if.
3132
*/
3233
public class IfValidator extends BaseKeywordValidator {
33-
private static final List<String> KEYWORDS = Arrays.asList("if", "then", "else");
34-
3534
private final Schema ifSchema;
3635
private final Schema thenSchema;
3736
private final Schema elseSchema;
3837

3938
public IfValidator(SchemaLocation schemaLocation, JsonNode schemaNode, Schema parentSchema, SchemaContext schemaContext) {
4039
super(KeywordType.IF_THEN_ELSE, schemaNode, schemaLocation, parentSchema, schemaContext);
41-
42-
Schema foundIfSchema = null;
43-
Schema foundThenSchema = null;
44-
Schema foundElseSchema = null;
45-
46-
for (final String keyword : KEYWORDS) {
47-
final JsonNode node = parentSchema.getSchemaNode().get(keyword);
48-
final SchemaLocation schemaLocationOfSchema = parentSchema.getSchemaLocation().append(keyword);
49-
if (keyword.equals("if")) {
50-
foundIfSchema = schemaContext.newSchema(schemaLocationOfSchema, node,
51-
parentSchema);
52-
} else if (keyword.equals("then") && node != null) {
53-
foundThenSchema = schemaContext.newSchema(schemaLocationOfSchema, node,
54-
parentSchema);
55-
} else if (keyword.equals("else") && node != null) {
56-
foundElseSchema = schemaContext.newSchema(schemaLocationOfSchema, node,
57-
parentSchema);
58-
}
40+
JsonNode node = parentSchema.getSchemaNode().get("if");
41+
if (node != null) {
42+
SchemaLocation schemaLocationOfSchema = parentSchema.getSchemaLocation().append("if");
43+
this.ifSchema = schemaContext.newSchema(schemaLocationOfSchema, node, parentSchema);
44+
} else {
45+
this.ifSchema = null;
46+
}
47+
node = parentSchema.getSchemaNode().get("then");
48+
if (node != null) {
49+
SchemaLocation schemaLocationOfSchema = parentSchema.getSchemaLocation().append("then");
50+
this.thenSchema = schemaContext.newSchema(schemaLocationOfSchema, node, parentSchema);
51+
} else {
52+
this.thenSchema = null;
53+
}
54+
node = parentSchema.getSchemaNode().get("else");
55+
if (node != null) {
56+
SchemaLocation schemaLocationOfSchema = parentSchema.getSchemaLocation().append("else");
57+
this.elseSchema = schemaContext.newSchema(schemaLocationOfSchema, node, parentSchema);
58+
} else {
59+
this.elseSchema = null;
5960
}
60-
61-
this.ifSchema = foundIfSchema;
62-
this.thenSchema = foundThenSchema;
63-
this.elseSchema = foundElseSchema;
6461
}
6562

6663
@Override
@@ -70,7 +67,7 @@ public void validate(ExecutionContext executionContext, JsonNode node, JsonNode
7067
// Save flag as nested schema evaluation shouldn't trigger fail fast
7168
boolean failFast = executionContext.isFailFast();
7269
List<Error> existingErrors = executionContext.getErrors();
73-
List<Error> test = new ArrayList<>();
70+
List<Error> test = new Flag<>();
7471
executionContext.setErrors(test);
7572
try {
7673
executionContext.setFailFast(false);
@@ -87,21 +84,22 @@ public void validate(ExecutionContext executionContext, JsonNode node, JsonNode
8784
// This removes the "if" in the evaluation path so the rest of the evaluation paths will be correct
8885
executionContext.evaluationPathRemoveLast();
8986
executionContext.evaluationPathAddLast("then");
90-
try {
87+
// For performance the "if" isn't replaced on the evaluation path
88+
// try {
9189
this.thenSchema.validate(executionContext, node, rootNode, instanceLocation);
92-
} finally {
93-
executionContext.evaluationPathRemoveLast();
94-
executionContext.evaluationPathAddLast("if");
95-
}
90+
// } finally {
91+
// executionContext.evaluationPathRemoveLast();
92+
// executionContext.evaluationPathAddLast("if");
93+
// }
9694
} else if (!ifConditionPassed && this.elseSchema != null) {
9795
executionContext.evaluationPathRemoveLast();
9896
executionContext.evaluationPathAddLast("else");
99-
try {
97+
// try {
10098
this.elseSchema.validate(executionContext, node, rootNode, instanceLocation);
101-
} finally {
102-
executionContext.evaluationPathRemoveLast();
103-
executionContext.evaluationPathAddLast("if");
104-
}
99+
// } finally {
100+
// executionContext.evaluationPathRemoveLast();
101+
// executionContext.evaluationPathAddLast("if");
102+
// }
105103
}
106104
}
107105

@@ -144,42 +142,42 @@ public void walk(ExecutionContext executionContext, JsonNode node, JsonNode root
144142
if (this.thenSchema != null) {
145143
executionContext.evaluationPathRemoveLast();
146144
executionContext.evaluationPathAddLast("then");
147-
try {
145+
// try {
148146
this.thenSchema.walk(executionContext, node, rootNode, instanceLocation, shouldValidateSchema);
149-
} finally {
150-
executionContext.evaluationPathRemoveLast();
151-
executionContext.evaluationPathAddLast("if");
152-
}
147+
// } finally {
148+
// executionContext.evaluationPathRemoveLast();
149+
// executionContext.evaluationPathAddLast("if");
150+
// }
153151
}
154152
if (this.elseSchema != null) {
155153
executionContext.evaluationPathRemoveLast();
156154
executionContext.evaluationPathAddLast("else");
157-
try {
155+
// try {
158156
this.elseSchema.walk(executionContext, node, rootNode, instanceLocation, shouldValidateSchema);
159-
} finally {
160-
executionContext.evaluationPathRemoveLast();
161-
executionContext.evaluationPathAddLast("if");
162-
}
157+
// } finally {
158+
// executionContext.evaluationPathRemoveLast();
159+
// executionContext.evaluationPathAddLast("if");
160+
// }
163161
}
164162
} else {
165163
if (this.thenSchema != null && ifConditionPassed) {
166164
executionContext.evaluationPathRemoveLast();
167165
executionContext.evaluationPathAddLast("then");
168-
try {
166+
// try {
169167
this.thenSchema.walk(executionContext, node, rootNode, instanceLocation, shouldValidateSchema);
170-
} finally {
171-
executionContext.evaluationPathRemoveLast();
172-
executionContext.evaluationPathAddLast("if");
173-
}
168+
// } finally {
169+
// executionContext.evaluationPathRemoveLast();
170+
// executionContext.evaluationPathAddLast("if");
171+
// }
174172
} else if (this.elseSchema != null && !ifConditionPassed) {
175173
executionContext.evaluationPathRemoveLast();
176174
executionContext.evaluationPathAddLast("else");
177-
try {
175+
// try {
178176
this.elseSchema.walk(executionContext, node, rootNode, instanceLocation, shouldValidateSchema);
179-
} finally {
180-
executionContext.evaluationPathRemoveLast();
181-
executionContext.evaluationPathAddLast("if");
182-
}
177+
// } finally {
178+
// executionContext.evaluationPathRemoveLast();
179+
// executionContext.evaluationPathAddLast("if");
180+
// }
183181
}
184182
}
185183
}

src/main/java/com/networknt/schema/keyword/OneOfValidator.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,11 @@ protected void validate(ExecutionContext executionContext, JsonNode node, JsonNo
7474
// Save flag as nested schema evaluation shouldn't trigger fail fast
7575
boolean failFast = executionContext.isFailFast();
7676
Boolean canShortCircuit = null;
77-
int schemaIndex = 0;
7877
try {
7978
executionContext.setFailFast(false);
80-
for (Schema schema : this.schemas) {
79+
int size = this.schemas.size();
80+
for (int schemaIndex = 0; schemaIndex < size; schemaIndex++) {
81+
Schema schema = this.schemas.get(schemaIndex);
8182
subSchemaErrors.clear();
8283
executionContext.evaluationPathAddLast(schemaIndex);
8384
try {
@@ -89,7 +90,6 @@ protected void validate(ExecutionContext executionContext, JsonNode node, JsonNo
8990
} finally {
9091
executionContext.evaluationPathRemoveLast();
9192
}
92-
schemaIndex++;
9393

9494
// check if any validation errors have occurred
9595
if (subSchemaErrors.isEmpty()) { // No new errors
@@ -240,15 +240,15 @@ public void walk(ExecutionContext executionContext, JsonNode node, JsonNode root
240240
if (shouldValidateSchema && node != null) {
241241
validate(executionContext, node, rootNode, instanceLocation, true);
242242
} else {
243-
int schemaIndex = 0;
244-
for (Schema schema : this.schemas) {
243+
int size = this.schemas.size();
244+
for (int schemaIndex = 0; schemaIndex < size; schemaIndex++) {
245+
Schema schema = this.schemas.get(schemaIndex);
245246
executionContext.evaluationPathAddLast(schemaIndex);
246247
try {
247248
schema.walk(executionContext, node, rootNode, instanceLocation, false);
248249
} finally {
249250
executionContext.evaluationPathRemoveLast();
250251
}
251-
schemaIndex++;
252252
}
253253
}
254254
}

0 commit comments

Comments
 (0)