Skip to content

Commit f5382c9

Browse files
LuisOsvdiemol
andauthored
Caching the size/length in loops to slightly improve performance (SeleniumHQ#15852)
caching the size/length in loops to slightly improve performance and readability Co-authored-by: Diego Molina <[email protected]>
1 parent 2fefb7b commit f5382c9

File tree

9 files changed

+25
-15
lines changed

9 files changed

+25
-15
lines changed

java/src/dev/selenium/tools/javadoc/JavadocJarMaker.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ public static void main(String[] args) throws IOException {
5555
Path out = null;
5656
Set<Path> classpath = new HashSet<>();
5757

58-
for (int i = 0; i < args.length; i++) {
58+
int argCount = args.length;
59+
for (int i = 0; i < argCount; i++) {
5960
String flag = args[i];
6061
String next = args[++i];
6162

java/src/dev/selenium/tools/modules/ModuleGenerator.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,8 @@ public static void main(String[] args) throws IOException {
108108
Set<String> openTo = new TreeSet<>();
109109
boolean isOpen = false;
110110

111-
for (int i = 0; i < args.length; i++) {
111+
int argCount = args.length;
112+
for (int i = 0; i < argCount; i++) {
112113
String flag = args[i];
113114
String next = args[++i];
114115
switch (flag) {

java/src/org/openqa/selenium/grid/node/docker/DockerOptions.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,11 @@ public Map<Capabilities, Collection<SessionFactory>> getDockerSessionFactories(
140140
config.getAll(DOCKER_SECTION, "host-config-keys").orElseGet(Collections::emptyList);
141141

142142
Multimap<String, Capabilities> kinds = HashMultimap.create();
143-
for (int i = 0; i < allConfigs.size(); i++) {
143+
int configsCount = allConfigs.size();
144+
for (int i = 0; i < configsCount; i++) {
144145
String imageName = allConfigs.get(i);
145146
i++;
146-
if (i == allConfigs.size()) {
147+
if (i == configsCount) {
147148
throw new DockerException("Unable to find JSON config");
148149
}
149150
Capabilities stereotype =

java/src/org/openqa/selenium/grid/node/relay/RelayOptions.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,15 +153,16 @@ public Map<Capabilities, Collection<SessionFactory>> getSessionFactories(
153153
() -> new ConfigException("Unable to find configs for " + getServiceUri()));
154154

155155
Multimap<Integer, Capabilities> parsedConfigs = HashMultimap.create();
156-
for (int i = 0; i < allConfigs.size(); i++) {
156+
int configsCount = allConfigs.size();
157+
for (int i = 0; i < configsCount; i++) {
157158
int maxSessions;
158159
try {
159160
maxSessions = Integer.parseInt(extractConfiguredValue(allConfigs.get(i)));
160161
} catch (NumberFormatException e) {
161162
throw new ConfigException("Unable parse value as number. " + allConfigs.get(i));
162163
}
163164
i++;
164-
if (i == allConfigs.size()) {
165+
if (i == configsCount) {
165166
throw new ConfigException("Unable to find stereotype config. " + allConfigs);
166167
}
167168
Capabilities stereotype =

java/src/org/openqa/selenium/json/JsonInput.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,8 @@ private void expect(JsonType type) {
507507
private <X> X read(String toCompare, Function<String, X> mapper) {
508508
skipWhitespace(input);
509509

510-
for (int i = 0; i < toCompare.length(); i++) {
510+
int toCompareLength = toCompare.length();
511+
for (int i = 0; i < toCompareLength; i++) {
511512
char read = input.read();
512513
if (read != toCompare.charAt(i)) {
513514
throw new JsonException(

java/src/org/openqa/selenium/remote/WebElementToJsonConverter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ public Object apply(Object arg) {
8585

8686
private static List<Object> arrayToList(Object array) {
8787
List<Object> list = new ArrayList<>();
88-
for (int i = 0; i < Array.getLength(array); i++) {
88+
int arrayLength = Array.getLength(array);
89+
for (int i = 0; i < arrayLength; i++) {
8990
list.add(Array.get(array, i));
9091
}
9192
return list;

java/src/org/openqa/selenium/remote/codec/AbstractHttpCommandCodec.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -387,11 +387,12 @@ boolean isFor(HttpMethod method, List<String> parts) {
387387
return false;
388388
}
389389

390-
if (parts.size() != this.pathSegments.size()) {
390+
int partsCount = parts.size();
391+
if (partsCount != this.pathSegments.size()) {
391392
return false;
392393
}
393394

394-
for (int i = 0; i < parts.size(); ++i) {
395+
for (int i = 0; i < partsCount; ++i) {
395396
String reqPart = parts.get(i);
396397
String specPart = pathSegments.get(i);
397398
if (!(specPart.startsWith(":") || specPart.equals(reqPart))) {
@@ -403,7 +404,8 @@ boolean isFor(HttpMethod method, List<String> parts) {
403404
}
404405

405406
void parsePathParameters(List<String> parts, Map<String, Object> parameters) {
406-
for (int i = 0; i < parts.size(); ++i) {
407+
int partsCount = parts.size();
408+
for (int i = 0; i < partsCount; ++i) {
407409
if (pathSegments.get(i).startsWith(":")) {
408410
parameters.put(pathSegments.get(i).substring(1), parts.get(i));
409411
}

java/src/org/openqa/selenium/remote/http/Route.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,10 +254,11 @@ public boolean matches(HttpRequest request) {
254254

255255
private boolean hasPrefix(HttpRequest request) {
256256
String[] parts = request.getUri().split("/");
257-
if (parts.length < prefixPaths.length) {
257+
int prefixPathCount = prefixPaths.length;
258+
if (parts.length < prefixPathCount) {
258259
return false;
259260
}
260-
for (int i = 0; i < prefixPaths.length; i++) {
261+
for (int i = 0; i < prefixPathCount; i++) {
261262
if (!prefixPaths[i].equals(parts[i])) {
262263
return false;
263264
}

java/src/org/openqa/selenium/remote/http/UrlTemplate.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ public UrlTemplate(String template) {
6969

7070
List<String> allGroups = List.copyOf(groups);
7171
// do we hit a fast path?
72-
switch (allGroups.size()) {
72+
int groupsCount = allGroups.size();
73+
switch (groupsCount) {
7374
case 0: // no groups, just .equals
7475
this.compiled =
7576
(matchAgainst) -> {
@@ -119,7 +120,7 @@ public UrlTemplate(String template) {
119120
}
120121

121122
Map<String, String> params = new LinkedHashMap<>();
122-
for (int i = 0; i < allGroups.size(); i++) {
123+
for (int i = 0; i < groupsCount; i++) {
123124
params.put(allGroups.get(i), matcher.group(i + 1));
124125
}
125126

0 commit comments

Comments
 (0)