ASF Bugzilla – Attachment 27186 Details for
Bug 51400
Use of "new String(byte[] b, String enc)" hits Sun JVM bottleneck
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
Remember
[x]
|
Forgot Password
Login:
[x]
[patch]
Patch with optimizations
tomcat6032-stringenc.diff (text/plain), 4.79 KB, created by
Dave Engberg
on 2011-06-21 00:47:24 UTC
(
hide
)
Description:
Patch with optimizations
Filename:
MIME Type:
Creator:
Dave Engberg
Created:
2011-06-21 00:47:24 UTC
Size:
4.79 KB
patch
obsolete
>Index: java/org/apache/tomcat/util/buf/ByteChunk.java >=================================================================== >--- java/org/apache/tomcat/util/buf/ByteChunk.java (revision 1101105) >+++ java/org/apache/tomcat/util/buf/ByteChunk.java (working copy) >@@ -19,6 +19,7 @@ > > import java.io.IOException; > import java.io.Serializable; >+import java.nio.charset.Charset; > > /* > * In a server it is very important to be able to operate on >@@ -95,7 +96,12 @@ > 8859_1, and this object is used mostly for servlets. > */ > public static final String DEFAULT_CHARACTER_ENCODING="ISO-8859-1"; >- >+ >+ /** Default Charset to use for interpreting byte[] as as String >+ */ >+ public static final Charset DEFAULT_CHARSET = >+ Charset.forName(DEFAULT_CHARACTER_ENCODING); >+ > // byte[] > private byte[] buff; > >@@ -493,8 +499,13 @@ > public String toStringInternal() { > String strValue=null; > try { >- if( enc==null ) enc=DEFAULT_CHARACTER_ENCODING; >- strValue = new String( buff, start, end-start, enc ); >+ Charset charset; >+ if (enc == null) { >+ charset = DEFAULT_CHARSET; >+ } else { >+ charset = B2CConverter.getCharset(enc); >+ } >+ strValue = new String( buff, start, end-start, charset ); > /* > Does not improve the speed too much on most systems, > it's safer to use the "clasical" new String(). >@@ -507,7 +518,7 @@ > // Method is commented out, in: > return B2CConverter.decodeString( enc ); > */ >- } catch (java.io.UnsupportedEncodingException e) { >+ } catch (java.nio.charset.UnsupportedCharsetException e) { > // Use the platform encoding in that case; the usage of a bad > // encoding will have been logged elsewhere already > strValue = new String(buff, start, end-start); >Index: java/org/apache/tomcat/util/buf/B2CConverter.java >=================================================================== >--- java/org/apache/tomcat/util/buf/B2CConverter.java (revision 1101105) >+++ java/org/apache/tomcat/util/buf/B2CConverter.java (working copy) >@@ -22,6 +22,8 @@ > import java.io.InputStream; > import java.io.InputStreamReader; > import java.io.UnsupportedEncodingException; >+import java.nio.charset.Charset; >+import java.util.concurrent.ConcurrentHashMap; > > /** Efficient conversion of bytes to character . > * >@@ -39,6 +41,9 @@ > > private static org.apache.juli.logging.Log log= > org.apache.juli.logging.LogFactory.getLog( B2CConverter.class ); >+ >+ private static final ConcurrentHashMap<String, Charset> encodingToCharsetCache = >+ new ConcurrentHashMap<String, Charset>(); > > private IntermediateInputStream iis; > private ReadConvertor conv; >@@ -114,7 +119,7 @@ > { > // destroy the reader/iis > iis=new IntermediateInputStream(); >- conv=new ReadConvertor( iis, encoding ); >+ conv=new ReadConvertor( iis, getCharset(encoding) ); > } > > private final int debug=0; >@@ -123,6 +128,15 @@ > log.debug("B2CConverter: " + s ); > } > >+ public static Charset getCharset(String enc) { >+ Charset charset = encodingToCharsetCache.get(enc); >+ if (charset == null) { >+ charset = Charset.forName(enc); >+ encodingToCharsetCache.put(enc, charset); >+ } >+ return charset; >+ } >+ > // -------------------- Not used - the speed improvemnt is quite small > > /* >@@ -192,10 +206,9 @@ > > /** Create a converter. > */ >- public ReadConvertor( IntermediateInputStream in, String enc ) >- throws UnsupportedEncodingException >+ public ReadConvertor( IntermediateInputStream in, Charset charset ) > { >- super( in, enc ); >+ super( in, charset ); > } > > /** Overriden - will do nothing but reset internal state. >@@ -224,6 +237,7 @@ > } catch(IOException ioe){ > } > } >+ > } > > >Index: java/org/apache/catalina/connector/Request.java >=================================================================== >--- java/org/apache/catalina/connector/Request.java (revision 1101105) >+++ java/org/apache/catalina/connector/Request.java (working copy) >@@ -1516,12 +1516,16 @@ > > if (usingReader) > return; >- >- // Ensure that the specified encoding is valid >- byte buffer[] = new byte[1]; >- buffer[0] = (byte) 'a'; >- String dummy = new String(buffer, enc); > >+ // Confirm that the encoding name is valid >+ try { >+ B2CConverter.getCharset(enc); >+ } catch (java.nio.charset.UnsupportedCharsetException e) { >+ throw new UnsupportedEncodingException(e.getMessage()); >+ } catch (java.nio.charset.IllegalCharsetNameException e) { >+ throw new UnsupportedEncodingException(e.getMessage()); >+ } >+ > // Save the validated encoding > coyoteRequest.setCharacterEncoding(enc); >
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
Attachments on
bug 51400
:
27186
|
27189
|
27202
|
27211
|
27212
|
27213
|
27214
|
27219