GetObject and GetMutableObject are similar to GetRoot and GetMutableRoot,
and can be useful when wanting to access data that has just been created.
Unfortunately there is a danger in using these methods, as it is possible
that the buffer reallocates which will invalidate the pointers.
This avoids the following allocations over repeated use:
- A ``CharsetEncoder``
- A byte array to contain the contents of the string
- A wrapper, via ``ByteBuffer#wrap``, for the previously mentioned byte
array
This also removes a copy of the temporary byte array. This extra copy
is needed because ``String#getBytes`` returns a byte array and its
length must match the exact size of the contents.
This implementation requires that the builder retain a ``ByteBuffer``
and ``CharEncoder``. This is considered slower only for users that
will only allocate a single string in their buffers or none at all.
The improvement is especially potent if the builder is constantly reused
by its caller. This also eliminates the penalty for users that do not
use strings at all since the cost of allocating these fields is
now amortized.
The only allocation left in this code is a ``CharBuffer`` wrapper. It's
possible to eliminate this allocation using an additional copy but need
further profiling to see which is the bigger problem.
Removes the following allocations:
- ``CharsetDecoder`` is reused between calls
- ``CharBuffer#wrap`` removed in favor of heap
based char buffer that is reused
- Temporary ``char[]``, an intermediate copy inside ``StringCoding``
- Another ``char[]``, this is needed because ``StringCoding`` uses
a ``CharBuffer`` internally but returns a ``char[]``. Extra
characters need to be trimmed so this means yet another allocation
- Yet another ``char[]`` directly from ``__string`` for non-heap
based buffers
Removes the following copies
- No copy is performed to trim the allocation since a ``CharBuffer``
is used directly
- For non-heap based byte buffers, removes the copy that was
previously done in the __string function
This does need to get the TLS entry which implies at least some
contention on the thread object table and a fence.
A previous commit that added "f" for C++/Java/C# would break
on gcc of constants like 3f, which are now output as 3.0f
Tested: on Linux
Change-Id: If9cabbe3c6d6948a5050b8b123bda9c06e181f52
CreateUninitializedVector was performing the following actions:
1. call StartVector.
2. call make_space, and set buf to point to the reserved space.
3. call EndVector.
The problem is that a call to EndVector can ultimately call make_space, which
if the buffer is full, will cause a reallocation, invalidating the value stored
in buf. So setting buf needs to be delayed until after EndVector.
The following code, when run under valgrind shows a write to free'd memory before
the change, but no such error after:
int main()
{
flatbuffers::FlatBufferBuilder fbb(128);
char *buf = nullptr;
fbb.CreateUninitializedVector(128, &buf);
*buf = 0;
}