优化util

This commit is contained in:
redkale
2023-08-12 18:50:22 +08:00
parent e729f04917
commit eb1c0e1513
8 changed files with 42 additions and 54 deletions

View File

@@ -490,7 +490,7 @@ public class HttpServer extends Server<String, HttpContext, HttpRequest, HttpRes
t.setDaemon(true); t.setDaemon(true);
return t; 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); final DateFormat gmtDateFormat = new SimpleDateFormat("EEE, d MMM y HH:mm:ss z", Locale.ENGLISH);
gmtDateFormat.setTimeZone(TimeZone.getTimeZone("GMT")); gmtDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
dateRef.set(("Date: " + gmtDateFormat.format(new Date()) + "\r\n").getBytes()); dateRef.set(("Date: " + gmtDateFormat.format(new Date()) + "\r\n").getBytes());

View File

@@ -20,8 +20,10 @@ public class ByteBufferPool extends ObjectPool<ByteBuffer> {
private final int bufferCapacity; private final int bufferCapacity;
protected ByteBufferPool(ObjectPool<ByteBuffer> parent, LongAdder creatCounter, LongAdder cycleCounter, Thread unsafeThread, int max, int bufferCapacity, Queue<ByteBuffer> queue) { protected ByteBufferPool(ObjectPool<ByteBuffer> parent, LongAdder creatCounter,
super(parent, creatCounter, cycleCounter, unsafeThread, max, (Object... params) -> ByteBuffer.allocateDirect(bufferCapacity), null, (e) -> { 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) { if (e == null || e.isReadOnly() || e.capacity() != bufferCapacity) {
return false; return false;
} }
@@ -52,18 +54,23 @@ public class ByteBufferPool extends ObjectPool<ByteBuffer> {
} }
//非线程安全版 //非线程安全版
public static ByteBufferPool createUnsafePool(ByteBufferPool parent, LongAdder creatCounter, LongAdder cycleCounter, int max, int bufferCapacity) { public static ByteBufferPool createUnsafePool(ByteBufferPool parent, LongAdder creatCounter,
return new ByteBufferPool(parent, creatCounter, cycleCounter, null, Math.max(Utility.cpus(), max), bufferCapacity, new ArrayDeque<>(Math.max(Utility.cpus(), max))); 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) { 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) { public static ByteBufferPool createUnsafePool(ByteBufferPool parent, LongAdder creatCounter,
return new ByteBufferPool(parent, creatCounter, cycleCounter, unsafeThread, Math.max(Utility.cpus(), max), bufferCapacity, new ArrayDeque<>(Math.max(Utility.cpus(), max))); 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) { 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() { public int getBufferCapacity() {

View File

@@ -19,6 +19,7 @@ import static java.lang.annotation.ElementType.*;
* *
* @author zhangjx * @author zhangjx
* @deprecated replaced by org.redkale.annotation.ConstructorParameters * @deprecated replaced by org.redkale.annotation.ConstructorParameters
* @see org.redkale.annotation.ConstructorParameters
*/ */
@Deprecated(since = "2.8.0") @Deprecated(since = "2.8.0")
@Documented @Documented

View File

@@ -9,32 +9,7 @@ import java.util.concurrent.ConcurrentHashMap;
import java.util.function.*; import java.util.function.*;
import static org.redkale.asm.ClassWriter.COMPUTE_FRAMES; import static org.redkale.asm.ClassWriter.COMPUTE_FRAMES;
import org.redkale.asm.*; import org.redkale.asm.*;
import static org.redkale.asm.Opcodes.ACC_BRIDGE; import static org.redkale.asm.Opcodes.*;
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 org.redkale.asm.Type; import org.redkale.asm.Type;
/** /**
@@ -166,7 +141,6 @@ public interface Copier<S, D> extends BiFunction<S, D, D> {
* @param <S> 源类泛型 * @param <S> 源类泛型
* @param destClass 目标类名 * @param destClass 目标类名
* @param srcClass 源类名 * @param srcClass 源类名
* @param options 可配项
* *
* @return 复制器 * @return 复制器
*/ */
@@ -181,7 +155,6 @@ public interface Copier<S, D> extends BiFunction<S, D, D> {
* @param <S> 源类泛型 * @param <S> 源类泛型
* @param destClass 目标类名 * @param destClass 目标类名
* @param srcClass 源类名 * @param srcClass 源类名
* @param options 可配项
* *
* @return 复制器 * @return 复制器
*/ */
@@ -196,7 +169,6 @@ public interface Copier<S, D> extends BiFunction<S, D, D> {
* @param <S> 源类泛型 * @param <S> 源类泛型
* @param destClass 目标类名 * @param destClass 目标类名
* @param srcClass 源类名 * @param srcClass 源类名
* @param options 可配项
* *
* @return 复制器 * @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 final String newDynName = "org/redkaledyn/copier/_Dyn" + Copier.class.getSimpleName() + "_" + options
+ "__" + srcClass.getName().replace('.', '_').replace('$', '_') + "__" + srcClass.getName().replace('.', '_').replace('$', '_')
+ "__" + destClass.getName().replace('.', '_').replace('$', '_'); + "__" + destClass.getName().replace('.', '_').replace('$', '_');
if (srcColumnPredicate == null && nameAlias == null) {
try { try {
Class clz = RedkaleClassLoader.findDynClass(newDynName.replace('/', '.')); Class clz = RedkaleClassLoader.findDynClass(newDynName.replace('/', '.'));
return (Copier) (clz == null ? loader.loadClass(newDynName.replace('/', '.')) : clz).getDeclaredConstructor().newInstance(); return (Copier) (clz == null ? loader.loadClass(newDynName.replace('/', '.')) : clz).getDeclaredConstructor().newInstance();
} catch (Throwable ex) { } catch (Throwable ex) {
} }
}
final Predicate<Class<?>> throwPredicate = e -> !RuntimeException.class.isAssignableFrom(e); final Predicate<Class<?>> throwPredicate = e -> !RuntimeException.class.isAssignableFrom(e);
// ------------------------------------------------------------------------------ // ------------------------------------------------------------------------------
ClassWriter cw = new ClassWriter(COMPUTE_FRAMES); 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); return defineClass(name, b, 0, b.length);
} }
}.loadClass(newDynName.replace('/', '.'), bytes); }.loadClass(newDynName.replace('/', '.'), bytes);
if (srcColumnPredicate == null && nameAlias == null) {
RedkaleClassLoader.putDynClass(newDynName.replace('/', '.'), bytes, newClazz); RedkaleClassLoader.putDynClass(newDynName.replace('/', '.'), bytes, newClazz);
}
RedkaleClassLoader.putReflectionDeclaredConstructors(newClazz, newDynName.replace('/', '.')); RedkaleClassLoader.putReflectionDeclaredConstructors(newClazz, newDynName.replace('/', '.'));
try { try {
return (Copier) newClazz.getDeclaredConstructor().newInstance(); return (Copier) newClazz.getDeclaredConstructor().newInstance();

View File

@@ -23,6 +23,7 @@ import java.lang.annotation.*;
* </pre></blockquote> * </pre></blockquote>
* <p> * <p>
* 详情见: https://redkale.org * 详情见: https://redkale.org
* @see org.redkale.annotation.LogExcludeLevel
* *
* @author zhangjx * @author zhangjx
*/ */

View File

@@ -14,6 +14,7 @@ import java.lang.annotation.*;
* *
* <p> * <p>
* 详情见: https://redkale.org * 详情见: https://redkale.org
* @see org.redkale.annotation.LogLevel
* *
* @author zhangjx * @author zhangjx
*/ */

View File

@@ -15,15 +15,15 @@ package org.redkale.util;
* *
* @since 2.8.0 * @since 2.8.0
*/ */
public final class ObjectReference<V> { public final class ObjectRef<V> {
private V value; private V value;
public ObjectReference(V initialValue) { public ObjectRef(V initialValue) {
this.value = initialValue; this.value = initialValue;
} }
public ObjectReference() { public ObjectRef() {
} }
public final V get() { public final V get() {

View File

@@ -373,9 +373,9 @@ public final class Utility {
static class ObjectWriteStream extends ObjectOutputStream { 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 { public ObjectWriteStream(OutputStream out) throws IOException {
super(out); super(out);