Skip to content

Support for Buffer slices & copyless methods in RepeatedBytes #72

Description

@cmuramoto

With the current API, it is 'tricky' to append byte[]s to an instance of RepeatedBytes without a copy.

The pattern is more or less like

void transfer(RepeatedBytes rb, Collection<byte[]> values) {
  rb.reserve(values.size());
  for(var value: values) {
    rb.next().setInternalArray(value).
  }
}

It would by nice to have something like

public final void addInternal(RepeatedByte value) {
  reserve(1);
  array[length++] = value;
}

public final void addInternal(byte[] value) {
  addInternal(RepeatedByte.newEmptyInstance().setInternalArray(value));
}

public final void setInternal(int index, RepeatedByte value) {
  checkIndex(index);
  array[index] = value;
}

public final void setInternal(int index, byte[] value) {
  setInternal(index,RepeatedByte.newEmptyInstance().setInternalArray(value)); 
}

When working with direct buffers, we must manifest a byte[] copy to transfer contents to a Message instance. Even though it is possible to avoid a second copy in RepeatedByte via setInternalArray, the same cannot be done in RepeatedBytes. Perhaps some helpers such as

static final byte[] copy(ByteBuffer value) {
  var rv = new byte[value.remaining()];
  buffer.duplicate().get(rv);
  return rv; 
}

public final void add(ByteBuffer value) {
  addInternal(copy(value));
}

public final void setInternal(int index, ByteBuffer value) {
  setInternal(index, copy(value)); 
}

could be useful too.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions