ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • yes command in Zig
    코딩Coding/Zig_❤️lang 2022. 7. 24. 21:38
    728x90

    yes command in Zig

    Hi, I came across a [interesting post](https://endler.dev/2017/yes/) about how to optimize unix command `yes`, I rewrite the code in Zig. Is there any way to improve this code?

    ## First buffered version
    ```zig
    const std = @import("std");

    pub fn main() !void {
        const argv = std.os.argv;

        const output: []const u8 = if (argv.len > 1)
            argv.ptr[1][0..std.mem.len(argv.ptr[1])]
        else
            "y";

        const out = std.io.getStdOut();
        var f = std.io.bufferedWriter(out.writer());
        while ((try f.write(output)) > 0) {
            _ = try f.write("\n");
        }
    }
    ```
    Compile and run
    ```
    zig build-exe yes.zig -Drelease-safe
    ./yes | pv -r > /dev/null
    ```
    116Mib/s

    ## Second reuse buffer version
    ```zig
    const std = @import("std");

    const BUFFER_CAP = 64 * 1024;

    fn fill_up_buffer(buf: *[BUFFER_CAP]u8, output: []const u8) []const u8 {
        if (output.len > buf.len / 2) {
            return output;
        }

        std.mem.copy(u8, buf, output);
        std.mem.copy(u8, buf[output.len..], "\n");
        var buffer_size = output.len + 1;
        while (buffer_size < buf.len / 2) {
            std.mem.copy(u8, buf[buffer_size..], buf[0..buffer_size]);
            buffer_size *= 2;
        }

        return buf;
    }

    pub fn main() !void {
        const argv = std.os.argv;

        const output: []const u8 = if (argv.len > 1)
            argv.ptr[1][0..std.mem.len(argv.ptr[1])]
        else
            "y";

        var buffer: [BUFFER_CAP]u8 = undefined;
        const body = fill_up_buffer(&buffer, output);
        const stdout = std.io.getStdOut();
        var writer = stdout.writer();
        while ((try writer.write(body)) > 0) {}
    }
    ```

    const std = @import("std");
    
    pub fn main() !void {
        const argv = std.os.argv;
    
        const output: []const u8 = if (argv.len > 1)
            argv.ptr[1][0..std.mem.len(argv.ptr[1])]
        else
            "y";
    
        const out = std.io.getStdOut();
        var f = std.io.bufferedWriter(out.writer());
        while ((try f.write(output)) > 0) {
            _ = try f.write("\n");
        }
    }
    ```
    Compile and run
    ```
    zig build-exe yes.zig -Drelease-safe
    ./yes | pv -r > /dev/null
    ```
    116Mib/s
    
    ## Second reuse buffer version
    ```zig
    const std = @import("std");
    
    const BUFFER_CAP = 64 * 1024;
    
    fn fill_up_buffer(buf: *[BUFFER_CAP]u8, output: []const u8) []const u8 {
        if (output.len > buf.len / 2) {
            return output;
        }
    
        std.mem.copy(u8, buf, output);
        std.mem.copy(u8, buf[output.len..], "\n");
        var buffer_size = output.len + 1;
        while (buffer_size < buf.len / 2) {
            std.mem.copy(u8, buf[buffer_size..], buf[0..buffer_size]);
            buffer_size *= 2;
        }
    
        return buf;
    }
    
    pub fn main() !void {
        const argv = std.os.argv;
    
        const output: []const u8 = if (argv.len > 1)
            argv.ptr[1][0..std.mem.len(argv.ptr[1])]
        else
            "y";
    
        var buffer: [BUFFER_CAP]u8 = undefined;
        const body = fill_up_buffer(&buffer, output);
        const stdout = std.io.getStdOut();
        var writer = stdout.writer();
        while ((try writer.write(body)) > 0) {}
    }



    5.4Gib/s

    https://www.reddit.com/r/Zig/comments/w65vsu/yes_command_in_zig/?utm_source=share&utm_medium=ios_app&utm_name=iossmf

    yes command in Zig

    Hi, I came across a [interesting post](https://endler.dev/2017/yes/) about how to optimize unix command `yes`, I rewrite the code in Zig. Is there...

    www.reddit.com











    반응형
Designed by Tistory.