Page MenuHomeFreeBSD

stdio: *memstream: grow the buffer by 1.5x on write
AcceptedPublic

Authored by kevans on May 29 2026, 11:43 PM.
Tags
None
Referenced Files
Unknown Object (File)
Sat, Jun 27, 2:06 PM
Unknown Object (File)
Mon, Jun 22, 11:54 PM
Unknown Object (File)
Wed, Jun 17, 3:32 AM
Unknown Object (File)
Tue, Jun 16, 11:33 AM
Unknown Object (File)
Jun 13 2026, 9:56 AM
Unknown Object (File)
Jun 12 2026, 11:52 PM
Unknown Object (File)
Jun 12 2026, 7:18 PM
Unknown Object (File)
Jun 10 2026, 12:35 PM
Subscribers

Details

Reviewers
jhb
des
Group Reviewers
Klara
Summary

This improves performance by reducing the number of allocations as we
write into the memstream, both in the fully buffered case with larger
memstreams and also more trivially in the line- and un-buffered case as
they flush back to the underlying buffer more often.

The inspiration for this was taken from Apple's implementation in
https://github.com/apple-oss-distributions/libc, but expanded to include
wmemstream for consistency. I've added a test for the bug that I hit in
libder that caused me to notice this in the first place, and fixed that
bug in this version.

Sponsored by: Klara, Inc.

Diff Detail

Repository
rG FreeBSD src repository
Lint
Lint Skipped
Unit
Tests Skipped
Build Status
Buildable 73691
Build 70574: arc lint + arc unit

Event Timeline

des requested changes to this revision.Jun 3 2026, 1:07 PM
des added a subscriber: des.
des added inline comments.
lib/libc/stdio/open_memstream.c
35
70
75

you can replace this entire thing with just ckd_add(&growsize, ms->size, ms->size / 2)

lib/libc/tests/stdio/open_memstream2_test.c
191

I'd prefer including a description if it's not too much trouble

223–225
This revision now requires changes to proceed.Jun 3 2026, 1:07 PM
kevans marked 3 inline comments as done.

Review feedback: use ckd_add() and add test descriptions

des added inline comments.
lib/libc/stdio/open_memstream.c
35

I don't think you actually need <stdbool.h> here

This revision is now accepted and ready to land.Jun 5 2026, 7:30 AM