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(...@@ -109,6 +109,7 @@ pub fn writeStream(
109/// `max_depth` is rounded up to the nearest multiple of 8.109/// `max_depth` is rounded up to the nearest multiple of 8.
110/// If the nesting depth exceeds `max_depth`, it is detectable illegal behavior.110/// If the nesting depth exceeds `max_depth`, it is detectable illegal behavior.
111/// Give `null` for `max_depth` to disable safety checks for the grammar and allow arbitrary nesting depth.111/// 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`.
112/// Alternatively, see `writeStreamArbitraryDepth` to do safety checks to arbitrary depth.113/// Alternatively, see `writeStreamArbitraryDepth` to do safety checks to arbitrary depth.
113///114///
114/// The caller does *not* need to call `deinit()` on the returned object.115/// The caller does *not* need to call `deinit()` on the returned object.
...@@ -130,6 +131,9 @@ pub fn writeStreamMaxDepth(...@@ -130,6 +131,9 @@ pub fn writeStreamMaxDepth(
130/// This version of the write stream enables safety checks to arbitrarily deep nesting levels131/// This version of the write stream enables safety checks to arbitrarily deep nesting levels
131/// by using the given allocator.132/// by using the given allocator.
132/// The caller should call `deinit()` on the returned object to free allocated memory.133/// 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*.
133pub fn writeStreamArbitraryDepth(137pub fn writeStreamArbitraryDepth(
134 allocator: Allocator,138 allocator: Allocator,
135 out_stream: anytype,139 out_stream: anytype,
...@@ -176,9 +180,11 @@ pub fn writeStreamArbitraryDepth(...@@ -176,9 +180,11 @@ pub fn writeStreamArbitraryDepth(
176/// * 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`.180/// * 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`.
177/// * Zig error -> JSON string naming the error.181/// * Zig error -> JSON string naming the error.
178/// * Zig `*T` -> the rendering of `T`. Note there is no guard against circular-reference infinite recursion.182/// * 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`.
179pub fn WriteStream(185pub fn WriteStream(
180 comptime OutStream: type,186 comptime OutStream: type,
181 comptime safety_checks: union(enum) {187 comptime safety_checks_hint: union(enum) {
182 checked_to_arbitrary_depth,188 checked_to_arbitrary_depth,
183 checked_to_fixed_depth: usize, // Rounded up to the nearest multiple of 8.189 checked_to_fixed_depth: usize, // Rounded up to the nearest multiple of 8.
184 assumed_correct,190 assumed_correct,
...@@ -186,6 +192,10 @@ pub fn WriteStream(...@@ -186,6 +192,10 @@ pub fn WriteStream(
186) type {192) type {
187 return struct {193 return struct {
188 const Self = @This();194 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
190 pub const Stream = OutStream;200 pub const Stream = OutStream;
191 pub const Error = switch (safety_checks) {201 pub const Error = switch (safety_checks) {