Skip to content

Commit abd250b

Browse files
committed
Use stack fallback allocator to usually avoid extra heap allocation in getEnvVarOwned
1 parent 68b8791 commit abd250b

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

lib/std/process.zig

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -393,8 +393,10 @@ pub const GetEnvVarOwnedError = error{
393393
pub 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
};

0 commit comments

Comments
 (0)