authorgravatar for thejoshwolfe@gmail.comJosh Wolfe <thejoshwolfe@gmail.com> 2023-07-22 18:49:59-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2023-07-22 18:49:59-04:00
log2ad16248d7d4352ed9bb7270084042bb76369770
tree1556eb7572563d893ea6dc9f583ade9a5f3b023c
parent04c7b55de49b3f5129098e079e6cde7704c70293
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

std.json.WriteStream: eliminate dead memory in optimized modes (#16485)


1 files changed, 11 insertions(+), 1 deletions(-)

lib/std/json/stringify.zig+11-1
......@@ -109,6 +109,7 @@ pub fn writeStream(
109109/// `max_depth` is rounded up to the nearest multiple of 8.
110110/// If the nesting depth exceeds `max_depth`, it is detectable illegal behavior.
111111/// Give `null` for `max_depth` to disable safety checks for the grammar and allow arbitrary nesting depth.
112/// In `ReleaseFast` and `ReleaseSmall`, `max_depth` is ignored, effectively equivalent to passing `null`.
112113/// Alternatively, see `writeStreamArbitraryDepth` to do safety checks to arbitrary depth.
113114///
114115/// The caller does *not* need to call `deinit()` on the returned object.
......@@ -130,6 +131,9 @@ pub fn writeStreamMaxDepth(
130131/// This version of the write stream enables safety checks to arbitrarily deep nesting levels
131132/// by using the given allocator.
132133/// The caller should call `deinit()` on the returned object to free allocated memory.
134///
135/// In `ReleaseFast` and `ReleaseSmall` mode, this function is effectively equivalent to calling `writeStreamMaxDepth(..., null)`;
136/// in those build modes, the allocator is *not used*.
133137pub fn writeStreamArbitraryDepth(
134138 allocator: Allocator,
135139 out_stream: anytype,
......@@ -176,9 +180,11 @@ pub fn writeStreamArbitraryDepth(
176180/// * If the enum declares a method `pub fn jsonStringify(self: *@This(), jw: anytype) !void`, it is called to do the serialization instead of the default behavior. The given `jw` is a pointer to this `WriteStream`.
177181/// * Zig error -> JSON string naming the error.
178182/// * Zig `*T` -> the rendering of `T`. Note there is no guard against circular-reference infinite recursion.
183///
184/// In `ReleaseFast` and `ReleaseSmall` mode, the given `safety_checks_hint` is ignored and is always treated as `.assumed_correct`.
179185pub fn WriteStream(
180186 comptime OutStream: type,
181 comptime safety_checks: union(enum) {
187 comptime safety_checks_hint: union(enum) {
182188 checked_to_arbitrary_depth,
183189 checked_to_fixed_depth: usize, // Rounded up to the nearest multiple of 8.
184190 assumed_correct,
......@@ -186,6 +192,10 @@ pub fn WriteStream(
186192) type {
187193 return struct {
188194 const Self = @This();
195 const safety_checks: @TypeOf(safety_checks_hint) = switch (@import("builtin").mode) {
196 .Debug, .ReleaseSafe => safety_checks_hint,
197 .ReleaseFast, .ReleaseSmall => .assumed_correct,
198 };
189199
190200 pub const Stream = OutStream;
191201 pub const Error = switch (safety_checks) {