Useless long->string->long conversion

Sean Mullan sean.mullan at oracle.com
Tue Apr 23 17:15:29 UTC 2024


It is probably fixable by storing the long as a transient field and 
overriding writeObject and computing the String as needed in other 
methods, but I am somewhat skeptical if it is worth it as it would make 
the implementation a bit more complex.

--Sean

On 4/19/24 4:38 AM, Mkrtchyan, Tigran wrote:
> 
> 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.



More information about the security-dev mailing list