This commit is contained in:
redkale
2024-10-20 08:39:19 +08:00
parent 7405ce4dab
commit 754c9f0c2b
2 changed files with 21 additions and 19 deletions

View File

@@ -23,6 +23,10 @@ public class ProtobufBytesWriter extends ProtobufWriter { // 存在child情况
byte[] content; byte[] content;
ProtobufBytesWriter child;
private ProtobufBytesWriter parent;
// 链表结构 // 链表结构
private ProtobufBytesWriter delegate; private ProtobufBytesWriter delegate;
@@ -54,14 +58,16 @@ public class ProtobufBytesWriter extends ProtobufWriter { // 存在child情况
protected boolean recycle() { protected boolean recycle() {
super.recycle(); super.recycle();
if (this.delegate != null && this.pool != null && this.parent == null) { if (this.delegate != null && this.pool != null && this.parent == null) {
ProtobufWriter s; ProtobufBytesWriter s;
ProtobufWriter p = this.delegate; ProtobufBytesWriter p = this.delegate;
do { do {
s = p; s = p;
p = p.parent; p = p.parent;
offerPool(s); offerPool(s);
} while (p != this); } while (p != this);
} }
this.child = null;
this.parent = null;
this.delegate = null; this.delegate = null;
if (this.content.length > RESET_MAX_SIZE) { if (this.content.length > RESET_MAX_SIZE) {
this.content = new byte[DEFAULT_SIZE]; this.content = new byte[DEFAULT_SIZE];
@@ -69,10 +75,10 @@ public class ProtobufBytesWriter extends ProtobufWriter { // 存在child情况
return true; return true;
} }
private void offerPool(ProtobufWriter item) { private void offerPool(ProtobufBytesWriter item) {
if (this.pool != null && this.pool.size() < CHILD_SIZE) { if (this.pool != null && this.pool.size() < CHILD_SIZE) {
item.recycle(); item.recycle();
this.pool.offer((ProtobufBytesWriter) item); this.pool.offer(item);
} }
} }
@@ -83,9 +89,9 @@ public class ProtobufBytesWriter extends ProtobufWriter { // 存在child情况
// 必须要使用根节点的pool // 必须要使用根节点的pool
ProtobufBytesWriter root = null; ProtobufBytesWriter root = null;
if (this.parent != null) { if (this.parent != null) {
ProtobufWriter p = this; ProtobufBytesWriter p = this;
while ((p = p.parent) instanceof ProtobufBytesWriter) { while ((p = p.parent) != null) {
root = (ProtobufBytesWriter) p; root = p;
} }
} }
if (root != null) { if (root != null) {
@@ -114,9 +120,9 @@ public class ProtobufBytesWriter extends ProtobufWriter { // 存在child情况
delegate = result; delegate = result;
} }
if (this.parent != null) { if (this.parent != null) {
ProtobufWriter p = this; ProtobufBytesWriter p = this;
while ((p = p.parent) instanceof ProtobufBytesWriter) { while ((p = p.parent) != null) {
((ProtobufBytesWriter) p).delegate = result; p.delegate = result;
} }
} }
result.configFieldFunc(result.parent); result.configFieldFunc(result.parent);
@@ -125,13 +131,14 @@ public class ProtobufBytesWriter extends ProtobufWriter { // 存在child情况
@Override @Override
public void offerChild(ProtobufWriter child) { public void offerChild(ProtobufWriter child) {
if (child != null) { ProtobufBytesWriter sub = (ProtobufBytesWriter) child;
int len = child.length(); if (sub != null) {
ProtobufWriter next = child; int len = sub.length();
ProtobufBytesWriter next = sub;
while ((next = next.child) != null) { while ((next = next.child) != null) {
len += next.length(); len += next.length();
} }
child.parent.writeSelfLength(len); sub.parent.writeSelfLength(len);
} }
} }

View File

@@ -66,9 +66,6 @@ public abstract class ProtobufWriter extends Writer {
protected boolean enumtostring; protected boolean enumtostring;
protected ProtobufWriter parent;
protected ProtobufBytesWriter child;
protected Map<Stream, Object[]> streamArrayCache; protected Map<Stream, Object[]> streamArrayCache;
@@ -113,8 +110,6 @@ public abstract class ProtobufWriter extends Writer {
@Override @Override
protected boolean recycle() { protected boolean recycle() {
super.recycle(); super.recycle();
this.child = null;
this.parent = null;
this.mapFieldFunc = null; this.mapFieldFunc = null;
this.objFieldFunc = null; this.objFieldFunc = null;
this.objExtFunc = null; this.objExtFunc = null;