News for hackers. Stuff that matters.
Re:Unix V5, OpenBSD, Plan 9, FreeBSD, and GNU implementation... by scythe on Tuesday July 19, 2011 @10:22AM (#2781005)
Oh, that makes sense.

Speaking of clever tricks, would it be faster or slower to use c99's variable-length arrays instead of malloc?

Re:Unix V5, OpenBSD, Plan 9, FreeBSD, and GNU implementation... by alexgartrell on Tuesday July 19, 2011 @11:32AM (#2781319)
IIRC faster, because variable-length arrays in c99 just bump down the stack pointer, while malloc can be pretty expensive.

But a single malloc is nothing compared to the cost of a context switch into kernel mode.

Re:Unix V5, OpenBSD, Plan 9, FreeBSD, and GNU implementation... by apaprocki on Tuesday July 19, 2011 @12:34PM (#2781656)
You can simply use alloca() as a replacement for malloc() to avoid depending on VLA support.
Re:Unix V5, OpenBSD, Plan 9, FreeBSD, and GNU implementation... by tedunangst on Tuesday July 19, 2011 @01:59PM (#2782178)
alloca sucks. The error handling is "haha, you overflowed your stack." (VLA sucks for the same reason.)
Re:Unix V5, OpenBSD, Plan 9, FreeBSD, and GNU implementation... by cpeterso on Tuesday July 19, 2011 @01:18PM (#2781924)
Or just declare a large static buffer.
Re:Unix V5, OpenBSD, Plan 9, FreeBSD, and GNU implementation... by tedunangst on Tuesday July 19, 2011 @01:55PM (#2782156)
"Nobody will ever echo this much data!"
Re:Unix V5, OpenBSD, Plan 9, FreeBSD, and GNU implementation... by tmp43522 on Tuesday July 19, 2011 @03:04PM (#2782545)
It's actually not a bad suggestion

    #define BUF_INITIAL 1024
    char buf[BUF_INITIAL];
    
    int main(int argc, char** argv)
    {
        char* p;
        ...
        p = len > BUF_INITIAL ? malloc(len) : buf;
        ...
        write(1, p, len);
        ...
    }
Re:Unix V5, OpenBSD, Plan 9, FreeBSD, and GNU implementation... by cpeterso on Tuesday July 19, 2011 @04:04PM (#2782821)
djb's allocator alloc() does this. He preallocates a 4 KB static buffer before hitting system malloc(). Avoiding the overhead of malloc() is pretty important for the performance of systems like qmail that fork many small processes.
Re:Unix V5, OpenBSD, Plan 9, FreeBSD, and GNU implementation... by tedunangst on Tuesday July 19, 2011 @04:56PM (#2783038)
That's ridiculous. The overhead of fork is about a bajillion times higher than malloc.
Re:Unix V5, OpenBSD, Plan 9, FreeBSD, and GNU implementation... by adestefan on Tuesday July 19, 2011 @10:40PM (#2784080)
With a COW fork() I bet it's smaller. I smell a test coming on, but alas it's late here and I'm going to bed.

I'm also guessing that 4k was chosen because a malloc() of 1 page is faster than a malloc() of >1 page. Of course that's with the assumption that the systems use a 4k page size.

Re:Unix V5, OpenBSD, Plan 9, FreeBSD, and GNU implementation... by aidenn0 on Tuesday July 19, 2011 @08:44PM (#2783776)
Are you sure? I'd bet it's greater, I'd not bet that it's an order of magnitude better, fork() has been optimized a lot more than brk().
Re:Unix V5, OpenBSD, Plan 9, FreeBSD, and GNU implementation... by sliverstorm on Tuesday July 19, 2011 @04:42PM (#2782986)
Many small processes? How many?

4 KB * (many) could be frightening.

Re:Unix V5, OpenBSD, Plan 9, FreeBSD, and GNU implementation... by jff on Tuesday July 19, 2011 @06:34PM (#2783436)
If your system has 32,000 processes and you allocate 4 KB to each of them... that's 128 MB. I'm not crapping my pants at that figure because even the oldest machine in my office, an old Thinkpad, has 2 GB of RAM.
Re:Unix V5, OpenBSD, Plan 9, FreeBSD, and GNU implementation... by GreySyntax on Tuesday July 19, 2011 @02:44PM (#2782430)
Oh wait they will