I am a noob. How do I submit new code to the standard library of zig? :)

Hi, This is my second post here. I am a noob.

So,

The standard lib has the following code in std.ascii.isDigit() as of the version 12/13

pub fn isDigit(c: u8) bool {

    return switch (c) {
        '0'...'9' => true,
        else => false,
    };
}

I love it. Simple, to the point. Easy to understand. Clearly it must go. So I rewrote it using bitwise. This is supposed to be faster.

// Written by endlessly_amused from ziggit.dev 20240606
pub fn isDigit_new(c: u8) bool {

    switch (c>>3) {
        6 => return true, // returns on 0,1,2,3,4,5,6,7
        7 => if (c&6==0) {return true;} else {return false;}, // 8,9
        else => return false,
    } // end of switch

} // end of fn

Functionally it does the same thing. But it only has 2 prongs as apposed to the previous 10. The first prong takes out 80% of the expected results.

It works because all digits start 00110xxx, or 0011100x

I am guessing there is some sort of release documentation to put it in as a candidate. If so, where can I get a copy of it? I am looking for the following guidance.

Q1) Is the code actually any better? And if so, What’s more important speed or readability?

Q2) is it actually any faster? Does it give better execution code? Seems to run slightly faster. With my test. But is there some sort of formal test? Or review process?

Q3) Do we care? It saves at best a few clock cycles.

Q4) I have plastered my name in the code. Is this the done thing? Or is this a no no.

So tell me what you think. How does this process work? Is this code any good? Or am I having delusions of adequacy?

I suppose I am just trying to help out. Is there a jobs board anywhere? ie a list of outstanding jobs?

regards

P.s.
I wrote some test for it

test "isDigit_new"
{
    // sanity check
    try std.testing.expect(isDigit_new('/') == false); // character just above and below the numbers
    try std.testing.expect(isDigit_new('0') == true);
    try std.testing.expect(isDigit_new('1') == true);
    try std.testing.expect(isDigit_new('2') == true);
    try std.testing.expect(isDigit_new('3') == true);
    try std.testing.expect(isDigit_new('4') == true);
    try std.testing.expect(isDigit_new('5') == true);
    try std.testing.expect(isDigit_new('6') == true);
    try std.testing.expect(isDigit_new('7') == true);
    try std.testing.expect(isDigit_new('8') == true);
    try std.testing.expect(isDigit_new('9') == true);
    try std.testing.expect(isDigit_new(':') == false); // character just above and below the numbers

    // run through all possible values of u8 and check that they are the same for new code
    for (0..255) |i| { try std.testing.expect(isDigit(@intCast(i))==isDigit_new(@intCast(i))); }

} // end of test
2 Likes

Hey there! I guess the answer to all your questions is at GitHub - ziglang/zig: General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software., the GitHub repository of zig, including the standard library. There is a section about contributing which explains it a bit. You basically just open pull requests. And yes, there are many open issues (or jobs to use your term) and some of them are labeled “beginner-friendy” or other things.

Edit: Not all your question! I did not talk about your code at all! But I also won’t as I really don’t know ^^

Edit: This link goes directly to the more detailed page: Contributing · ziglang/zig Wiki · GitHub

14 posts were split to a new topic: Benchmarking isDigit

As @Eisenhauer showed, they’re equivalent. The formal tests are analyzing the generated machine code and benchmarking. Analyzing machine code can give a lot of certainties, but only in certain cases, like in your code, where both versions lead to the same machine code. There’s obviously no point in benchmarking this.
If you can’t obviously deduce if something is better from analisys alone, you have to benchmark.

I care. I use this function in my code base in a hot loop. I would much prefer an optimized version of it, even it’s hard to read.

I think that’s not ok, unless you invented an awesome algorithm or something. Your name will live in the pull request, the blame page and the contributors section, so you’ll get your credit. Having to read everyone’s name who contributed in the middle of the code would be annoying.

4 Likes

I wouldn’t say that. While it’s good to know that two things result in identical assembly when compiled at maximum optimization settings, having smaller and/or faster runtime code is also nice to have in debug mode. So just generally, one piece of code can perform better than another even if they are both the same on ReleaseFast. I’m not implying that it’s the case for this specific example though.

1 Like

Hi Endlessly_amused! Welcome to ziggit.

Others can correct me, but the switch in the std version has only 2 prongs (counting the else prong). The switch statement isn’t inlined so the check is just checking if the value is between '0' and '9' (inclusive).

1 Like

github docs for creating a pull request

note that any performance enhancements must come with benchmark data points to prove that your alleged improvements are actually better.

5 Likes