Compare a Path to a string
Weijun Wang
weijun.wang at oracle.com
Tue Mar 21 14:47:14 UTC 2017
I'm profiling the new FilePermission implementation and found a lot of
time spending on comparing a Path to a string (in fact, toString()), and
in every case the path has only one element.
Here are them:
1. Comparing with "-":
Path lastName = npath.getFileName();
if (lastName != null && lastName.toString().equals("-")) { ... }
2. Comparing with "":
p = p2.relativize(p1).normalize();
if (p.getName(0).toString().isEmpty()) { ... }
3. Comparing with "..":
for (Path item: p) {
String s = item.toString();
if (!s.equals("..")) { ... }
}
Maybe it's better to compare them with static final Path fields? I
create some benchmarks and they show comparing with objects is better
than comparing with strings, the throughput is roughly 40% higher.
I am using linux-x64.
So I made one enhancement to UnixPath.<init>(fs, str) to store
normalizeAndCheck(str) (it is calculated anyway) as stringValue (which
will be returned by toString) in the constructor, and seems comparing
with objects and comparing with strings have no significant difference now.
I admit I haven't analyzed if normalizeAndCheck(str) is always the
correct toString() result.
Is there a better way?
Thanks
Max
More information about the nio-dev
mailing list