On Mon, 26 Apr 2021 11:24:23 GMT, Сергей Цыпанов <github.com+10835776+stsypa...@openjdk.org> wrote:
>> That should be fine, the null check in Objects.equals is benign with these >> usages. > > One more thing I'm thinking about (not to be done in this PR of course) is to > move call to `String.intern()` from where it is now in > `Class.getPackageName()` > > public String getPackageName() { > String pn = this.packageName; > if (pn == null) { > Class<?> c = this; > while (c.isArray()) { > c = c.getComponentType(); > } > if (c.isPrimitive()) { > pn = "java.lang"; > } else { > String cn = c.getName(); > int dot = cn.lastIndexOf('.'); > pn = (dot != -1) ? cn.substring(0, dot).intern() : ""; // <--- > } > this.packageName = pn; > } > return pn; > } > > to `packageName` field assignement like > > this.packageName = pn.intern(); > > this would add two more Strings (`""` and `"java.lang"`) into string table > and allow to avoid `String.equals()` in favour of `==` call when comparing > package names. What do you think? String literals are implicitly interned: System.out.println("" == new String("")); // false System.out.println("" == new String("").intern()); // true System.out.println("java.lang" == new String("java.lang")); // false System.out.println("java.lang" == new String("java.lang").intern()); // true So we could already use `==` instead of `equals` when evaluating the result of `getPackageName`, which might be a good optimization in some places (`ReflectionFactory::isSamePackage` shows up in profiles of some reflection-related benchmarks). ------------- PR: https://git.openjdk.java.net/jdk/pull/3571