Commit graph

1688 commits

Author SHA1 Message Date
Protobuf Team Bot
d17cf67434 Use monomorphic static dispatch for Java Protobuf submessage parsing
PiperOrigin-RevId: 970550003
2026-08-25 07:51:15 -07:00
Protobuf Team Bot
7f86060aae Avoid unchecked cast warnings in TextFormat.parse
Use Class.cast instead of unchecked casts to eliminate @SuppressWarnings("unchecked") in TextFormat.parse overloads.

Shouldn't be a performance problem - this is only available in full runtime (not android) where hotspot has a class.cast intrinsic (and text proto parsing is relatively slow anyway, this won't be the bottleneck).

PiperOrigin-RevId: 970171466
2026-08-24 17:04:58 -07:00
Protobuf Team Bot
44baf91cc1 Improve type safety in ProtobufList.concatenate
Use Collection<?> for the sizing check to eliminate unchecked cast warning and narrow @SuppressWarnings("unchecked") to the specific ProtobufList fast-path assignment.

PiperOrigin-RevId: 970165037
2026-08-24 16:54:42 -07:00
Protobuf Team Bot
11117398dd Update CodedInputStream Javadoc regarding exception handling and isAtEnd behavior.
PiperOrigin-RevId: 970118591
2026-08-24 15:32:21 -07:00
Protobuf Team Bot
dee117a787 Avoid empty byte array allocations in Proto Lite toByteArray().
In AbstractMessageLite.toByteArray(), check if getSerializedSize() == 0 and return Internal.EMPTY_BYTE_ARRAY directly instead of allocating a new 0-byte array and CodedOutputStream wrappers.

PiperOrigin-RevId: 970010400
2026-08-24 12:44:01 -07:00
Protobuf Team Bot
f1a2ca97f8 Avoid unchecked cast warning in Internal.getDefaultInstance
Use Class.cast instead of an unchecked generic cast to avoid compiler warnings and ensure runtime type safety.

Not a big deal but a nice opportunity to remove a warning.

This is only used in the full runtime AFAIK, where hotspot has an intrinsic for Class.cast so shouldn't be a performance issue.

PiperOrigin-RevId: 969794451
2026-08-24 05:40:20 -07:00
Protobuf Team Bot
bf50167a85 Allow disabling lazy parsing in individual ExtensionRegistryLite which will signal the parsing whether should parse extension fields lazily.
PiperOrigin-RevId: 968674119
2026-08-21 13:57:24 -07:00
Protobuf Team Bot
c1d8a858cd Short-circuit empty packed fields in CodedInputStream.countPackedVarints
0-length packed fields occur but are fairly rare since our serializers will emit nothing instead of emitting a 0-length packed field. But, there's little reason to leave inequality case behind since we're checking against <=0 anyway.

PiperOrigin-RevId: 967271476
2026-08-19 10:08:51 -07:00
Protobuf Team Bot
37f78578e7 - Updates DslList and ExtensionList to take a lazy supplier lambda () -> List<E>, retaining secondary constructors for binary backward compatibility with existing compiled proto code.
PiperOrigin-RevId: 966853174
2026-08-18 16:01:51 -07:00
Protobuf Team Bot
b9c05c1d72 Internal change.
PiperOrigin-RevId: 966801750
2026-08-18 14:19:21 -07:00
Protobuf Team Bot
cfb934ad48 Optimize Java Protobuf packed varint counting using SWAR bitwise operations.
Implements 64-bit SWAR chunking via ByteBuffer on non-Android devices, falling back to a scalar countdown loop on Android devices. Sub-8 byte inputs bypass 64-bit chunking and fall through to scalar counting.

### JMH Microbenchmark Results (VarintCountingBenchmark)

| Payload Length | Scalar Baseline | SWAR (ByteBuffer) | Speedup |
| :--- | :--- | :--- | :--- |
| 1 B | 1.78 ns/op | 1.91 ns/op | 1.0x (parity) |
| 4 B | 3.75 ns/op | 3.65 ns/op | 1.0x (parity) |
| 8 B | 6.19 ns/op | 3.44 ns/op | 1.8x faster |
| 16 B | 9.93 ns/op | 4.00 ns/op | 2.5x faster |
| 32 B | 18.58 ns/op | 5.06 ns/op | 3.7x faster |
| 64 B | 35.50 ns/op | 7.51 ns/op | 4.7x faster |
| 128 B | 74.66 ns/op | 10.92 ns/op | 6.8x faster |
| 1024 B | 641.02 ns/op | 75.63 ns/op | 8.5x faster |

PiperOrigin-RevId: 966557820
2026-08-18 06:29:53 -07:00
Protobuf Team Bot
4374cbf43b Internal change.
PiperOrigin-RevId: 966255809
2026-08-17 16:54:30 -07:00
Protobuf Team Bot
ea0cc34313 Move initialization of descriptor out of the generated file outer class to avoid circular static initialization issue (e.g. https://github.com/protocolbuffers/protobuf/issues/29012).
In OSS Java protobuf generation, this change wraps the file descriptor initialization inside a nested static class named `InternalDescriptors`. This prevents statically initializing the generated file's outer class when only its descriptor is accessed, which avoids premature loading of dependencies like `DescriptorsProto` and `JavaFeaturesProto`.

PiperOrigin-RevId: 964796532
2026-08-14 11:23:22 -07:00
Protobuf Team Bot
cba090367e Internal change.
PiperOrigin-RevId: 964417298
2026-08-13 19:04:34 -07:00
Protobuf Team Bot
42ceed172e Internal change.
PiperOrigin-RevId: 964378413
2026-08-13 17:17:49 -07:00
Protobuf Team Bot
6e6f4048c4 Internal Change
PiperOrigin-RevId: 964360922
2026-08-13 16:37:00 -07:00
Protobuf Team Bot
528ec6b9e8 Automated rollback of commit 3c13af10fa.
PiperOrigin-RevId: 964327862
2026-08-13 15:33:20 -07:00
Protobuf Team Bot
090f4cbb88 Internal change
PiperOrigin-RevId: 964178362
2026-08-13 10:58:44 -07:00
Protobuf Team Bot
3c13af10fa Internal change.
PiperOrigin-RevId: 964153546
2026-08-13 10:13:50 -07:00
Protobuf Team Bot
8b7fbb5927 Automated rollback of commit 7023abcb48.
PiperOrigin-RevId: 963495450
2026-08-12 09:39:10 -07:00
Protobuf Team Bot
b67d2697ae Add tests for int64 varint 10th byte bug.
This is the test for an issue where if the 10th byte was 0x81 (a non-zero value with  was accidentally included in cl/962791052 and submitted, which was intended to be submitted separately.

PiperOrigin-RevId: 963393786
2026-08-12 06:00:36 -07:00
Protobuf Team Bot
6d03e4f090 Clean up varint encoder experiment, the 'new' arm has won for all cases.
PiperOrigin-RevId: 962791052
2026-08-11 08:20:45 -07:00
Protobuf Team Bot
dd3d3e82fc Automatically traverse proto_library exports for Kotlin code generation.
Aligns Kotlin proto rules with `java_proto_library`, letting Kotlin gencode resolve exported proto targets from `proto_library.exports`.

PiperOrigin-RevId: 962600951
2026-08-11 00:04:51 -07:00
Protobuf Team Bot
45b99dc2a9 Automatically traverse proto_library exports for Kotlin code generation.
Aligns Kotlin proto rules with `java_proto_library`, letting Kotlin gencode resolve exported proto targets from `proto_library.exports`.

PiperOrigin-RevId: 962536667
2026-08-10 21:03:36 -07:00
Protobuf Team Bot
50f810693c Internal change.
PiperOrigin-RevId: 962434750
2026-08-10 16:36:53 -07:00
Protobuf Team Bot
7023abcb48 Bypass dynamicMethod for getParserForType and getDefaultInstanceForType in Java Lite.
Directly invoke getParserForClass and getDefaultInstance in GeneratedMessageLite instead of routing through dynamicMethod. This builds on the work added in cl/944139195.

This allows removing the GET_PARSER and GET_DEFAULT_INSTANCE switch cases from dynamicMethod in compiler/java/lite/message.cc and removing the corresponding enum values from MethodToInvoke.

Additionally, updates the Samsung bug fallback path in GeneratedMessageLite.getDefaultInstance to allocate an instance via UnsafeUtil without calling method getters on the uninitialized object, triggering static initialization (<clinit>) safely without reflection or method call cycles.

PiperOrigin-RevId: 962355079
2026-08-10 14:05:24 -07:00
Protobuf Team Bot
620c9cf483 Disable lazy parsing for extensions on descriptor.proto messages.
PiperOrigin-RevId: 960920493
2026-08-07 07:36:45 -07:00
Protobuf Team Bot
0c9ba64990 Kotlinc 2.4 language version update pre-work.
Fix impossible is checks that are guaranteed to always returns false.

PiperOrigin-RevId: 958436856
2026-08-03 10:12:49 -07:00
Jason Aragorn Tobias Lunn
713b6a4a24 Add json_options.proto and json_enumvalue_options.proto and/or gencode derived from them to release artifacts
* `protoc`: Included in zips alongside binary
* Java: Generated classes have been added to the Maven `util` artifact
* Ruby: Generated classes have been added to gems
* CMake: `.proto` files are included in installation

PiperOrigin-RevId: 957382561
2026-07-31 16:20:59 -07:00
Protobuf Team Bot
90257a0983 Internal change
PiperOrigin-RevId: 955548742
2026-07-28 17:03:23 -07:00
Michael Hoisie
54fe86b091 Avoid UTF8 length calculations in ArrayEncoder.writeStringNoTag
Previously, for strings whose length prefix could span more than one varint
byte, the encoder walked the string twice: Utf8.encodedLength to size the
prefix, then Utf8.encode to write the bytes. Update this logic to just write
the string in one pass and avoid calling Utf8.encodedLength.

For ServerProcessor, the current optimal String.getBytes(UTF_8) is called. For
MobileProcessor, the string is written to the buffer directly with no
additional allocation first, and then the length is written as the prefix.
This approach is faster for mobile than Utf8.encodedLength, which requires
iterating over the string character by character. This approach is also
guaranteed to not overrun the buffer, because the buffer was already sized
using Utf8.encodedLength.

Message                   Runtime   Before(ns)  After(ns) Speedup
------------------------  -------   ---------  ---------  -------
google_message1 (228 B)   JVM           253.8      199.4   1.26x
google_message1           ART           460.9      407.3   1.13x
google_message2 (85 KB)   JVM          156309     130485   1.25x
google_message2           ART          201538     178130   1.13x

PiperOrigin-RevId: 955379320
2026-07-28 11:43:42 -07:00
Protobuf Team Bot
b95796f8c2 Fix Java Lite EOF conformance issue when hitting EOF early in stream parsing.
This is achieved by making the recently-added `pushLimitBeforeMessage` and `popLimitAfterMessage` package-protected so that it can be used by CodedInputStreamReader which was otherwise doing its own pushing-and-popping without properly checking these boundary conditions.

PiperOrigin-RevId: 955353136
2026-07-28 10:59:47 -07:00
Protobuf Team Bot
a0c493ce55 Propagate recursion limit when parsing MessageSet extensions.
PiperOrigin-RevId: 954943638
2026-07-27 18:27:35 -07:00
Protobuf Team Bot
a045f8497e Add LazyStringArrayList specific equals method
Check instanceof LazyStringArrayList as that'll be the most common case and it's cheap (a pointer on the klass) rather than a loop on the set of interfaces.

PiperOrigin-RevId: 954927009
2026-07-27 17:47:51 -07:00
Protobuf Team Bot
3b45e7a88c Optimize ProtobufArrayList.equals by checking instanceof ProtobufArrayList early
I think it's most common that we're comparing protos against protos, where we'll be comparing ProtobufArrayLists against ProtobufArrayLists.

It's faster to check instanceof SomeFinalClass (a pointer comparison) than to check instanceof SomeInterface (walk a set of interfaces)

PiperOrigin-RevId: 954892499
2026-07-27 16:30:21 -07:00
Protobuf Team Bot
572ca36fd7 Omit GET_MEMOIZED_IS_INITIALIZED in Java Lite for messages without required fields.
Messages without required fields omit GET_MEMOIZED_IS_INITIALIZED and SET_MEMOIZED_IS_INITIALIZED switch cases from dynamicMethod to minimize gencode size.

For those messages, dynamicMethod returns null, skipping memoization and delegating directly to MessageSchema.isInitialized(), which evaluates to true in O(1) time.

PiperOrigin-RevId: 954754880
2026-07-27 12:02:55 -07:00
Protobuf Team Bot
7bdbfcd927 Add @ForLiteOnly and Lite-only Javadoc to Lite-only classes.
PiperOrigin-RevId: 954642441
2026-07-27 08:27:14 -07:00
Protobuf Team Bot
d03f760689 internal changes
PiperOrigin-RevId: 953407098
2026-07-24 09:33:35 -07:00
Tony Liao
d6fc95d0c3 (Second try) Cache the custom JSON names of enum fields upon first lookup.
Benchmark                                                     Mode  Cnt    Score   Error  Units
JsonFormatBenchmark.benchmarkParseJsonCustom                  avgt   50    3.974 ± 0.037  us/op
JsonFormatBenchmark.benchmarkParseJsonDefault                 avgt   50    3.837 ± 0.114  us/op
JsonFormatBenchmark.benchmarkParseJsonUnknown                 avgt   50   33.699 ± 0.256  us/op
JsonFormatBenchmark.benchmarkParseJsonUnknownIgnored          avgt   50   26.216 ± 0.167  us/op
JsonFormatBenchmark.benchmarkParseRepeatedJsonCustom          avgt   50   20.698 ± 0.457  us/op
JsonFormatBenchmark.benchmarkParseRepeatedJsonDefault         avgt   50   12.277 ± 0.112  us/op
JsonFormatBenchmark.benchmarkParseRepeatedJsonUnknownIgnored  avgt   50  364.327 ± 5.504  us/op

Benchmark                                                     Mode  Cnt    Score   Error  Units
JsonFormatBenchmark.benchmarkParseJsonCustom                  avgt   50    5.049 ± 0.057  us/op
JsonFormatBenchmark.benchmarkParseJsonDefault                 avgt   50    3.879 ± 0.072  us/op
JsonFormatBenchmark.benchmarkParseJsonUnknown                 avgt   50   33.157 ± 0.378  us/op
JsonFormatBenchmark.benchmarkParseJsonUnknownIgnored          avgt   50   26.461 ± 0.151  us/op
JsonFormatBenchmark.benchmarkParseRepeatedJsonCustom          avgt   50   14.664 ± 0.085  us/op
JsonFormatBenchmark.benchmarkParseRepeatedJsonDefault         avgt   50   12.230 ± 0.101  us/op
JsonFormatBenchmark.benchmarkParseRepeatedJsonUnknownIgnored  avgt   50  350.246 ± 5.642  us/op

PiperOrigin-RevId: 949670944
2026-07-17 11:26:05 -07:00
Protobuf Team Bot
65429e343d Pre-size collections when parsing packed repeated fields to avoid over-allocation.
PiperOrigin-RevId: 949610352
2026-07-17 09:29:58 -07:00
Tony Liao
6ef18f6071 Automated rollback of commit 7db08e8f1b.
PiperOrigin-RevId: 949606118
2026-07-17 09:18:44 -07:00
cpovirk
927dead4c8 Delete unused refillCallback and associated types.
They were introduced along with a Google-internal usage in cl/47874747. That usage was removed in cl/791705426.

PiperOrigin-RevId: 949521950
2026-07-17 06:03:18 -07:00
Tony Liao
6155df9cca Additional unit tests verifying ignoringUnknownFields behavior on non-string primitives.
PiperOrigin-RevId: 949064971
2026-07-16 11:07:22 -07:00
Sandy Zhang
42b1e8ed8e internal change
PiperOrigin-RevId: 949045769
2026-07-16 10:30:48 -07:00
Protobuf Team Bot
caa55cbeb3 Further cleanup of experimental runtime.
PiperOrigin-RevId: 948949657
2026-07-16 07:04:15 -07:00
Protobuf Team Bot
48a0e0936e Open-source the ProGuard assumeLiteRuntime=true rule.
Open-source Protobuf users should benefit from bypassing full runtime inheritance checks in the lite runtime on startup paths.

PiperOrigin-RevId: 948783493
2026-07-16 00:03:59 -07:00
Protobuf Team Bot
d781b31418 Optimize GeneratedMessageLite.isMutable.
Checking if the high bit is set is equivalent to checking if the value is negative. This change avoids a bitwise AND instruction in the generated assembly.

Before/After:

```
boolean FizzBuzz.isMutable() [20 bytes]
    0x000040c0    ldr w0, [x1, #12]
    0x000040c4    and w0, w0, #0x80000000   <--- THIS INSTRUCTION CAN GO
    0x000040c8    cmp w0, #0x0 (0)
    0x000040cc    cset w0, ne
    0x000040d0    ret

boolean FizzBuzz.isMutable2() [16 bytes]
    0x000040e0    ldr w0, [x1, #12]
    0x000040e4    cmp w0, #0x0 (0)
    0x000040e8    cset w0, lt
    0x000040ec    ret
```

PiperOrigin-RevId: 948755487
2026-07-15 22:53:35 -07:00
Protobuf Team Bot
dbe1410096 Split large mergeFrom methods in Java immutable builders into smaller sub-methods.
PiperOrigin-RevId: 948612144
2026-07-15 16:35:11 -07:00
Protobuf Team Bot
1a06ad89e7 Optimize GeneratedMessage.getAllFields by using fields sorted by number.
PiperOrigin-RevId: 948474555
2026-07-15 12:14:18 -07:00
Protobuf Team Bot
6128a90a69 Improve length checks in JavaLite in the face of overlong length prefixes.
PiperOrigin-RevId: 948256971
2026-07-15 04:35:56 -07:00