Improve AioChannelFinder implementations

- Use copy-on-write map
- Fix a potential bug where the old implementation assumed that one 
  Runnable type always wraps the same Runnable
- Cache offset value instead of Field in UnsafeAioChannelFinder
This commit is contained in:
Trustin Lee 2012-08-28 16:57:45 +09:00
parent 17ecbdd804
commit 709b3abd83
2 changed files with 83 additions and 56 deletions

View File

@ -1,79 +1,59 @@
package io.netty.channel.socket.aio; package io.netty.channel.socket.aio;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.ArrayDeque; import java.util.HashMap;
import java.util.Deque; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
class DefaultAioChannelFinder implements AioChannelFinder { class DefaultAioChannelFinder implements AioChannelFinder {
private static final ConcurrentMap<Class<?>, Field[]> fieldCache = new ConcurrentHashMap<Class<?>, Field[]>(); private static volatile Map<Class<?>, Field> fieldCache = new HashMap<Class<?>, Field>();
private static final Field[] FAILURE = new Field[0];
@Override @Override
public AbstractAioChannel findChannel(Runnable command) throws Exception { public AbstractAioChannel findChannel(Runnable command) throws Exception {
Class<?> commandType = command.getClass(); Field f;
Field[] fields = fieldCache.get(commandType); for (;;) {
if (fields == null) { f = findField(command);
try { if (f == null) {
fields = findFieldSequence(command, new ArrayDeque<Field>(2)); return null;
} catch (Throwable t) {
// Failed to get the field list
} }
Object next = f.get(command);
if (fields == null) { if (next instanceof AbstractAioChannel) {
fields = FAILURE; return (AbstractAioChannel) next;
} }
command = (Runnable) next;
fieldCache.put(commandType, fields); // No need to use putIfAbsent()
} }
if (fields == FAILURE) {
return null;
}
final int lastIndex = fields.length - 1;
for (int i = 0; i < lastIndex; i ++) {
command = (Runnable) get(fields[i], command);
}
return (AbstractAioChannel) get(fields[lastIndex], command);
} }
private Field[] findFieldSequence(Runnable command, Deque<Field> fields) throws Exception { private static Field findField(Object command) throws Exception {
Map<Class<?>, Field> fieldCache = DefaultAioChannelFinder.fieldCache;
Class<?> commandType = command.getClass(); Class<?> commandType = command.getClass();
Field res = fieldCache.get(commandType);
if (res != null) {
return res;
}
for (Field f: commandType.getDeclaredFields()) { for (Field f: commandType.getDeclaredFields()) {
if (f.getType() == Runnable.class) { if (f.getType() == Runnable.class) {
f.setAccessible(true); f.setAccessible(true);
fields.addLast(f); put(fieldCache, commandType, f);
try { return f;
Field[] ret = findFieldSequence((Runnable) get(f, command), fields);
if (ret != null) {
return ret;
}
} finally {
fields.removeLast();
}
} }
if (f.getType() == Object.class) { if (f.getType() == Object.class) {
f.setAccessible(true); f.setAccessible(true);
fields.addLast(f); Object candidate = f.get(command);
try { if (candidate instanceof AbstractAioChannel) {
Object candidate = get(f, command); put(fieldCache, commandType, f);
if (candidate instanceof AbstractAioChannel) { return f;
return fields.toArray(new Field[fields.size()]);
}
} finally {
fields.removeLast();
} }
} }
} }
return null; return null;
} }
protected Object get(Field f, Object command) throws Exception { private static void put(Map<Class<?>, Field> oldCache, Class<?> key, Field value) {
return f.get(command); Map<Class<?>, Field> newCache = new HashMap<Class<?>, Field>(oldCache.size());
newCache.putAll(oldCache);
newCache.put(key, value);
fieldCache = newCache;
} }
} }

View File

@ -1,19 +1,66 @@
package io.netty.channel.socket.aio; package io.netty.channel.socket.aio;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import sun.misc.Unsafe; import sun.misc.Unsafe;
@SuppressWarnings("restriction") @SuppressWarnings("restriction")
class UnsafeAioChannelFinder extends DefaultAioChannelFinder { class UnsafeAioChannelFinder implements AioChannelFinder {
private static final Unsafe UNSAFE = getUnsafe(); private static final Unsafe UNSAFE = getUnsafe();
private static volatile Map<Class<?>, Long> offsetCache = new HashMap<Class<?>, Long>();
@Override @Override
protected Object get(Field f, Object command) throws Exception { public AbstractAioChannel findChannel(Runnable command) throws Exception {
// using Unsafe to directly access the field. This should be Long offset;
// faster then "pure" reflection for (;;) {
long offset = UNSAFE.objectFieldOffset(f); offset = findField(command);
return UNSAFE.getObject(command, offset); if (offset == null) {
return null;
}
Object next = UNSAFE.getObject(command, offset);
if (next instanceof AbstractAioChannel) {
return (AbstractAioChannel) next;
}
command = (Runnable) next;
}
}
private static Long findField(Object command) throws Exception {
Map<Class<?>, Long> offsetCache = UnsafeAioChannelFinder.offsetCache;
Class<?> commandType = command.getClass();
Long res = offsetCache.get(commandType);
if (res != null) {
return res;
}
for (Field f: commandType.getDeclaredFields()) {
if (f.getType() == Runnable.class) {
res = UNSAFE.objectFieldOffset(f);
put(offsetCache, commandType, res);
return res;
}
if (f.getType() == Object.class) {
f.setAccessible(true);
Object candidate = f.get(command);
if (candidate instanceof AbstractAioChannel) {
res = UNSAFE.objectFieldOffset(f);
put(offsetCache, commandType, res);
return res;
}
}
}
return null;
}
private static void put(Map<Class<?>, Long> oldCache, Class<?> key, Long value) {
Map<Class<?>, Long> newCache = new HashMap<Class<?>, Long>(oldCache.size());
newCache.putAll(oldCache);
newCache.put(key, value);
offsetCache = newCache;
} }
private static Unsafe getUnsafe() { private static Unsafe getUnsafe() {