authorgravatar for 72643694+mgord9518@users.noreply.github.comMathew R Gordon <72643694+mgord9518@users.noreply.github.com> 2023-07-17 08:16:17-06:00
committergravatar for mail@isaacfreund.comIsaac Freund <mail@isaacfreund.com> 2023-07-18 11:44:23+02:00
log11695745e5c6cbd158625d2c1682a09e2ee4c679
tree71df103adf7fedece1ec2401c616682e72fd5e16
parenta0b35249a29c6869650ba7e3515999fcced8dcf2

getenv: remove unnessary `small key` block

The code removed does unnecessary copying in order to create a null-terminated pointer, just to pass it to libc getenv. It only does this for `small keys`, which are under 64 bytes in size. Instead of going out of the way to add a null byte to a function that takes normal slices, this should just be handled by the loop below, which scans c.environ to find the value

1 files changed, 0 insertions(+), 9 deletions(-)

lib/std/os.zig-9
......@@ -1910,15 +1910,6 @@ pub fn execvpeZ(
19101910/// See also `getenvZ`.
19111911pub fn getenv(key: []const u8) ?[:0]const u8 {
19121912 if (builtin.link_libc) {
1913 // Append null byte to the key to use with cstd getenv
1914 var small_key_buf: [64]u8 = undefined;
1915 if (key.len < small_key_buf.len) {
1916 @memcpy(small_key_buf[0..key.len], key);
1917 small_key_buf[key.len] = 0;
1918 return getenvZ(small_key_buf[0..key.len :0]);
1919 }
1920
1921 // Search the entire `environ` because we don't have a null terminated pointer.
19221913 var ptr = std.c.environ;
19231914 while (ptr[0]) |line| : (ptr += 1) {
19241915 var line_i: usize = 0;