Efficiently clearing flatbuffer builders for layers of tables

1.2k Views Asked by At

Can I reuse 'sub' builder instances generated from a flatbuffers::FlatBufferBuilder after calling builder.Clear()? What is the effect of builder.Clear() on sub-builders?

Having generated a flatbuffers schema such as the following:

table FB_mytable1{
myshort::ushort = 0;
}
table FB_table2{
nestedTable1::FB_mytable1;
nestedTable1::FB_mytable1;
}
root_type FB_table2;

If I reset the builder using builder.clear(), my instantiation of 'flatbuffers::FlatBufferBuilder', will this allow me to generate new serial data without calling a reset function or renewing any of the individual table serialisers, such as my instantiation of FB_mytable1Builder myFB_mytable1Builder(builder)? Or do I need to ensure that the individual builder objects have a scope that means they are not reused?

1

There are 1 best solutions below

2
On BEST ANSWER

clear() resets a FlatBufferBuilder as if just constructed, and any of the table builder instances should not be reused across multiple buffers or even multiple tables.

See, a FlatBufferBuilder is a somewhat heavyweight structure (since it owns a buffer), so it makes sense to reuse it when you can. The table builders however are super lightweight, so should just be a local variable used for construction of a single table, you can't reuse them.