Bernhard
Bernhard

Reputation: 883

How to model Java primitive int[] array in protobuf message

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>?

Upvotes: 2

Views: 2635

Answers (1)

volley
volley

Reputation: 6711

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.

Upvotes: 2

Related Questions