Useless long->string->long conversion

Mkrtchyan, Tigran tigran.mkrtchyan at desy.de
Fri Apr 19 08:38:09 UTC 2024


Dear JDK Security-devs,

Recently, during profiling, I noticed that UnixNumericUserPrincipal has
useless conversion between long and string:

```
    private String name;

    public UnixNumericUserPrincipal(long name) {
        this.name = Long.toString(name);
    }

    /**
     * Return the user identification number (UID) for this
     * {@code UnixNumericUserPrincipal} as a long.
     *
     * @return the user identification number (UID) for this
     *          {@code UnixNumericUserPrincipal} as a long.
     */
    public long longValue() {
        return Long.parseLong(name);
    }

```

My naive assumption is, that if one uses UnixNumericXxxPrincipal,
then with a high probability, `longValue` will be called, thus
converting long to a string and back is not that efficient (which
is visible in the profiler). The same is true for
UnixNumericGroupPrincipal too.

Unfortunately, the presence of `readObject`method makes modifications
to the class not trivial.

Best regards,
   Tigran.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 2826 bytes
Desc: S/MIME Cryptographic Signature
URL: <https://mail.openjdk.org/pipermail/security-dev/attachments/20240419/0186925f/smime.p7s>


More information about the security-dev mailing list