Java Proto lite: avoid boxing Integers accessing enum lists

Provide a specialization for ListAdapter that avoids boxing ints into Integers.

Use this for repeated enum accessors.

In practice, the ints for most enums will fit into the JDK's "boxed int cache", avoiding extra allocations. For large enum numbers, they won't fit, and would allocate. Removing the boxing should also slightly speed up code.

PiperOrigin-RevId: 629583247
pull/16690/head
Mark Hansen 7 months ago committed by Copybara-Service
parent e1559c8efd
commit fb054c8e41
  1. 30
      java/core/src/main/java/com/google/protobuf/Internal.java
  2. 25
      src/google/protobuf/compiler/java/lite/enum_field.cc

@ -371,6 +371,36 @@ public final class Internal {
return ((MessageLite) destination).toBuilder().mergeFrom((MessageLite) source).buildPartial();
}
/**
* Provides an immutable view of {@code List<T>} around an {@code IntList}.
*
* <p>Protobuf internal. Used in protobuf generated code only.
*/
public static class IntListAdapter<T> extends AbstractList<T> {
/** Convert individual elements of the List from int to T. */
public interface IntConverter<T> {
T convert(int from);
}
private final IntList fromList;
private final IntConverter<T> converter;
public IntListAdapter(IntList fromList, IntConverter<T> converter) {
this.fromList = fromList;
this.converter = converter;
}
@Override
public T get(int index) {
return converter.convert(fromList.getInt(index));
}
@Override
public int size() {
return fromList.size();
}
}
/**
* Provides an immutable view of {@code List<T>} around a {@code List<F>}.
*

@ -597,12 +597,12 @@ void RepeatedImmutableEnumFieldLiteGenerator::GenerateMembers(
variables_,
"private com.google.protobuf.Internal.IntList $name$_;\n"
"private static final "
"com.google.protobuf.Internal.ListAdapter.Converter<\n"
" java.lang.Integer, $type$> $name$_converter_ =\n"
" new com.google.protobuf.Internal.ListAdapter.Converter<\n"
" java.lang.Integer, $type$>() {\n"
"com.google.protobuf.Internal.IntListAdapter.IntConverter<\n"
" $type$> $name$_converter_ =\n"
" new com.google.protobuf.Internal.IntListAdapter.IntConverter<\n"
" $type$>() {\n"
" @java.lang.Override\n"
" public $type$ convert(java.lang.Integer from) {\n"
" public $type$ convert(int from) {\n"
" $type$ result = $type$.forNumber(from);\n"
" return result == null ? $unknown$ : result;\n"
" }\n"
@ -610,14 +610,13 @@ void RepeatedImmutableEnumFieldLiteGenerator::GenerateMembers(
PrintExtraFieldInfo(variables_, printer);
WriteFieldAccessorDocComment(printer, descriptor_, LIST_GETTER,
context_->options());
printer->Print(
variables_,
"@java.lang.Override\n"
"$deprecation$public java.util.List<$type$> "
"${$get$capitalized_name$List$}$() {\n"
" return new com.google.protobuf.Internal.ListAdapter<\n"
" java.lang.Integer, $type$>($name$_, $name$_converter_);\n"
"}\n");
printer->Print(variables_,
"@java.lang.Override\n"
"$deprecation$public java.util.List<$type$> "
"${$get$capitalized_name$List$}$() {\n"
" return new com.google.protobuf.Internal.IntListAdapter<\n"
" $type$>($name$_, $name$_converter_);\n"
"}\n");
printer->Annotate("{", "}", descriptor_);
WriteFieldAccessorDocComment(printer, descriptor_, LIST_COUNT,
context_->options());

Loading…
Cancel
Save