os: remove unnecessary heap allocation from os.execute

pull/12447/head
Delyan Angelov 2021-11-12 11:44:26 +02:00
parent c6b8b0bb0a
commit 50a608aab3
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
2 changed files with 6 additions and 5 deletions

View File

@ -344,23 +344,23 @@ pub fn execute(cmd string) Result {
} }
} }
fd := fileno(f) fd := fileno(f)
buf := unsafe { malloc_noscan(4096) }
mut res := strings.new_builder(1024) mut res := strings.new_builder(1024)
defer { defer {
unsafe { res.free() } unsafe { res.free() }
} }
buf := [4096]byte{}
unsafe { unsafe {
pbuf := &buf[0]
for { for {
len := C.read(fd, buf, 4096) len := C.read(fd, pbuf, 4096)
if len == 0 { if len == 0 {
break break
} }
res.write_ptr(buf, len) res.write_ptr(pbuf, len)
} }
} }
soutput := res.str() soutput := res.str()
exit_code := vpclose(f) exit_code := vpclose(f)
unsafe { free(buf) }
return Result{ return Result{
exit_code: exit_code exit_code: exit_code
output: soutput output: soutput

View File

@ -858,5 +858,6 @@ fn test_execute() ? {
// println('output.len: $result.output.len') // println('output.len: $result.output.len')
// println('output hexresult: $hexresult') // println('output hexresult: $hexresult')
assert result.exit_code == 0 assert result.exit_code == 0
assert hexresult == '7374617274004d4944444c450066696e6973680a7878' assert hexresult.starts_with('7374617274004d4944444c450066696e697368')
assert hexresult.ends_with('0a7878')
} }