Skip to content

Commit

Permalink
Use a static cache of B2CConverter objects rather than a per request …
Browse files Browse the repository at this point in the history
…cache that gets cleared every time the request switches to async mode.

git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@1701120 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
markt-asf committed Sep 3, 2015
1 parent 0305c21 commit 83fb92b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 39 deletions.
9 changes: 0 additions & 9 deletions java/org/apache/catalina/connector/CoyoteAdapter.java
Expand Up @@ -304,10 +304,6 @@ public boolean asyncDispatch(org.apache.coyote.Request req,
if (!success || !request.isAsync()) {
request.recycle();
response.recycle();
} else {
// Clear converters so that the minimum amount of memory
// is used by this processor
request.clearEncoders();
}
}
return success;
Expand Down Expand Up @@ -404,13 +400,8 @@ public void service(org.apache.coyote.Request req,
if (!async || error.get()) {
request.recycle();
response.recycle();
} else {
// Clear converters so that the minimum amount of memory
// is used by this processor
request.clearEncoders();
}
}

}


Expand Down
40 changes: 17 additions & 23 deletions java/org/apache/catalina/connector/InputBuffer.java
Expand Up @@ -33,6 +33,7 @@
import org.apache.tomcat.util.buf.B2CConverter;
import org.apache.tomcat.util.buf.ByteChunk;
import org.apache.tomcat.util.buf.CharChunk;
import org.apache.tomcat.util.collections.SynchronizedStack;
import org.apache.tomcat.util.res.StringManager;

/**
Expand All @@ -52,9 +53,6 @@ public class InputBuffer extends Reader
*/
protected static final StringManager sm = StringManager.getManager(InputBuffer.class);


// -------------------------------------------------------------- Constants

public static final int DEFAULT_BUFFER_SIZE = 8*1024;

// The buffer can be used for byte[] and char[] reading
Expand All @@ -64,6 +62,13 @@ public class InputBuffer extends Reader
public final int BYTE_STATE = 2;


/**
* Encoder cache.
*/
private static final ConcurrentHashMap<Charset,SynchronizedStack<B2CConverter>> encoders =
new ConcurrentHashMap<>();


// ----------------------------------------------------- Instance Variables

/**
Expand Down Expand Up @@ -96,12 +101,6 @@ public class InputBuffer extends Reader
private String enc;


/**
* List of encoders.
*/
protected final ConcurrentHashMap<Charset,B2CConverter> encoders = new ConcurrentHashMap<>();


/**
* Current byte to char converter.
*/
Expand Down Expand Up @@ -197,21 +196,14 @@ public void recycle() {

if (conv != null) {
conv.recycle();
encoders.get(conv.getCharset()).push(conv);
conv = null;
}

enc = null;
}


/**
* Clear cached encoders (to save memory for async requests).
*/
public void clearEncoders() {
encoders.clear();
}


/**
* Close the input buffer.
*
Expand Down Expand Up @@ -370,9 +362,7 @@ public void setEncoding(String s) {

@Override
public int realReadChars() throws IOException {
if (conv == null) {
setConverter();
}
checkConverter();

boolean eof = false;

Expand Down Expand Up @@ -545,7 +535,6 @@ public void checkConverter() throws IOException {


private void setConverter() throws IOException {

if (coyoteRequest != null) {
enc = coyoteRequest.getCharacterEncoding();
}
Expand All @@ -555,11 +544,16 @@ private void setConverter() throws IOException {
}

Charset charset = B2CConverter.getCharset(enc);
conv = encoders.get(charset);
SynchronizedStack<B2CConverter> stack = encoders.get(charset);
if (stack == null) {
stack = new SynchronizedStack<>();
encoders.putIfAbsent(charset, stack);
stack = encoders.get(charset);
}
conv = stack.pop();

if (conv == null) {
conv = createConverter(charset);
encoders.put(charset, conv);
}
}

Expand Down
7 changes: 0 additions & 7 deletions java/org/apache/catalina/connector/Request.java
Expand Up @@ -517,13 +517,6 @@ public void recycle() {
pathParameters.clear();
}

/**
* Clear cached encoders (to save memory for async requests).
*/
public void clearEncoders() {
inputBuffer.clearEncoders();
}


// -------------------------------------------------------- Request Methods

Expand Down
5 changes: 5 additions & 0 deletions java/org/apache/tomcat/util/buf/B2CConverter.java
Expand Up @@ -198,4 +198,9 @@ public void convert(ByteChunk bc, CharChunk cc, boolean endOfInput)
}
}
}


public Charset getCharset() {
return decoder.charset();
}
}

0 comments on commit 83fb92b

Please sign in to comment.