addIntIndirectly method

void addIntIndirectly(
  1. int value, {
  2. bool cache = false,
})

Stores int value indirectly in the buffer.

Adding large integer values indirectly might be beneficial if those values suppose to be store in a vector together with small integer values. This is due to the fact that FlexBuffers will add padding to small integer values, if they are stored together with large integer values. When we add integer indirectly the vector of ints will contain not the value itself, but only the relative offset to the value. By setting the cache parameter to true, you make sure that the builder tracks added int value and performs deduplication.

Implementation

void addIntIndirectly(int value, {bool cache = false}) {
  _integrityCheckOnValueAddition();
  if (_indirectIntCache.containsKey(value)) {
    _stack.add(_indirectIntCache[value]!);
    return;
  }
  final stackValue = _StackValue.withInt(value);
  final byteWidth = _align(stackValue.width);
  final newOffset = _newOffset(byteWidth);
  final valueOffset = _offset;
  _pushBuffer(stackValue.asU8List(stackValue.width));
  final stackOffset = _StackValue.withOffset(
      valueOffset, ValueType.IndirectInt, stackValue.width);
  _stack.add(stackOffset);
  _offset = newOffset;
  if (cache) {
    _indirectIntCache[value] = stackOffset;
  }
}