Extend StdoutWrite/StderrWrite so they optionally append a newline
Summary
Writing lines to STDOUT and STDERR is very common. Currently the standard library manually adds the newline. For example, stdout.print
is implemented as follows:
def print(data: ?ToString = Nil) -> Integer {
process.blocking {
write_string(data.to_string) +
write_string(NEWLINE)
}
}
The VM instructions StdoutWrite
and StderrWrite
should take an argument that indicates if a newline should be appended. This argument is required for the sake of VM code simplicity.
Motivation
The current implementation requires two calls to write_string
. Both these calls will end up calling process.blocking
. Due to print
already wrapping this code in a process.blocking
block, this all ends up in redundant method calls. Implementing this directly in the VM allows us to add the newline in the most efficient way possible, instead of having to concatenate strings at runtime.
Implementation
The VM's io::io_write
function will take an additional argument called append_newline
. After writing the initial output, we check this argument. If it's true, we write the system's newline separator. If this succeeds we add the number of bytes written for the newline to the total, then return that.
Drawbacks
This requires extending io::io_write
, which may result in a tiny bit of extra overhead for writes.