Page MenuHomeFreeBSD

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

Authored by kevans on May 29 2026, 11:43 PM.
Tags
None
Referenced Files
Unknown Object (File)
Mon, Aug 3, 8:59 AM
Unknown Object (File)
Sat, Aug 1, 7:46 AM
Unknown Object (File)
Sat, Aug 1, 5:22 AM
Unknown Object (File)
Fri, Jul 31, 7:12 AM
Unknown Object (File)
Mon, Jul 27, 10:05 AM
Unknown Object (File)
Tue, Jul 21, 7:03 PM
Unknown Object (File)
Sat, Jul 18, 8:48 AM
Unknown Object (File)
Sat, Jul 18, 7:39 AM
Subscribers

Details

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 Not Applicable
Unit
Tests Not Applicable

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
76
81

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
lib/libc/stdio/open_memstream.c
60

I think it would be clearer (and avoid the need for the comment below about leaving newsize unchanged as it would now be more obvious to the reader) if you renamed this to newlen and then you could call growsize below newsize.

80
This revision was automatically updated to reflect the committed changes.
kevans marked 3 inline comments as done.