On Thu, 27 May 2021 10:08:17 GMT, Claes Redestad <[email protected]> wrote:
>> 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).
Done!
-------------
PR: https://git.openjdk.java.net/jdk/pull/3571