Commit graph

1614 commits

Author SHA1 Message Date
Protobuf Team Bot
706fc1e3cb Optimize SmallSortedMap to improve getAllFields() efficiency.
PiperOrigin-RevId: 944593820
2026-07-08 11:08:58 -07:00
Mark Hansen
5759717273 Automated rollback of commit 42b411e7c9.
PiperOrigin-RevId: 944198382
2026-07-07 18:19:12 -07:00
Protobuf Team Bot
42b411e7c9 Optimize Java Lite Parser Registry with Centralized Cache
Avoid generating double-checked locking inside every generated message class's
dynamicMethod(GET_PARSER) by centralizing the parser instantiation and caching
in a thread-safe map inside GeneratedMessageLite.

This saves substantial DEX and ODEX size for apps with many protobuf classes.

Note: This change requires the generated code to run against a runtime of at
least the same version. Mixing new generated code with an older runtime version
is not supported (see https://protobuf.dev/support/cross-version-runtime-guarantee/).
PiperOrigin-RevId: 944139195
2026-07-07 15:57:19 -07:00
Rachel Goldfinger
d5e039a302 Make Timestamps.parse() attempt non-lenient parsing first and warning if this fails.
PiperOrigin-RevId: 944126931
2026-07-07 15:34:12 -07:00
Protobuf Team Bot
5e24bccb66 Auto-generate files after cl/944116273 2026-07-07 22:24:32 +00:00
Jason Aragorn Tobias Lunn
636ae747e6 Update our compiled_edition_default's maximum_edition to 2026
PiperOrigin-RevId: 944116273
2026-07-07 15:13:49 -07:00
Rachel Goldfinger
ab69831380 Add a warning for potential OneofDescriptor collisions prior to Q1 2027 breaking changes.
PiperOrigin-RevId: 943277587
2026-07-06 07:37:05 -07:00
Protobuf Team Bot
e1b94ce8f9 Optimize MapEntry allocations during map serialization
PiperOrigin-RevId: 941708409
2026-07-02 08:16:13 -07:00
Tobias Werth
3907d14b7d Automated Code Change
PiperOrigin-RevId: 941537305
2026-07-02 01:08:39 -07:00
Protobuf Team Bot
06076f5c2c Check MessageLite.class.isAssignableFrom() always, not just in the fallback path.
PiperOrigin-RevId: 941284190
2026-07-01 14:07:32 -07:00
Liam Miller-Cushon
6fc9d438b7 Remove 'The descriptor for type ... cannot be found and a placeholder is created for it' logs
PiperOrigin-RevId: 939963336
2026-06-29 12:17:15 -07:00
Protobuf Team Bot
50d0d1bf0f Document that DiscardUnknownFieldsParser does not discard unrecognized closed enum values.
Fixes https://github.com/protocolbuffers/protobuf/issues/23019

PiperOrigin-RevId: 938127796
2026-06-25 12:27:07 -07:00
Tristan
c56cd5b441 Reject negative size in mergeDelimitedFrom (#28097)
## Summary

`mergeDelimitedFrom` passes the return value of `readRawVarint32` directly to `LimitedInputStream` without checking for negative values. For 5-byte varints encoding values >= 2^31, `readRawVarint32` returns a negative int (e.g., `\x80\x80\x80\x80\x08` decodes to -2147483648).

`LimitedInputStream` with a negative limit immediately returns EOF (`if (limit <= 0) return -1`), causing the parser to produce a default/empty message and return `true` (success). The actual message body bytes remain unconsumed in the stream, permanently desynchronizing subsequent `parseDelimitedFrom` calls.

## Fix

Add `if (size < 0) throw InvalidProtocolBufferException.negativeSize()` before constructing `LimitedInputStream`.

## Test plan

- [x] Verified with PoC: 5-byte varint prefix encoding 2^31 previously produced empty message + stream desync; after fix, `negativeSize` exception thrown
- [ ] Existing unit tests pass

Closes #28097

COPYBARA_INTEGRATE_REVIEW=https://github.com/protocolbuffers/protobuf/pull/28097 from TristanInSec:fix/merge-delimited-negative-size 7ccb18676e
PiperOrigin-RevId: 937547094
2026-06-24 14:07:37 -07:00
Protobuf Team Bot
6aacd4ddc1 Improve the behavior of StreamDecoder in the face of large ints on the wire.
PiperOrigin-RevId: 937496635
2026-06-24 12:36:50 -07:00
Protobuf Team Bot
3245bdb028 Small optimizations to Java JsonFormat.
- Apply a fast path to Uint64 parse where we try to parse it directly before falling back to the BigDecimal path. This optimization is preexisting on int64 and int32 and uint32 but was missed on Uint64

- Avoid doing out.append(value.subSequence(start, end)) which forces an intermediate copy the string, instead use the out.append(value, start, end)

PiperOrigin-RevId: 937290790
2026-06-24 06:14:18 -07:00
Liam Miller-Cushon
974e76e820 Replace getClass() null checks with Objects.requireNonNull in java lite gencode
PiperOrigin-RevId: 934860264
2026-06-19 04:37:16 -07:00
Liam Miller-Cushon
31ef11cb59 Update Java protobuf generator to use Objects.requireNonNull
PiperOrigin-RevId: 934288237
2026-06-18 05:25:52 -07:00
petedmarsh
16b949d98a Json format lienent tests (#27622)
Because JsonFormat uses GSON to parse JSON it has a variety of
leniencies, e.g. allowing keys and values to skip quotes:

```
{hello: world}
```

These behaviours are not part of the ProtoJSON format but they are
behaviours that JsonFormat's parser has. It's possible someone in the
wild depends upon these unknowingly. This adds explicit tests for these
behaviours to make sure that they are not accidentally lost in future.

Closes #27622

COPYBARA_INTEGRATE_REVIEW=https://github.com/protocolbuffers/protobuf/pull/27622 from petedmarsh:json-format-lienent-tests edebd543cd
PiperOrigin-RevId: 933849754
2026-06-17 12:03:07 -07:00
Protobuf Team Bot
1d1c1b9f61 Remove unused nextCalledBeforeRemove from SmallSortedMap, as the code path does not exist in the internal implementation, and only immutable maps will be exposed to users.
PiperOrigin-RevId: 932635956
2026-06-15 13:29:47 -07:00
Protobuf Team Bot
ea0e67a9ca Restore the prior behavior of gencode catches for now; while it did drink the gencode it caused a regression in some JIT behavior.
No releases were cut with this change so it is safe to remove.

PiperOrigin-RevId: 932423133
2026-06-15 06:29:39 -07:00
Protobuf Team Bot
261f4cabf1 Optimize ArrayDecoder limit handling (again)
PiperOrigin-RevId: 932410076
2026-06-15 06:03:07 -07:00
Protobuf Team Bot
6beea4e08a Remove unused Fieldset descendingIterator, which saves us some allocations
PiperOrigin-RevId: 931358824
2026-06-12 15:20:28 -07:00
Protobuf Team Bot
7ab792b969 Add link to the page which explains that deterministic != canonical on our apis which let you set determinsitic serialization
PiperOrigin-RevId: 930485402
2026-06-11 06:20:08 -07:00
Protobuf Team Bot
5f91a74c50 Fix edge case on recursion limit for groups in a Lite parse path.
Note that as this case is only reachable for known groups, its not a usable primitive for fully unconstrained recursion, since even if a recursive schema did use a group it would call back to a message between, meaning that even in such an obscure case this would only unlock reaching depth 200 not unconstrained.

PiperOrigin-RevId: 930082333
2026-06-10 14:36:34 -07:00
Protobuf Team Bot
acb6c197da Remove defunct Reader and Writer interfaces from JavaProto
These interfaces were part of the experimental runtime, and before this change have exactly one concrete implementation each. This CL simply deletes the interfaces and inlines the corresponding concrete type wherever they were named.

PiperOrigin-RevId: 930019405
2026-06-10 12:48:25 -07:00
Protobuf Team Bot
7caffc11ee Partial rollforward of cl/928600418: merge HeapNioEncoder into ArrayEncoder.
The previous change looked to get down to exactly 2 CodedOutputStream concrete classes: unfortunately some users are relying on the incidental-but-reliable behavior that CodedOutputStreams with ByteBuffers do immediately push the content without a flush() but that the position update does not apply until you call flush(). This oddity has been incidentally/accidentally true for both Heap and Direct ByteBuffer cases even though they didn't use a common codepath.

To move incrementally forward, we can merge in HeapNio into ArrayEncoder without touching DirectNio. For now this still leaves 3  totalconcrete classes, but it will achieve the 'exactly 2 concrete classes loaded' for any server if they never reach DirectNioEncoder or never reach the OutputStreamEncoder.

PiperOrigin-RevId: 929337270
2026-06-09 12:24:57 -07:00
Protobuf Team Bot
964fe23b18 Restrict Protobuf.java methods to GeneratedMessageLite as part of experimental runtime turndown.
This requires sprinkling some casts around locally, mainly surrounding the mismatch of apis that return `MessageLite` which on LiteRuntime is guaranteed to be `GeneratedMessageLite`, but statically thats not known because Full messages also implement it.

PiperOrigin-RevId: 928716180
2026-06-08 12:44:48 -07:00
Protobuf Team Bot
f770097e46 Optimize JsonFormat handling of Struct/Value by casting gencode accessors instead of reflection.
This still retains the reflection path for these types so that it will continue to handle DynamicMessage printing correctly.

PiperOrigin-RevId: 928689274
2026-06-08 11:51:02 -07:00
Protobuf Team Bot
5d01f7bd22 Optimize Java JsonFormat string printing path.
- Use a lookup table for the replacement characters in the ascii range (which is all of the characters that gson escapes except for exactly 2).

- Stream the escapes out ourselves instead of going through gson.toJson(val.toString()) which goes through a lot of heavy gson machinery and makes several copies of the string.

We exactly match the choices that gson made for escapes here to make this an implementation detail change with no observable difference.

PiperOrigin-RevId: 928654085
2026-06-08 10:51:44 -07:00
Protobuf Team Bot
b9e2715850 Automated rollback of commit 2db709e0a0.
PiperOrigin-RevId: 928600418
2026-06-08 09:18:26 -07:00
Protobuf Team Bot
2db709e0a0 Reduce CodedOutputStream concrete subclasses to 2
The purpose of this change is guarantee the JIT can perform always bimorphization is possible in all paths which are handling a CodedOutputStream, which can in turn unlock significant other optimization wins.

PiperOrigin-RevId: 928539681
2026-06-08 07:00:00 -07:00
Protobuf Team Bot
b6c2e87291 Automated Code Change
PiperOrigin-RevId: 928516242
2026-06-08 06:03:39 -07:00
Protobuf Team Bot
07454526e2 Automated Code Change
PiperOrigin-RevId: 928508944
2026-06-08 05:43:33 -07:00
Stefan Haustein
71263ce02d Internal change
PiperOrigin-RevId: 928436147
2026-06-08 02:40:47 -07:00
Protobuf Team Bot
0d1d7734eb Rename SafeProcessor/UnsafeProcessor to MobileProcessor/ServerProcessor in Utf8.java
It used to be that the UnsafeProcessor was named that because it used sun.misc.Unsafe, and SafeProcessor did not.

However, over time as optimizations shifted the dominant difference was actually that UnsafeProcessor was targeting serverside and SafeProcess targeted Android (even though we otherwise heavily use sun.misc.Unsafe on Android).

Since we no longer sun.misc.Unsafe in either path now, its time to change the names.

PiperOrigin-RevId: 927336258
2026-06-05 09:33:47 -07:00
Protobuf Team Bot
45c6cbedda Add @SuppressWarnings("removal") to Lite-only classes that will still use sun.misc.Unsafe for now.
PiperOrigin-RevId: 927301764
2026-06-05 08:03:13 -07:00
Protobuf Team Bot
3de8c73465 Remove dead BinaryReader.java
This was part of the experimental Java runtime, which is now defunct.

PiperOrigin-RevId: 927300718
2026-06-05 07:59:35 -07:00
Protobuf Team Bot
dd543428c7 Automated Code Change
PiperOrigin-RevId: 927243592
2026-06-05 05:44:19 -07:00
Protobuf Team Bot
94de7ab2b3 Introduce getMessageExtensionOrNull for extendable messages to avoid the code pattern of hasExtension() ? getExtension : null which takes an extra extension lookup.
PiperOrigin-RevId: 926787242
2026-06-04 11:17:43 -07:00
Protobuf Team Bot
791fbe2249 Support merging lazy fields with different default instances but equal descriptor types; e.g. DynamicMessage
Lazy field should only be applicable to full message, whose `mergeFrom(Message)` supports merging from a different message but with the same descriptor. Therefore, this change abandons the default instance check in the lazy field impl.

Also note that InternalLazyField currently belongs to the lite runtime, so direct access to descriptors is infeasible, thus depending on the mergeFrom overrides to invoke the full merging method.

PiperOrigin-RevId: 926112976
2026-06-03 10:15:18 -07:00
Protobuf Team Bot
50ac9feec5 Micro-optimize latin1Cat() to speed up protobuf descriptor initialization.
We have to break up the descriptors into multiple strings because of the 64k cap on string literals and then join them back up at runtime. This change we avoid building the intermediate joined string before extracting it back as byte[], and instead pre-allocate the total byte[] array first and turns each 64kb segment to byte[] and uses System.arraycopy to copy them directly.

This avoids some pointless work, but also removes garbage copies. Note that its a garbage copy that should be escape-analysis by the JVM and so should be very cheap, but still cheaper not to.

PiperOrigin-RevId: 925969602
2026-06-03 05:56:44 -07:00
Protobuf Team Bot
11f708e186 Shrink gencode parsePartialFrom() by pushing the exception wrapping to a helper.
By outlining the exception handling path, this notably shrinks this method to below 35 bytes in .class bytecode representation, which is one of the magic arbitrary limits that influences JIT inlining behaviors.

PiperOrigin-RevId: 925530732
2026-06-02 13:01:34 -07:00
Protobuf Team Bot
d1e6ad01ec Move the "same instance" check at mergeFrom(MessageLite) into GeneratedMessageLite.
In `AbstractMessageLite.Builder.mergeFrom(MessageLite)`, it either goes into (full runtime) `AbstractMessage` which supports merge from a different instance but the same *descriptor* type, or into the lite runtime path which should not support it in the absence of descriptors .e.g in case of `DynamicMessage`.

PiperOrigin-RevId: 924941126
2026-06-01 15:25:40 -07:00
Protobuf Team Bot
529daaa6e8 Micro-optimize fieldsSortedByNumber to just point an fields normally.
PiperOrigin-RevId: 924916713
2026-06-01 14:34:37 -07:00
Protobuf Team Bot
64d0bca8af Avoid always parsing all lazy extensions on equals()
Today, equals() causes all lazy extensions to be parsed before the equality check starts. After this change, equals() will try to avoid hitting lazy parsing if possible

Notably:
- When the count of extensions don't match, or if "which extensions are set" don't match, we can return false before looking at any extension values.

- When actually comparing extensions, it goes iteratively which means it'll only parse the extensions as it goes; if it finds any one ext which is not-equal it will will bail out and not parse any of the rest.

Note that when the case where two messages are equal, this unavoidably still cause all extensions to parse since it has to examine all fields.

To achieve this does require adding a protected `extensionsEquals()` method for the gencode to call, since the current base class API isn't otherwise powerful enough to do so.

PiperOrigin-RevId: 924903438
2026-06-01 14:12:34 -07:00
Protobuf Team Bot
ae1faeedfd Use natural String comparison instead of UTF-8 ByteString comparison for sorting map keys in JsonFormat.
This only changes the sort order in an obscure edge case of map-keys which sort differently in Utf8 and Utf16, meaning a mix of certain characters that are "very high code point but still single char" and "utf16 surrogate pairs".

PiperOrigin-RevId: 922768203
2026-05-28 07:45:14 -07:00
Protobuf Team Bot
61dfcbc7cf Optimize JsonFormat by handling the newline pretty printing better.
PiperOrigin-RevId: 922127998
2026-05-27 07:56:12 -07:00
Protobuf Team Bot
bca685147b Optimize JavaProto JSON string escaping handling.
PiperOrigin-RevId: 921615825
2026-05-26 12:27:10 -07:00
Protobuf Team Bot
40825a1df7 Add option to enable strict JSON parsing in Protobuf Java JsonFormat.
JsonFormat used 'reader.setLenient(false)' to try to get spec JSON behavior out of GSON. However, this mode was still not actually spec JSON. In ~2024 GSON deprecated setLenient(), made that behavior LEGACY_STRICT and added a new level STRICT which is intended to be spec.

Switching unilaterally would be a breaking change for Protobuf: this change just adds the option for users to ask us to use GSON in strict mode. At a later date we will consider flipping the default of this (letting people opt into LEGACY_STRICT if they want to).

PiperOrigin-RevId: 921432331
2026-05-26 06:39:41 -07:00
Protobuf Team Bot
547b9d505a Clean up remaining defunct experimental runtime JavaFull runtime schema classes.
These paths were in support of the experimental runtime and are not reachable by any exposed public APIs today.

PiperOrigin-RevId: 919828913
2026-05-22 13:21:43 -07:00