Skip to content

Commit fdee538

Browse files
authored
Merge pull request #92 from BuddhiLW/fix/stdin-reader-compact
fix(gc): return memory in long-running stdin servers (bb-mcp)
2 parents 7e62326 + b548b76 commit fdee538

9 files changed

Lines changed: 143 additions & 40 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@ first stable `1.0.0` tag; pre-1.0 `alpha` / `rc` tags may still change surfaces.
99

1010
### Fixed
1111

12+
- **A long-running server that reads stdin now gives memory back.** The
13+
`*in*` and `System/in` readers kept every byte they had ever read; they now
14+
drop what has been consumed. While the main thread waits on stdin, each idle
15+
second runs a collect (when at least 1 MiB was allocated since the last one)
16+
and returns freed memory to the OS, so garbage left by `future`s and worker
17+
threads no longer waits for the next request. Socket and `StringBuilder`
18+
buffers now count toward the collect trigger. A bb-mcp-shaped probe (20
19+
`future`s each building 4 MB of JSON, then idle) settles at 129 MB instead
20+
of holding 955 MB.
21+
1222
- **A `wasm/call` on the JIT no longer costs time proportional to the
1323
module.** zwasm resolved the export by re-parsing the module on every call;
1424
it now reads the export table built at compile. A trivial export of a 47 KB

‎src/runtime/cljw/net/socket.zig‎

Lines changed: 10 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,13 @@ fn connectFn(rt: *Runtime, env: *Env, args: []const Value, loc: SourceLocation)
109109
return error_catalog.raise(.net_connect_failed, loc, .{ .host = host, .port = port_i });
110110
};
111111

112+
return wrapStream(rt, stream);
113+
}
114+
115+
/// A socket Value over the connected `stream`, in a fresh `SocketBox`. The
116+
/// box's buffers are charged to the collect trigger, since the only cell
117+
/// standing for them is a few words. Closes `stream` if the box cannot be made.
118+
fn wrapStream(rt: *Runtime, stream: std.Io.net.Stream) !Value {
112119
const box = rt.gpa.create(SocketBox) catch |e| {
113120
stream.close(rt.io);
114121
return e;
@@ -124,6 +131,7 @@ fn connectFn(rt: *Runtime, env: *Env, args: []const Value, loc: SourceLocation)
124131
};
125132
box.reader = box.stream.reader(box.io, &box.rbuf);
126133
box.writer = box.stream.writer(box.io, &box.wbuf);
134+
rt.gc.noteOwnedAlloc(@sizeOf(SocketBox));
127135

128136
return host_instance.alloc(rt, &descriptor, .{ @intFromPtr(box), 0, 0, 0 });
129137
}
@@ -313,23 +321,7 @@ fn acceptMethod(rt: *Runtime, env: *Env, args: []const Value, loc: SourceLocatio
313321
const stream = safepoint.blocking(std.Io.net.Server.accept, .{ &box.server, box.io }) catch
314322
return error_catalog.raise(.net_io_failed, loc, .{ .op = "accept" });
315323

316-
const sock = rt.gpa.create(SocketBox) catch |e| {
317-
stream.close(rt.io);
318-
return e;
319-
};
320-
sock.* = .{
321-
.io = rt.io,
322-
.stream = stream,
323-
.reader = undefined,
324-
.writer = undefined,
325-
.closed = false,
326-
.rbuf = undefined,
327-
.wbuf = undefined,
328-
};
329-
sock.reader = sock.stream.reader(sock.io, &sock.rbuf);
330-
sock.writer = sock.stream.writer(sock.io, &sock.wbuf);
331-
332-
return host_instance.alloc(rt, &descriptor, .{ @intFromPtr(sock), 0, 0, 0 });
324+
return wrapStream(rt, stream);
333325
}
334326

335327
/// `(.port srv)` — the BOUND port. `listen` with 0 resolves an ephemeral port
@@ -433,23 +425,7 @@ fn connectUnixFn(rt: *Runtime, env: *Env, args: []const Value, loc: SourceLocati
433425
const stream = safepoint.blocking(std.Io.net.UnixAddress.connect, .{ &ua, rt.io }) catch
434426
return error_catalog.raise(.net_unix_connect_failed, loc, .{ .path = path });
435427

436-
const box = rt.gpa.create(SocketBox) catch |e| {
437-
stream.close(rt.io);
438-
return e;
439-
};
440-
box.* = .{
441-
.io = rt.io,
442-
.stream = stream,
443-
.reader = undefined,
444-
.writer = undefined,
445-
.closed = false,
446-
.rbuf = undefined,
447-
.wbuf = undefined,
448-
};
449-
box.reader = box.stream.reader(box.io, &box.rbuf);
450-
box.writer = box.stream.writer(box.io, &box.wbuf);
451-
452-
return host_instance.alloc(rt, &descriptor, .{ @intFromPtr(box), 0, 0, 0 });
428+
return wrapStream(rt, stream);
453429
}
454430

455431
/// `(cljw.net/listen-unix path)` — bind and listen on a UNIX domain socket.

‎src/runtime/gc/gc_heap.zig‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ pub const GcHeap = struct {
150150
pub const retainBytesAfterCollect = trigger.retainBytesAfterCollect;
151151
pub const maybeAutoCollect = trigger.maybeAutoCollect;
152152
pub const requestCollect = trigger.requestCollect;
153+
pub const collectBeforeIdle = trigger.collectBeforeIdle;
153154

154155
pub fn init(infra: std.mem.Allocator) GcHeap {
155156
const owner = root_set_mod.currentThreadId();

‎src/runtime/gc/gc_trigger.zig‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
//! so callers write `rt.gc.noteOwnedAlloc(n)`.
1818

1919
const std = @import("std");
20+
const builtin = @import("builtin");
2021
const gc_heap_mod = @import("gc_heap.zig");
2122
const root_set_mod = @import("root_set.zig");
2223

@@ -132,6 +133,28 @@ pub fn requestCollect(gc: *GcHeap) void {
132133
collectIfSafe(gc);
133134
}
134135

136+
/// Allocation pressure below which `collectBeforeIdle` does nothing, so an
137+
/// idle loop that allocates little pays no mark.
138+
pub const idle_collect_min_bytes: usize = 1024 * 1024;
139+
140+
/// Collect while the mutator waits for input (a server between requests),
141+
/// then hand freed malloc memory back to the OS. A no-op below
142+
/// `idle_collect_min_bytes` of pressure. Same safety gates and page
143+
/// release as `requestCollect`.
144+
pub fn collectBeforeIdle(gc: *GcHeap) void {
145+
if (pressureBytes(gc) < idle_collect_min_bytes) return;
146+
requestCollect(gc);
147+
trimProcessHeap();
148+
}
149+
150+
extern "c" fn malloc_trim(pad: usize) c_int;
151+
152+
/// Return glibc's free arena memory to the OS. Other allocators unmap large
153+
/// frees themselves, so this is a no-op off glibc.
154+
fn trimProcessHeap() void {
155+
if (comptime builtin.link_libc and builtin.target.abi.isGnu()) _ = malloc_trim(0);
156+
}
157+
135158
/// True iff a stop-the-world collect may run on this thread right now: no
136159
/// multi-alloc builder is mid-bracket (its intermediate nodes are
137160
/// live-but-unrooted), this is not a registered worker, the collector is not

‎src/runtime/io/host_stream.zig‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ fn readByte(rt: *Runtime, env: *Env, args: []const Value, loc: SourceLocation) a
115115
/// stdin mode: append one blocking chunk from process stdin to `data`.
116116
/// A zero-byte read (or error) latches EOF. Mirrors text_io's `*in*` fill.
117117
fn stdinFill(rt: *Runtime, st: *StreamState) !void {
118+
stdin.beforeFill(rt.gc.infra, &st.data, &st.pos, &rt.gc);
118119
var chunk: [4096]u8 = undefined;
119120
const n = stdin.readChunk(rt.io, &chunk) catch {
120121
st.stdin_eof = true;

‎src/runtime/io/stdin.zig‎

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,89 @@
33
//! `*in*` readers (text_io, host_stream), via `std.Io.File`.
44

55
const std = @import("std");
6+
const builtin = @import("builtin");
67

78
/// Read up to `buf.len` bytes from stdin into `buf`. Returns bytes read
89
/// (0 = EOF).
910
pub fn readChunk(io: std.Io, buf: []u8) std.Io.File.ReadStreamingError!usize {
1011
return std.Io.File.stdin().readStreaming(io, &.{buf});
1112
}
13+
14+
/// How long a stdin fill waits for input before running an idle collect.
15+
pub const idle_poll_ms: i32 = 1000;
16+
17+
/// Wait up to `timeout_ms` for stdin to become readable (data, EOF or
18+
/// hang-up). True when a read will not block. Answers true at once where
19+
/// there is no `poll`, so callers never spin.
20+
pub fn waitReadable(timeout_ms: i32) bool {
21+
if (comptime builtin.os.tag == .wasi or builtin.os.tag == .windows) return true;
22+
var fds = [_]std.posix.pollfd{.{
23+
.fd = std.posix.STDIN_FILENO,
24+
.events = std.posix.POLL.IN,
25+
.revents = 0,
26+
}};
27+
const n = std.posix.poll(&fds, timeout_ms) catch return true;
28+
return n > 0;
29+
}
30+
31+
/// Ready a demand-filled stdin buffer for its next fill: drop its consumed
32+
/// prefix, then wait for input, calling `gc.collectBeforeIdle()` each time
33+
/// `idle_poll_ms` passes with none, so a process blocked on stdin still
34+
/// collects and returns memory.
35+
pub fn beforeFill(infra: std.mem.Allocator, data: *std.ArrayList(u8), pos: *usize, gc: anytype) void {
36+
dropConsumed(infra, data, pos);
37+
while (!waitReadable(idle_poll_ms)) gc.collectBeforeIdle();
38+
}
39+
40+
/// Capacity up to which `dropConsumed` never frees a stdin buffer's excess, so
41+
/// a steady stream of small lines does not realloc per fill.
42+
pub const retained_capacity: usize = 64 * 1024;
43+
44+
/// Drop the consumed prefix `data[0..pos]` of a demand-filled stdin buffer
45+
/// and reset `pos` to 0, so a long-running reader holds only its unread tail.
46+
/// Frees the excess allocation once it is over 4x the tail and
47+
/// `retained_capacity`.
48+
/// Call before appending a fill: the append may move `data` anyway, so no
49+
/// caller holds a slice or offset across it.
50+
pub fn dropConsumed(infra: std.mem.Allocator, data: *std.ArrayList(u8), pos: *usize) void {
51+
const consumed = pos.*;
52+
if (consumed == 0) return;
53+
const rest = data.items.len - consumed;
54+
std.mem.copyForwards(u8, data.items[0..rest], data.items[consumed..]);
55+
data.shrinkRetainingCapacity(rest);
56+
pos.* = 0;
57+
if (data.capacity > retained_capacity and data.capacity > rest * 4)
58+
data.shrinkAndFree(infra, rest);
59+
}
60+
61+
test "dropConsumed keeps the unread tail and resets pos" {
62+
const a = std.testing.allocator;
63+
var data: std.ArrayList(u8) = .empty;
64+
defer data.deinit(a);
65+
try data.appendSlice(a, "line one\nline two");
66+
var pos: usize = 9;
67+
dropConsumed(a, &data, &pos);
68+
try std.testing.expectEqualStrings("line two", data.items);
69+
try std.testing.expectEqual(@as(usize, 0), pos);
70+
}
71+
72+
test "dropConsumed releases a buffer that grew far past its tail" {
73+
const a = std.testing.allocator;
74+
var data: std.ArrayList(u8) = .empty;
75+
defer data.deinit(a);
76+
try data.appendNTimes(a, 'x', 1024 * 1024);
77+
var pos: usize = data.items.len - 3;
78+
dropConsumed(a, &data, &pos);
79+
try std.testing.expectEqual(@as(usize, 3), data.items.len);
80+
try std.testing.expect(data.capacity <= retained_capacity);
81+
}
82+
83+
test "dropConsumed is a no-op at pos 0" {
84+
const a = std.testing.allocator;
85+
var data: std.ArrayList(u8) = .empty;
86+
defer data.deinit(a);
87+
try data.appendSlice(a, "abc");
88+
var pos: usize = 0;
89+
dropConsumed(a, &data, &pos);
90+
try std.testing.expectEqualStrings("abc", data.items);
91+
}

‎src/runtime/io/text_io.zig‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ const ReaderState = struct {
174174
/// TTY is the correct clj-parity behaviour (read-line waits for input).
175175
fn fillFromStdin(rt: *Runtime, st: *ReaderState) bool {
176176
if (st.stdin_eof) return false;
177+
stdin.beforeFill(rt.gc.infra, &st.data, &st.pos, &rt.gc);
177178
var chunk: [4096]u8 = undefined;
178179
const n = stdin.readChunk(rt.io, &chunk) catch {
179180
st.stdin_eof = true;

‎src/runtime/java/lang/StringBuilder.zig‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,15 @@ fn appendStr(rt: *Runtime, env: *Env, lp: *ByteList, v: Value) !void {
5050
var aw: std.Io.Writer.Allocating = .init(rt.gpa);
5151
defer aw.deinit();
5252
try print_mod.writeStrValue(rt, env, &aw.writer, v);
53-
try lp.appendSlice(rt.gc.infra, aw.writer.buffered());
53+
try appendCharged(rt, lp, aw.writer.buffered());
54+
}
55+
56+
/// Append `bytes` to `lp`, charging any buffer growth to the collect trigger:
57+
/// the builder's cell is a few words however large the buffer gets.
58+
fn appendCharged(rt: *Runtime, lp: *ByteList, bytes: []const u8) !void {
59+
const before = lp.capacity;
60+
try lp.appendSlice(rt.gc.infra, bytes);
61+
if (lp.capacity > before) rt.gc.noteOwnedAlloc(lp.capacity - before);
5462
}
5563

5664
/// `(StringBuilder.)` / `(StringBuilder. "seed")` / `(StringBuilder. n)` —
@@ -90,7 +98,7 @@ fn append(rt: *Runtime, env: *Env, args: []const Value, loc: SourceLocation) any
9098
const to = byteOffsetOfCodepoint(s, to_i);
9199
if (from_i < 0 or to_i < from_i or from == null or to == null)
92100
return error_catalog.raise(.index_out_of_range, loc, .{ .fn_name = "java.lang.StringBuilder/append" });
93-
try listOf(args[0]).appendSlice(rt.gc.infra, s[from.?..to.?]);
101+
try appendCharged(rt, listOf(args[0]), s[from.?..to.?]);
94102
return args[0];
95103
}
96104

‎test/clj/suites/gc_heap_return_test.clj‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@
1111
(defn- committed [] (.totalMemory (Runtime/getRuntime)))
1212

1313
(defn- burst
14-
"Allocate a ~8.5 MB vector trie (1M elements) and drop it."
14+
"Allocate a ~34 MB vector trie (4M elements) and drop it. It must be much
15+
larger than the free space earlier suites leave in partly used pages of the
16+
shared run_suites image, or it lands in already-committed pages and the
17+
growth is invisible (#80's macOS gate: base 208 MB, burst +0.8 MB)."
1518
[]
16-
(count (vec (range 1000000))))
19+
(count (vec (range 4000000))))
1720

1821
(deftest requested-collect-returns-empty-pages
1922
(System/gc)
@@ -23,9 +26,9 @@
2326
_ (System/gc)
2427
after (committed)]
2528
(testing "the burst grew the committed heap"
26-
(is (> grown (+ base (* 4 mb)))))
29+
(is (> grown (+ base (* 16 mb)))))
2730
(testing "System/gc gave the dropped burst's pages back"
28-
(is (< after (- grown (* 4 mb))))
31+
(is (< after (- grown (* 16 mb))))
2932
(is (<= after (+ base (* 2 mb)))))))
3033

3134
(deftest freed-pages-are-reused-by-the-next-burst

0 commit comments

Comments
 (0)