|
@@ -7,6 +7,7 @@ package org.redkalex.benchmark;
|
|
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
import java.util.List;
|
|
|
+import java.util.function.Function;
|
|
|
import org.redkale.convert.Convert;
|
|
|
import org.redkale.net.http.*;
|
|
|
import org.redkale.util.*;
|
|
@@ -19,7 +20,9 @@ public class FortuneRender implements org.redkale.net.http.HttpRender {
|
|
|
|
|
|
private static final String contentType = "text/html; charset=utf-8";
|
|
|
|
|
|
- private static final byte[] text1 = "<!DOCTYPE html><html><head><title>Fortunes</title></head><body><table><tr><th>id</th><th>message</th></tr>".getBytes(StandardCharsets.UTF_8);
|
|
|
+ private static final byte[] text1 =
|
|
|
+ "<!DOCTYPE html><html><head><title>Fortunes</title></head><body><table><tr><th>id</th><th>message</th></tr>"
|
|
|
+ .getBytes(StandardCharsets.UTF_8);
|
|
|
|
|
|
private static final byte[] text2 = "<tr><td>".getBytes(StandardCharsets.UTF_8);
|
|
|
|
|
@@ -29,9 +32,9 @@ public class FortuneRender implements org.redkale.net.http.HttpRender {
|
|
|
|
|
|
private static final byte[] text5 = "</table></body></html>".getBytes(StandardCharsets.UTF_8);
|
|
|
|
|
|
- private final ThreadLocal<ByteArray> localByteArray = ThreadLocal.withInitial(() -> new ByteArray(1280));
|
|
|
+ private static final String arrayName = "fortuneByteArray";
|
|
|
|
|
|
- private final ThreadLocal<ByteArray> localTmpArray = ThreadLocal.withInitial(() -> new ByteArray(128));
|
|
|
+ private static final Function<String, ByteArray> arrayFunc = s -> new ByteArray(1280);
|
|
|
|
|
|
private byte[][] idBytesCache;
|
|
|
|
|
@@ -53,12 +56,11 @@ public class FortuneRender implements org.redkale.net.http.HttpRender {
|
|
|
|
|
|
@Override
|
|
|
public void renderTo(HttpRequest request, HttpResponse response, Convert convert, HttpScope scope) {
|
|
|
- ByteArray array = localByteArray.get().clear();
|
|
|
+ ByteArray array = request.getSubobjectIfAbsent(arrayName, arrayFunc).clear();
|
|
|
array.put(text1);
|
|
|
- ByteArray tmp = localTmpArray.get();
|
|
|
for (Fortune item : (List<Fortune>) scope.getReferObj()) {
|
|
|
- array.put(text2).put(escapeId(item.getId()))
|
|
|
- .put(text3).put(escapeMessage(tmp, item.getMessage())).put(text4);
|
|
|
+ ByteArray msg = request.getSubobjectIfAbsent(item.getMessage(), this::escapeMessage);
|
|
|
+ array.put(text2).put(escapeId(item.getId())).put(text3).put(msg).put(text4);
|
|
|
}
|
|
|
array.put(text5);
|
|
|
response.finish(contentType, array);
|
|
@@ -72,23 +74,22 @@ public class FortuneRender implements org.redkale.net.http.HttpRender {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private ByteArray escapeMessage(ByteArray tmp, String message) {
|
|
|
- tmp.clear();
|
|
|
+ private ByteArray escapeMessage(String message) {
|
|
|
+ ByteArray array = new ByteArray(128);
|
|
|
CharSequence cs = message;
|
|
|
for (int i = 0; i < cs.length(); i++) {
|
|
|
char c = cs.charAt(i);
|
|
|
byte[] bs = c < escapeCache.length ? escapeCache[c] : null;
|
|
|
if (bs != null) {
|
|
|
- tmp.put(bs);
|
|
|
+ array.put(bs);
|
|
|
} else if (c < 0x80) {
|
|
|
- tmp.put((byte) c);
|
|
|
+ array.put((byte) c);
|
|
|
} else if (c < 0x800) {
|
|
|
- tmp.put((byte) (0xc0 | (c >> 6)), (byte) (0x80 | (c & 0x3f)));
|
|
|
+ array.put((byte) (0xc0 | (c >> 6)), (byte) (0x80 | (c & 0x3f)));
|
|
|
} else {
|
|
|
- tmp.put((byte) (0xe0 | ((c >> 12))), (byte) (0x80 | ((c >> 6) & 0x3f)), (byte) (0x80 | (c & 0x3f)));
|
|
|
+ array.put((byte) (0xe0 | (c >> 12)), (byte) (0x80 | ((c >> 6) & 0x3f)), (byte) (0x80 | (c & 0x3f)));
|
|
|
}
|
|
|
}
|
|
|
- return tmp;
|
|
|
+ return array;
|
|
|
}
|
|
|
-
|
|
|
}
|