Replace TypeParameterFinder with TypeParameterMatcher

- We can avoid reflective matching using byte code generation.
 - Better matching performance when message type is Object
This commit is contained in:
Trustin Lee 2013-02-08 18:21:07 +09:00
parent ee189d1da7
commit ff5aec0c78
5 changed files with 63 additions and 36 deletions

View File

@ -22,12 +22,12 @@ import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundByteHandler;
import io.netty.channel.ChannelOutboundMessageHandler;
import io.netty.channel.ChannelPromise;
import io.netty.util.internal.TypeParameterFinder;
import io.netty.util.internal.TypeParameterMatcher;
public abstract class ByteToMessageCodec<I> extends ChannelDuplexHandler
implements ChannelInboundByteHandler, ChannelOutboundMessageHandler<I> {
private final Class<?> acceptedOutboundMsgType;
private final TypeParameterMatcher outboundMsgMatcher;
private final MessageToByteEncoder<I> encoder = new MessageToByteEncoder<I>() {
@Override
public boolean acceptOutboundMessage(Object msg) throws Exception {
@ -60,8 +60,7 @@ public abstract class ByteToMessageCodec<I> extends ChannelDuplexHandler
@SuppressWarnings("rawtypes")
Class<? extends ByteToMessageCodec> parameterizedHandlerType,
int messageTypeParamIndex) {
acceptedOutboundMsgType =
TypeParameterFinder.findActualTypeParameter(this, parameterizedHandlerType, messageTypeParamIndex);
outboundMsgMatcher = TypeParameterMatcher.find(this, parameterizedHandlerType, messageTypeParamIndex);
}
@Override
@ -105,7 +104,7 @@ public abstract class ByteToMessageCodec<I> extends ChannelDuplexHandler
}
public boolean acceptOutboundMessage(Object msg) throws Exception {
return acceptedOutboundMsgType.isInstance(msg);
return outboundMsgMatcher.match(msg);
}
protected abstract void encode(ChannelHandlerContext ctx, I msg, ByteBuf out) throws Exception;

View File

@ -24,7 +24,7 @@ import io.netty.channel.ChannelInboundMessageHandlerAdapter;
import io.netty.channel.ChannelOutboundMessageHandler;
import io.netty.channel.ChannelOutboundMessageHandlerAdapter;
import io.netty.channel.ChannelPromise;
import io.netty.util.internal.TypeParameterFinder;
import io.netty.util.internal.TypeParameterMatcher;
/**
* A Codec for on-the-fly encoding/decoding of message.
@ -97,12 +97,12 @@ public abstract class MessageToMessageCodec<INBOUND_IN, OUTBOUND_IN>
}
};
private final Class<?> acceptedInboundMsgType;
private final Class<?> acceptedOutboundMsgType;
private final TypeParameterMatcher inboundMsgMatcher;
private final TypeParameterMatcher outboundMsgMatcher;
protected MessageToMessageCodec() {
acceptedInboundMsgType = TypeParameterFinder.findActualTypeParameter(this, MessageToMessageCodec.class, 0);
acceptedOutboundMsgType = TypeParameterFinder.findActualTypeParameter(this, MessageToMessageCodec.class, 1);
inboundMsgMatcher = TypeParameterMatcher.find(this, MessageToMessageCodec.class, 0);
outboundMsgMatcher = TypeParameterMatcher.find(this, MessageToMessageCodec.class, 1);
}
protected MessageToMessageCodec(
@ -113,9 +113,9 @@ public abstract class MessageToMessageCodec<INBOUND_IN, OUTBOUND_IN>
Class<? extends ChannelOutboundMessageHandlerAdapter> parameterizedOutboundHandlerType,
int outboundMessageTypeParamIndex) {
acceptedInboundMsgType = TypeParameterFinder.findActualTypeParameter(
inboundMsgMatcher = TypeParameterMatcher.find(
this, parameterizedInboundHandlerType, inboundMessageTypeParamIndex);
acceptedOutboundMsgType = TypeParameterFinder.findActualTypeParameter(
outboundMsgMatcher = TypeParameterMatcher.find(
this, parameterizedOutboundHandlerType, outboundMessageTypeParamIndex);
}
@ -158,7 +158,7 @@ public abstract class MessageToMessageCodec<INBOUND_IN, OUTBOUND_IN>
* @param msg the message
*/
public boolean acceptInboundMessage(Object msg) throws Exception {
return acceptedInboundMsgType.isInstance(msg);
return inboundMsgMatcher.match(msg);
}
/**
@ -167,7 +167,7 @@ public abstract class MessageToMessageCodec<INBOUND_IN, OUTBOUND_IN>
* @param msg the message
*/
public boolean acceptOutboundMessage(Object msg) throws Exception {
return acceptedOutboundMsgType.isInstance(msg);
return outboundMsgMatcher.match(msg);
}
protected abstract Object encode(ChannelHandlerContext ctx, OUTBOUND_IN msg) throws Exception;

View File

@ -21,21 +21,31 @@ import java.lang.reflect.Type;
import java.util.IdentityHashMap;
import java.util.Map;
public final class TypeParameterFinder {
public abstract class TypeParameterMatcher {
private static final ThreadLocal<Map<Class<?>, Class<?>>> typeMap = new ThreadLocal<Map<Class<?>, Class<?>>>() {
private static final TypeParameterMatcher NOOP = new TypeParameterMatcher() {
@Override
protected Map<Class<?>, Class<?>> initialValue() {
return new IdentityHashMap<Class<?>, Class<?>>();
public boolean match(Object msg) {
return true;
}
};
public static Class<?> findActualTypeParameter(
private static final ThreadLocal<Map<Class<?>, TypeParameterMatcher>> typeMap =
new ThreadLocal<Map<Class<?>, TypeParameterMatcher>>() {
@Override
protected Map<Class<?>, TypeParameterMatcher> initialValue() {
return new IdentityHashMap<Class<?>, TypeParameterMatcher>();
}
};
public static TypeParameterMatcher find(
final Object object, final Class<?> parameterizedSuperClass, final int typeParamIndex) {
final Map<Class<?>, Class<?>> typeMap = TypeParameterFinder.typeMap.get();
final Map<Class<?>, TypeParameterMatcher> typeMap = TypeParameterMatcher.typeMap.get();
final Class<?> thisClass = object.getClass();
Class<?> messageType = typeMap.get(thisClass);
if (messageType == null) {
TypeParameterMatcher matcher = typeMap.get(thisClass);
if (matcher == null) {
Class<?> currentClass = thisClass;
for (;;) {
if (currentClass.getSuperclass() == parameterizedSuperClass) {
@ -45,17 +55,37 @@ public final class TypeParameterFinder {
"cannot determine the type of the type parameter of " + thisClass.getSimpleName());
}
messageType = (Class<?>) types[0];
Class<?> messageType = (Class<?>) types[0];
if (messageType == Object.class) {
matcher = NOOP;
} else {
matcher = new ReflectiveMatcher(messageType);
}
break;
}
currentClass = currentClass.getSuperclass();
}
typeMap.put(thisClass, messageType);
typeMap.put(thisClass, matcher);
}
return messageType;
return matcher;
}
private TypeParameterFinder() { }
public abstract boolean match(Object msg);
private static final class ReflectiveMatcher extends TypeParameterMatcher {
private final Class<?> type;
ReflectiveMatcher(Class<?> type) {
this.type = type;
}
@Override
public boolean match(Object msg) {
return type.isInstance(msg);
}
}
TypeParameterMatcher() { }
}

View File

@ -17,7 +17,7 @@ package io.netty.channel;
import io.netty.buffer.MessageBuf;
import io.netty.buffer.Unpooled;
import io.netty.util.internal.TypeParameterFinder;
import io.netty.util.internal.TypeParameterMatcher;
/**
* {@link ChannelHandler} which handles inbound messages of a specific type.
@ -44,7 +44,7 @@ import io.netty.util.internal.TypeParameterFinder;
public abstract class ChannelInboundMessageHandlerAdapter<I>
extends ChannelStateHandlerAdapter implements ChannelInboundMessageHandler<I> {
private final Class<?> acceptedMsgType;
private final TypeParameterMatcher msgMatcher;
protected ChannelInboundMessageHandlerAdapter() {
this(ChannelInboundMessageHandlerAdapter.class, 0);
@ -54,8 +54,7 @@ public abstract class ChannelInboundMessageHandlerAdapter<I>
@SuppressWarnings("rawtypes")
Class<? extends ChannelInboundMessageHandlerAdapter> parameterizedHandlerType,
int messageTypeParamIndex) {
acceptedMsgType = TypeParameterFinder.findActualTypeParameter(
this, parameterizedHandlerType, messageTypeParamIndex);
msgMatcher = TypeParameterMatcher.find(this, parameterizedHandlerType, messageTypeParamIndex);
}
@Override
@ -126,7 +125,7 @@ public abstract class ChannelInboundMessageHandlerAdapter<I>
* @param msg the message
*/
public boolean acceptInboundMessage(Object msg) throws Exception {
return acceptedMsgType.isInstance(msg);
return msgMatcher.match(msg);
}
/**

View File

@ -17,7 +17,7 @@ package io.netty.channel;
import io.netty.buffer.MessageBuf;
import io.netty.buffer.Unpooled;
import io.netty.util.internal.TypeParameterFinder;
import io.netty.util.internal.TypeParameterMatcher;
/**
* Abstract base class which handles messages of a specific type.
@ -27,7 +27,7 @@ import io.netty.util.internal.TypeParameterFinder;
public abstract class ChannelOutboundMessageHandlerAdapter<I>
extends ChannelOperationHandlerAdapter implements ChannelOutboundMessageHandler<I> {
private final Class<?> acceptedMsgType;
private final TypeParameterMatcher msgMatcher;
protected ChannelOutboundMessageHandlerAdapter() {
this(ChannelOutboundMessageHandlerAdapter.class, 0);
@ -37,8 +37,7 @@ public abstract class ChannelOutboundMessageHandlerAdapter<I>
@SuppressWarnings("rawtypes")
Class<? extends ChannelOutboundMessageHandlerAdapter> parameterizedHandlerType,
int messageTypeParamIndex) {
acceptedMsgType = TypeParameterFinder.findActualTypeParameter(
this, parameterizedHandlerType, messageTypeParamIndex);
msgMatcher = TypeParameterMatcher.find(this, parameterizedHandlerType, messageTypeParamIndex);
}
@Override
@ -57,7 +56,7 @@ public abstract class ChannelOutboundMessageHandlerAdapter<I>
* @param msg the message
*/
public boolean acceptOutboundMessage(Object msg) throws Exception {
return acceptedMsgType.isInstance(msg);
return msgMatcher.match(msg);
}
@Override