Skip to content

lib/protocol: add FileInfo.WireSize to avoid protobuf allocations - #10707

Closed
mattn wants to merge 2 commits into
syncthing:mainfrom
mattn:perf/fileinfo-wire-size
Closed

mattn wants to merge 2 commits into
syncthing:mainfrom
mattn:perf/fileinfo-wire-size

Conversation

@mattn

@mattn mattn commented May 24, 2026 •

Copy link
Copy Markdown
Contributor

Purpose

FileInfoBatch.Append and encryptionTrailerSize only need the serialized size of a FileInfo, but used proto.Size(f.ToWire(...)), which allocates a full wire message just to measure it. On the index batching and encrypted puller paths this is visible in allocation profiles.

This PR adds FileInfo.WireSize(withInternalFields bool) int that walks fields directly and sums protobuf tag/varint/length-prefix sizes without allocating an intermediate message, and switches both hot callers to it.

The change is split into two commits so the benchmark can be run on each to compare:

  • lib/protocol: add benchmark for FileInfo wire size
  • lib/protocol: avoid protobuf allocations when sizing FileInfo

Testing

TestFileInfoWireSizeMatchesProtoSize cross-checks WireSize against proto.Size(ToWire(...)) over representative FileInfo shapes (empty, full file with blocks/xattrs, symlink with internal flags), with and without internal fields.

The two commits on this branch are arranged so that BenchmarkFileInfoSize measures the old path at the first commit and the new path at the second. To reproduce:

# before
git checkout <branch>~1   # "lib/protocol: add benchmark for FileInfo wire size"
go test ./lib/protocol/ -run XXX -bench=BenchmarkFileInfoSize -benchmem -count=8 -benchtime=2s > before.txt

# after
git checkout <branch>     # "lib/protocol: avoid protobuf allocations when sizing FileInfo"
go test ./lib/protocol/ -run XXX -bench=BenchmarkFileInfoSize -benchmem -count=8 -benchtime=2s > after.txt

benchstat before.txt after.txt

Result on AMD Ryzen 7 7735HS (8 runs):

                │   before    │              after              │
                │   sec/op    │   sec/op     vs base            │
FileInfoSize-16   2919.0n ±1%   340.7n ±3%   -88.33% (p=0.000)

                │    B/op     │    B/op      vs base            │
FileInfoSize-16   3.641Ki ±0%   0.000Ki ±0% -100.00% (p=0.000)

                │  allocs/op  │  allocs/op   vs base            │
FileInfoSize-16     44.00 ±0%     0.00 ±0%  -100.00% (p=0.000)

Documentation

Not applicable — internal helper, no user-visible or wire-protocol change.

Authorship

Your name and email will be added automatically to the AUTHORS file based on the commit metadata.

Measures the current proto.Size(ToWire(...)) path so the follow-up
allocation-free implementation can be compared against it.

Signed-off-by: Yasuhiro Matsumoto <[email protected]>
@mattn
mattn force-pushed the perf/fileinfo-wire-size branch from 9e35cf1 to 5cf4253 Compare May 24, 2026 16:40
Add FileInfo.WireSize that walks the fields directly and sums protobuf
tag and varint lengths without allocating an intermediate wire message.
A cross-check test asserts it matches proto.Size(ToWire(...)) over
representative FileInfo shapes.

Switch the hot callers (FileInfoBatch.Append and encryptionTrailerSize)
to the new method.

Signed-off-by: Yasuhiro Matsumoto <[email protected]>
@mattn
mattn force-pushed the perf/fileinfo-wire-size branch from 5cf4253 to 8901f09 Compare May 24, 2026 16:44
@calmh

calmh commented May 25, 2026

Copy link
Copy Markdown
Member

Hmm, I'm skeptical. Not enamoured with the idea of maintaining a separate copy of protobuf encoding rules, and easy for this to drift out of sync as the types in question evolve.

@imsodin

imsodin commented May 25, 2026 •

Copy link
Copy Markdown
Member

An alternative might be to just drop the wire size threshold - I don't think it carries its weight anymore: For many usecases of this batch it's largely irrelevant aka those writing to DB not to a connection. And if we want to keep the max. wire size per batch on the connection, we can check and split the batch at the protocol layer after we converted to wire format and thus get the size "for free" with .ProtoSize().

@calmh

calmh commented May 25, 2026 •

Copy link
Copy Markdown
Member

For the batching, I think it makes sense to keep the message size reasonable, at least to the point that if we've built up a reasonably sized batch there is no advantage to delaying further and we should just send it, even if it's only two (large) files.

But this doesn't require any kind of precise size metric; we could equally well say that a batch is (e.g.) 250 files or 10000 blocks, and that'll cover the same purpose with a trivial int counter.

(For the encryption trailer we do need the precise size I think, but this happens just once in relation to a file operation that will get fsync()ed etc so I think the performance aspect is irrelevant more or less.)

calmh added a commit that referenced this pull request Jul 1, 2026
We used a size computation to generate reasonably sized batches, but the
ProtoSize call is fairly expensive as it requires a conversion to a wire
type etc. We don't need that much precision. Instead, just limit to 1000
files or 5000 blocks, which is likely approximately that much data
anyway.

Closes #10707

Signed-off-by: Jakob Borg <[email protected]>
@calmh calmh closed this in #10776 Jul 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants