Hi,

On Wed, May 8, 2019 at 7:16 PM Christopher Schultz <
ch...@christopherschultz.net> wrote:

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA256
>
> All,
>
> While working on the sort-file-listing enhancement over the weekend, I
> looked in the Tomcat code to see if we already had a method to split a
> string by a single character. The closest method I found was
>

Since few years java.lang.String#split(String) is optimized to not use
Regex when the following condition passes:

if (((regex.value.length == 1 &&
     ".$|()[{^?*+\\".indexOf(ch = regex.charAt(0)) == -1) ||
     (regex.length() == 2 &&
      regex.charAt(0) == '\\' &&
      (((ch = regex.charAt(1))-'0')|('9'-ch)) < 0 &&
      ((ch-'a')|('z'-ch)) < 0 &&
      ((ch-'A')|('Z'-ch)) < 0)) &&
    (ch < Character.MIN_HIGH_SURROGATE ||
     ch > Character.MAX_LOW_SURROGATE))
{

i.e. in many cases it is already fast enough.


> org.pache.jasper.compiler.JspUtil which splits on a string, so I
> rejected it as being a bit too general-purpose for my needs.
>
> But it got me looking into the implementation of the method, and it
> looks a little sketchy.
>
> First, it uses a java.util.Vector. Yep, a Vector. Remember those?
> Synchronized everything? The Vector is not used outside of the method
> at all -- it's just local and temporary. So, the obvious conclusion is
> that it should be changed to ArrayList, right?
>
> But, modern JVMs are fairly good at (a) processing contention-less
> locks more quickly and (b) identifying objects which cannot possibly
> have lock-contention and simply skipping the locking.
>
> What's the right move in these cases? Should we change Vector ->
> ArrayList and "help-out" the compiler and runtime, or should we leave
> the code alone because "it's fine"?
>
> Similar arguments can be made for using StringBuilder over
> StringBuffer, etc.
>
> WDYT?
>
> - -chris
> -----BEGIN PGP SIGNATURE-----
> Comment: Using GnuPG with Thunderbird - https://www.enigmail.net/
>
> iQIzBAEBCAAdFiEEMmKgYcQvxMe7tcJcHPApP6U8pFgFAlzTAMcACgkQHPApP6U8
> pFjEOQ//SpbOVCErJu8SCue+RagRp0Z0PsAwbquf2z4NuMsPZVST66fcjE7QY7aI
> 9whVdUBauxNiUCwMUOkgV8g8IGBB8KoWyhyIkTWCZuh+cwan6+5aKesE5UcUdYW5
> Xg93cqB4oGUWsR1YaEWEdycBObCTcXx/oHTq26/rXZvrGDHGU7rrGGOgTqO4TETU
> 6xvME86xTaImrBYIrnSUTo2S3QyPbqsoOOzEWinyW75cvR6xFw4iNNRfgzHIlb8f
> EIUjCqHasm4qAcxQ+t0DIoeVbWvcLpX49UMNyXP5Wt2frOjeTzpx4ErFrNXsAlQd
> JYoNgo50nAYNwxAsnBbZCrqSkaFO89I+nj7Tq9WDovOTF17CyMMaYtJiBe/ocTNv
> 25JmldlsM/wFY+ol9PQz9UmfzOHCcKGWEV4DcKaro6aTBzhELGYHpRh3z+ZNNuHD
> zkbJPRZFDZ4XC9dwRSfm0DUbIsYP2peBp3z7/1hWm+lnK6NIAZQQh0TzJ/cIliKw
> 6uWvCesERD1GyQosqoL0h9MHSckGpTSfbk+iI6MZtEwQc3AH4C27fm6MpfAc8YQU
> sr0jf2EdgzrC990jazPZcRbpwT8Eifc5kF6QgkbiockkqC/Zr28awL7vY2m5Szg9
> n2scZoWUAnkGM8k0x3sFM4DZpthjUGVe78ZsBnJkKX4Q5LAnAYQ=
> =U4Jd
> -----END PGP SIGNATURE-----
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
> For additional commands, e-mail: dev-h...@tomcat.apache.org
>
>

Reply via email to