mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-29 04:40:03 +00:00
Reducing garbage produced by Java serializer.
startObject() now only allocates a new vtable array when it needs to grow. Tested: on Linux. Change-Id: Idd041605afcb9487a34d63bda067172d797f437a
This commit is contained in:
@@ -28,12 +28,13 @@ import java.nio.charset.Charset;
|
|||||||
* main FlatBuffers documentation.
|
* main FlatBuffers documentation.
|
||||||
*/
|
*/
|
||||||
public class FlatBufferBuilder {
|
public class FlatBufferBuilder {
|
||||||
ByteBuffer bb; // Where we construct the FlatBuffer.
|
ByteBuffer bb; // Where we construct the FlatBuffer.
|
||||||
int space; // Remaining space in the ByteBuffer.
|
int space; // Remaining space in the ByteBuffer.
|
||||||
static final Charset utf8charset = Charset.forName("UTF-8");
|
static final Charset utf8charset = Charset.forName("UTF-8");
|
||||||
int minalign = 1; // Minimum alignment encountered so far.
|
int minalign = 1; // Minimum alignment encountered so far.
|
||||||
int[] vtable = null; // The vtable for the current table, null otherwise.
|
int[] vtable = null; // The vtable for the current table.
|
||||||
int object_start; // Starting offset of the current struct/table.
|
boolean nested = false; // Whether we are currently serializing a table.
|
||||||
|
int object_start; // Starting offset of the current struct/table.
|
||||||
int[] vtables = new int[16]; // List of offsets of all vtables.
|
int[] vtables = new int[16]; // List of offsets of all vtables.
|
||||||
int num_vtables = 0; // Number of entries in `vtables` in use.
|
int num_vtables = 0; // Number of entries in `vtables` in use.
|
||||||
int vector_num_elems = 0; // For the current vector being built.
|
int vector_num_elems = 0; // For the current vector being built.
|
||||||
@@ -245,7 +246,7 @@ public class FlatBufferBuilder {
|
|||||||
* while an object is being constructed
|
* while an object is being constructed
|
||||||
*/
|
*/
|
||||||
public void notNested() {
|
public void notNested() {
|
||||||
if (vtable != null)
|
if (nested)
|
||||||
throw new AssertionError("FlatBuffers: object serialization must not be nested.");
|
throw new AssertionError("FlatBuffers: object serialization must not be nested.");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -304,7 +305,8 @@ public class FlatBufferBuilder {
|
|||||||
*/
|
*/
|
||||||
public void startObject(int numfields) {
|
public void startObject(int numfields) {
|
||||||
notNested();
|
notNested();
|
||||||
vtable = new int[numfields];
|
if (vtable == null || vtable.length < numfields) vtable = new int[numfields];
|
||||||
|
nested = true;
|
||||||
object_start = offset();
|
object_start = offset();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -338,7 +340,8 @@ public class FlatBufferBuilder {
|
|||||||
* @see #startObject(int)
|
* @see #startObject(int)
|
||||||
*/
|
*/
|
||||||
public int endObject() {
|
public int endObject() {
|
||||||
assert vtable != null; // calling endObject without a startObject
|
if (vtable == null || !nested)
|
||||||
|
throw new AssertionError("FlatBuffers: endObject called without startObject");
|
||||||
addInt(0);
|
addInt(0);
|
||||||
int vtableloc = offset();
|
int vtableloc = offset();
|
||||||
// Write out the current vtable.
|
// Write out the current vtable.
|
||||||
@@ -385,7 +388,7 @@ public class FlatBufferBuilder {
|
|||||||
bb.putInt(bb.capacity() - vtableloc, offset() - vtableloc);
|
bb.putInt(bb.capacity() - vtableloc, offset() - vtableloc);
|
||||||
}
|
}
|
||||||
|
|
||||||
vtable = null;
|
nested = false;
|
||||||
return vtableloc;
|
return vtableloc;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user