2

I am new to Google Protocol buffers and trying to model a primitive int[] array in java via a protobuf message.

Currently i am using this proto file:

syntax = "proto3";
package protobuf;

message primitiveArrayMsg {
    repeated int32 data = 1;
}

Which compiles to a java class with an List<Integer> data structure instead of an array of primitive int.

/**
 * <code>repeated int32 data = 1;</code>
 */
java.util.List<java.lang.Integer> getDataList();

My application must hold millions of int values and for saving memory I decided to use int instead of Integer. Is there a way to compile a protobuf message description in a java class with an int[] data structure?

Unfortunately, I found nothing in the Protocol Buffers Language Guide (proto3). A similar question was also asked in How to add a int array in protobuf message, which I tried but obviously the question author was looking for an ArrayList<Integer> and therefore the answer did not help me.

If there is no support for that, can you recommend me a more memory efficient way than boxing to Integer and using List<Integer>?

Bernhard
  • 703
  • 5
  • 25
  • Are you sure that it actually stores the list like that? For example, [`getData(int)`](https://developers.google.com/protocol-buffers/docs/reference/java-generated#repeated-fields) will give you back the unboxed value. It would be inefficient if it were storing the boxed value, unboxing the value to give it back to you, only for you to have to box it again. It's possible that the `getDataList()` is merely a "boxing view" of an `int[]`, but has to give you back a `List` because that's all a `List` can provide. – Andy Turner Nov 30 '18 at 23:19

1 Answers1

2

Protocol Buffer messages are not designed to handle large messages.

Even though the integers are efficiently packed by default when using proto3, a large amount of Integer objects would be needed in run-time memory (unless few distinct values are actually ever used, in which case the Integer objects can be re-used).

If you really do have to use Protocol Buffer messages for this, another option would be to transcribe the int arrays to and from a byte array format when encoding/decoding.

volley
  • 6,651
  • 1
  • 27
  • 28