SIGN IN SIGN UP

Optimize JSONMessageCodec UTF-8 conversion (#190529)

`JSONMessageCodec` currently serializes values to a JSON `String` and
then passes that string through `StringCodec` for UTF-8 conversion.
Decoding performs the reverse sequence.

This change uses cached JSON/UTF-8 converters directly, reducing
conversion work and temporary allocations while preserving the existing
wire format. `JSONMethodCodec` also benefits because it delegates its
encoding and decoding to `JSONMessageCodec`.

This affects framework channels such as text input
(`TextField`/`EditableText`), clipboard and haptics, system UI,
navigation, localization, and keyboard/system events. It does not affect
`StandardMessageCodec` or standalone `jsonEncode`/`jsonDecode` calls.

Median of nine samples comparing the old and new implementations in the
same process:

| Environment and payload | Encode | Decode |
| --- | ---: | ---: |
| macOS arm64 AOT, 186 bytes | 1.03 → 0.75 µs (-27%) | 0.58 → 0.42 µs
(-29%) |
| macOS arm64 AOT, 4.15 KB | 39.4 → 25.6 µs (-35%) | 29.4 → 15.3 µs
(-48%) |
| Physical iPad, AOT profile, 186 bytes | 1.35 → 0.98 µs (-28%) | 0.79 →
0.57 µs (-28%) |
| Physical iPad, AOT profile, 4.15 KB | 48.2 → 30.7 µs (-36%) | 36.4 →
20.1 µs (-45%) |

Also added a regression test that verifies the exact UTF-8 wire format,
a four-byte Unicode character crossing the encoder’s default 256-byte
chunk boundary, and decoding from a bounded `ByteData` view. The codec
tests passed on both the Dart VM and Chrome, along with the related
text-input/platform-channel tests and analysis.

## Pre-launch Checklist

- [x] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [x] I read the [AI contribution guidelines] and understand my
responsibilities, or I am not using AI tools.
- [x] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [x] I read and followed the [Flutter Style Guide], including [Features
we expect every widget to implement].
- [x] I signed the [CLA].
- [ ] I listed at least one issue that this PR fixes in the description
above.
- [x] I updated/added relevant documentation (doc comments with `///`).
- [x] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [x] I followed the [breaking change policy] and added [Data Driven
Fixes] where supported.
- [x] All existing and new tests are passing.
F
FelixMittermeier committed
4bdb8c61751a5d5da86753f45ed3c7603a61e1c0
Parent: f8d5141
Committed by GitHub <noreply@github.com> on 8/20/2026, 9:39:56 PM