优化util
This commit is contained in:
@@ -490,7 +490,7 @@ public class HttpServer extends Server<String, HttpContext, HttpRequest, HttpRes
|
||||
t.setDaemon(true);
|
||||
return t;
|
||||
});
|
||||
final ObjectReference<byte[]> dateRef = new ObjectReference<>();
|
||||
final ObjectRef<byte[]> dateRef = new ObjectRef<>();
|
||||
final DateFormat gmtDateFormat = new SimpleDateFormat("EEE, d MMM y HH:mm:ss z", Locale.ENGLISH);
|
||||
gmtDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
|
||||
dateRef.set(("Date: " + gmtDateFormat.format(new Date()) + "\r\n").getBytes());
|
||||
|
||||
@@ -20,14 +20,16 @@ public class ByteBufferPool extends ObjectPool<ByteBuffer> {
|
||||
|
||||
private final int bufferCapacity;
|
||||
|
||||
protected ByteBufferPool(ObjectPool<ByteBuffer> parent, LongAdder creatCounter, LongAdder cycleCounter, Thread unsafeThread, int max, int bufferCapacity, Queue<ByteBuffer> queue) {
|
||||
super(parent, creatCounter, cycleCounter, unsafeThread, max, (Object... params) -> ByteBuffer.allocateDirect(bufferCapacity), null, (e) -> {
|
||||
if (e == null || e.isReadOnly() || e.capacity() != bufferCapacity) {
|
||||
return false;
|
||||
}
|
||||
e.clear();
|
||||
return true;
|
||||
}, queue);
|
||||
protected ByteBufferPool(ObjectPool<ByteBuffer> parent, LongAdder creatCounter,
|
||||
LongAdder cycleCounter, Thread unsafeThread, int max, int bufferCapacity, Queue<ByteBuffer> queue) {
|
||||
super(parent, creatCounter, cycleCounter, unsafeThread, max,
|
||||
(Object... params) -> ByteBuffer.allocateDirect(bufferCapacity), null, (e) -> {
|
||||
if (e == null || e.isReadOnly() || e.capacity() != bufferCapacity) {
|
||||
return false;
|
||||
}
|
||||
e.clear();
|
||||
return true;
|
||||
}, queue);
|
||||
this.bufferCapacity = bufferCapacity;
|
||||
}
|
||||
|
||||
@@ -52,18 +54,23 @@ public class ByteBufferPool extends ObjectPool<ByteBuffer> {
|
||||
}
|
||||
|
||||
//非线程安全版
|
||||
public static ByteBufferPool createUnsafePool(ByteBufferPool parent, LongAdder creatCounter, LongAdder cycleCounter, int max, int bufferCapacity) {
|
||||
return new ByteBufferPool(parent, creatCounter, cycleCounter, null, Math.max(Utility.cpus(), max), bufferCapacity, new ArrayDeque<>(Math.max(Utility.cpus(), max)));
|
||||
public static ByteBufferPool createUnsafePool(ByteBufferPool parent, LongAdder creatCounter,
|
||||
LongAdder cycleCounter, int max, int bufferCapacity) {
|
||||
return new ByteBufferPool(parent, creatCounter, cycleCounter, null,
|
||||
Math.max(Utility.cpus(), max), bufferCapacity, new ArrayDeque<>(Math.max(Utility.cpus(), max)));
|
||||
}
|
||||
|
||||
//非线程安全版
|
||||
public static ByteBufferPool createUnsafePool(Thread unsafeThread, int max, ByteBufferPool safePool) {
|
||||
return createUnsafePool(safePool, safePool.getCreatCounter(), safePool.getCycleCounter(), unsafeThread, max, safePool.getBufferCapacity());
|
||||
return createUnsafePool(safePool, safePool.getCreatCounter(),
|
||||
safePool.getCycleCounter(), unsafeThread, max, safePool.getBufferCapacity());
|
||||
}
|
||||
|
||||
//非线程安全版
|
||||
public static ByteBufferPool createUnsafePool(ByteBufferPool parent, LongAdder creatCounter, LongAdder cycleCounter, Thread unsafeThread, int max, int bufferCapacity) {
|
||||
return new ByteBufferPool(parent, creatCounter, cycleCounter, unsafeThread, Math.max(Utility.cpus(), max), bufferCapacity, new ArrayDeque<>(Math.max(Utility.cpus(), max)));
|
||||
public static ByteBufferPool createUnsafePool(ByteBufferPool parent, LongAdder creatCounter,
|
||||
LongAdder cycleCounter, Thread unsafeThread, int max, int bufferCapacity) {
|
||||
return new ByteBufferPool(parent, creatCounter, cycleCounter, unsafeThread,
|
||||
Math.max(Utility.cpus(), max), bufferCapacity, new ArrayDeque<>(Math.max(Utility.cpus(), max)));
|
||||
}
|
||||
|
||||
//线程安全版
|
||||
@@ -78,7 +85,8 @@ public class ByteBufferPool extends ObjectPool<ByteBuffer> {
|
||||
|
||||
//线程安全版
|
||||
public static ByteBufferPool createSafePool(LongAdder creatCounter, LongAdder cycleCounter, int max, int bufferCapacity) {
|
||||
return new ByteBufferPool(null, creatCounter, cycleCounter, null, Math.max(Utility.cpus(), max), bufferCapacity, new LinkedBlockingQueue<>(Math.max(Utility.cpus(), max)));
|
||||
return new ByteBufferPool(null, creatCounter, cycleCounter, null,
|
||||
Math.max(Utility.cpus(), max), bufferCapacity, new LinkedBlockingQueue<>(Math.max(Utility.cpus(), max)));
|
||||
}
|
||||
|
||||
public int getBufferCapacity() {
|
||||
|
||||
@@ -19,6 +19,7 @@ import static java.lang.annotation.ElementType.*;
|
||||
*
|
||||
* @author zhangjx
|
||||
* @deprecated replaced by org.redkale.annotation.ConstructorParameters
|
||||
* @see org.redkale.annotation.ConstructorParameters
|
||||
*/
|
||||
@Deprecated(since = "2.8.0")
|
||||
@Documented
|
||||
|
||||
@@ -9,32 +9,7 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.function.*;
|
||||
import static org.redkale.asm.ClassWriter.COMPUTE_FRAMES;
|
||||
import org.redkale.asm.*;
|
||||
import static org.redkale.asm.Opcodes.ACC_BRIDGE;
|
||||
import static org.redkale.asm.Opcodes.ACC_FINAL;
|
||||
import static org.redkale.asm.Opcodes.ACC_PRIVATE;
|
||||
import static org.redkale.asm.Opcodes.ACC_PUBLIC;
|
||||
import static org.redkale.asm.Opcodes.ACC_STATIC;
|
||||
import static org.redkale.asm.Opcodes.ACC_SUPER;
|
||||
import static org.redkale.asm.Opcodes.ACC_SYNTHETIC;
|
||||
import static org.redkale.asm.Opcodes.ALOAD;
|
||||
import static org.redkale.asm.Opcodes.ARETURN;
|
||||
import static org.redkale.asm.Opcodes.ASTORE;
|
||||
import static org.redkale.asm.Opcodes.CHECKCAST;
|
||||
import static org.redkale.asm.Opcodes.GETFIELD;
|
||||
import static org.redkale.asm.Opcodes.GOTO;
|
||||
import static org.redkale.asm.Opcodes.IFEQ;
|
||||
import static org.redkale.asm.Opcodes.IFLE;
|
||||
import static org.redkale.asm.Opcodes.IFNONNULL;
|
||||
import static org.redkale.asm.Opcodes.IFNULL;
|
||||
import static org.redkale.asm.Opcodes.INSTANCEOF;
|
||||
import static org.redkale.asm.Opcodes.INVOKEINTERFACE;
|
||||
import static org.redkale.asm.Opcodes.INVOKESPECIAL;
|
||||
import static org.redkale.asm.Opcodes.INVOKESTATIC;
|
||||
import static org.redkale.asm.Opcodes.INVOKEVIRTUAL;
|
||||
import static org.redkale.asm.Opcodes.POP;
|
||||
import static org.redkale.asm.Opcodes.PUTFIELD;
|
||||
import static org.redkale.asm.Opcodes.RETURN;
|
||||
import static org.redkale.asm.Opcodes.V11;
|
||||
import static org.redkale.asm.Opcodes.*;
|
||||
import org.redkale.asm.Type;
|
||||
|
||||
/**
|
||||
@@ -166,7 +141,6 @@ public interface Copier<S, D> extends BiFunction<S, D, D> {
|
||||
* @param <S> 源类泛型
|
||||
* @param destClass 目标类名
|
||||
* @param srcClass 源类名
|
||||
* @param options 可配项
|
||||
*
|
||||
* @return 复制器
|
||||
*/
|
||||
@@ -181,7 +155,6 @@ public interface Copier<S, D> extends BiFunction<S, D, D> {
|
||||
* @param <S> 源类泛型
|
||||
* @param destClass 目标类名
|
||||
* @param srcClass 源类名
|
||||
* @param options 可配项
|
||||
*
|
||||
* @return 复制器
|
||||
*/
|
||||
@@ -196,7 +169,6 @@ public interface Copier<S, D> extends BiFunction<S, D, D> {
|
||||
* @param <S> 源类泛型
|
||||
* @param destClass 目标类名
|
||||
* @param srcClass 源类名
|
||||
* @param options 可配项
|
||||
*
|
||||
* @return 复制器
|
||||
*/
|
||||
@@ -597,11 +569,14 @@ public interface Copier<S, D> extends BiFunction<S, D, D> {
|
||||
final String newDynName = "org/redkaledyn/copier/_Dyn" + Copier.class.getSimpleName() + "_" + options
|
||||
+ "__" + srcClass.getName().replace('.', '_').replace('$', '_')
|
||||
+ "__" + destClass.getName().replace('.', '_').replace('$', '_');
|
||||
try {
|
||||
Class clz = RedkaleClassLoader.findDynClass(newDynName.replace('/', '.'));
|
||||
return (Copier) (clz == null ? loader.loadClass(newDynName.replace('/', '.')) : clz).getDeclaredConstructor().newInstance();
|
||||
} catch (Throwable ex) {
|
||||
if (srcColumnPredicate == null && nameAlias == null) {
|
||||
try {
|
||||
Class clz = RedkaleClassLoader.findDynClass(newDynName.replace('/', '.'));
|
||||
return (Copier) (clz == null ? loader.loadClass(newDynName.replace('/', '.')) : clz).getDeclaredConstructor().newInstance();
|
||||
} catch (Throwable ex) {
|
||||
}
|
||||
}
|
||||
|
||||
final Predicate<Class<?>> throwPredicate = e -> !RuntimeException.class.isAssignableFrom(e);
|
||||
// ------------------------------------------------------------------------------
|
||||
ClassWriter cw = new ClassWriter(COMPUTE_FRAMES);
|
||||
@@ -1132,7 +1107,9 @@ public interface Copier<S, D> extends BiFunction<S, D, D> {
|
||||
return defineClass(name, b, 0, b.length);
|
||||
}
|
||||
}.loadClass(newDynName.replace('/', '.'), bytes);
|
||||
RedkaleClassLoader.putDynClass(newDynName.replace('/', '.'), bytes, newClazz);
|
||||
if (srcColumnPredicate == null && nameAlias == null) {
|
||||
RedkaleClassLoader.putDynClass(newDynName.replace('/', '.'), bytes, newClazz);
|
||||
}
|
||||
RedkaleClassLoader.putReflectionDeclaredConstructors(newClazz, newDynName.replace('/', '.'));
|
||||
try {
|
||||
return (Copier) newClazz.getDeclaredConstructor().newInstance();
|
||||
|
||||
@@ -23,6 +23,7 @@ import java.lang.annotation.*;
|
||||
* </pre></blockquote>
|
||||
* <p>
|
||||
* 详情见: https://redkale.org
|
||||
* @see org.redkale.annotation.LogExcludeLevel
|
||||
*
|
||||
* @author zhangjx
|
||||
*/
|
||||
|
||||
@@ -14,6 +14,7 @@ import java.lang.annotation.*;
|
||||
*
|
||||
* <p>
|
||||
* 详情见: https://redkale.org
|
||||
* @see org.redkale.annotation.LogLevel
|
||||
*
|
||||
* @author zhangjx
|
||||
*/
|
||||
|
||||
@@ -15,15 +15,15 @@ package org.redkale.util;
|
||||
*
|
||||
* @since 2.8.0
|
||||
*/
|
||||
public final class ObjectReference<V> {
|
||||
public final class ObjectRef<V> {
|
||||
|
||||
private V value;
|
||||
|
||||
public ObjectReference(V initialValue) {
|
||||
public ObjectRef(V initialValue) {
|
||||
this.value = initialValue;
|
||||
}
|
||||
|
||||
public ObjectReference() {
|
||||
public ObjectRef() {
|
||||
}
|
||||
|
||||
public final V get() {
|
||||
@@ -373,9 +373,9 @@ public final class Utility {
|
||||
|
||||
static class ObjectWriteStream extends ObjectOutputStream {
|
||||
|
||||
public final ObjectReference<String> methodNameReference = new ObjectReference<>();
|
||||
public final ObjectRef<String> methodNameReference = new ObjectRef<>();
|
||||
|
||||
public final ObjectReference<String> classNameReference = new ObjectReference<>();
|
||||
public final ObjectRef<String> classNameReference = new ObjectRef<>();
|
||||
|
||||
public ObjectWriteStream(OutputStream out) throws IOException {
|
||||
super(out);
|
||||
|
||||
Reference in New Issue
Block a user