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

This commit is contained in:
Josh Wolfe 2023-07-22 18:49:59 -04:00 committed by GitHub
parent 04c7b55de4
commit 2ad16248d7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -109,6 +109,7 @@ pub fn writeStream(
/// `max_depth` is rounded up to the nearest multiple of 8. /// `max_depth` is rounded up to the nearest multiple of 8.
/// If the nesting depth exceeds `max_depth`, it is detectable illegal behavior. /// If the nesting depth exceeds `max_depth`, it is detectable illegal behavior.
/// Give `null` for `max_depth` to disable safety checks for the grammar and allow arbitrary nesting depth. /// Give `null` for `max_depth` to disable safety checks for the grammar and allow arbitrary nesting depth.
/// In `ReleaseFast` and `ReleaseSmall`, `max_depth` is ignored, effectively equivalent to passing `null`.
/// Alternatively, see `writeStreamArbitraryDepth` to do safety checks to arbitrary depth. /// Alternatively, see `writeStreamArbitraryDepth` to do safety checks to arbitrary depth.
/// ///
/// The caller does *not* need to call `deinit()` on the returned object. /// The caller does *not* need to call `deinit()` on the returned object.
@ -130,6 +131,9 @@ pub fn writeStreamMaxDepth(
/// This version of the write stream enables safety checks to arbitrarily deep nesting levels /// This version of the write stream enables safety checks to arbitrarily deep nesting levels
/// by using the given allocator. /// by using the given allocator.
/// The caller should call `deinit()` on the returned object to free allocated memory. /// The caller should call `deinit()` on the returned object to free allocated memory.
///
/// In `ReleaseFast` and `ReleaseSmall` mode, this function is effectively equivalent to calling `writeStreamMaxDepth(..., null)`;
/// in those build modes, the allocator is *not used*.
pub fn writeStreamArbitraryDepth( pub fn writeStreamArbitraryDepth(
allocator: Allocator, allocator: Allocator,
out_stream: anytype, out_stream: anytype,
@ -176,9 +180,11 @@ pub fn writeStreamArbitraryDepth(
/// * 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`. /// * 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`.
/// * Zig error -> JSON string naming the error. /// * Zig error -> JSON string naming the error.
/// * Zig `*T` -> the rendering of `T`. Note there is no guard against circular-reference infinite recursion. /// * Zig `*T` -> the rendering of `T`. Note there is no guard against circular-reference infinite recursion.
///
/// In `ReleaseFast` and `ReleaseSmall` mode, the given `safety_checks_hint` is ignored and is always treated as `.assumed_correct`.
pub fn WriteStream( pub fn WriteStream(
comptime OutStream: type, comptime OutStream: type,
comptime safety_checks: union(enum) { comptime safety_checks_hint: union(enum) {
checked_to_arbitrary_depth, checked_to_arbitrary_depth,
checked_to_fixed_depth: usize, // Rounded up to the nearest multiple of 8. checked_to_fixed_depth: usize, // Rounded up to the nearest multiple of 8.
assumed_correct, assumed_correct,
@ -186,6 +192,10 @@ pub fn WriteStream(
) type { ) type {
return struct { return struct {
const Self = @This(); const Self = @This();
const safety_checks: @TypeOf(safety_checks_hint) = switch (@import("builtin").mode) {
.Debug, .ReleaseSafe => safety_checks_hint,
.ReleaseFast, .ReleaseSmall => .assumed_correct,
};
pub const Stream = OutStream; pub const Stream = OutStream;
pub const Error = switch (safety_checks) { pub const Error = switch (safety_checks) {