authorgravatar for squeek502@hotmail.comRyan Liptak <squeek502@hotmail.com> 2024-02-09 19:28:57-08:00
committergravatar for squeek502@hotmail.comRyan Liptak <squeek502@hotmail.com> 2024-02-24 14:05:24-08:00
logabd250bb9c94923cc27ce1b09e6113845b7021fa
tree7f0dedb0df168d142f3c101fa523ddecff18531e
parent68b87918df9ad82cf3161f323c55f2e238319922

Use stack fallback allocator to usually avoid extra heap allocation in getEnvVarOwned


1 files changed, 4 insertions(+), 2 deletions(-)

lib/std/process.zig+4-2
......@@ -393,8 +393,10 @@ pub const GetEnvVarOwnedError = error{
393393pub fn getEnvVarOwned(allocator: Allocator, key: []const u8) GetEnvVarOwnedError![]u8 {
394394 if (builtin.os.tag == .windows) {
395395 const result_w = blk: {
396 const key_w = try std.unicode.wtf8ToWtf16LeAllocZ(allocator, key);
397 defer allocator.free(key_w);
396 var stack_alloc = std.heap.stackFallback(256 * @sizeOf(u16), allocator);
397 const stack_allocator = stack_alloc.get();
398 const key_w = try std.unicode.wtf8ToWtf16LeAllocZ(stack_allocator, key);
399 defer stack_allocator.free(key_w);
398400
399401 break :blk std.os.getenvW(key_w) orelse return error.EnvironmentVariableNotFound;
400402 };