Lurker News

Comments (218) 56 threads

Show all comments

Showing thread 17 of 56

jacquesm2 replies#
If you're going to use local allocation of short lived buffers then don't use malloc but use alloca. That's much cleaner.

http.c around line 398, that looks wrong.

gpm1 reply#
I've been told that modern compilers really don't like alloca, is that wrong?
jacquesm2 replies#
I don't know who told you. But it's a lot slower than malloc, and requires you to do a bunch of bookkeeping, which is easy to mess up if you have multiple exits from your function.
CyberDildonics2 replies#
alloca is just a couple of instructions to allocate more memory on the stack, it is much faster than malloc for pretty much every reason including locality and the fact that it doesn't have to be freed because it goes away after the current scope.

Continue this thread — 2 replies

lelanthran1 reply#
> But it's a lot slower than malloc

How would it be slower? Isn't it simply bumping the stack pointer?

Continue this thread — 1 reply

2019841 reply#
Arenas would be a better fit for a webserver. Allocations are basically free and everything gets deallocated all at once, so having one arena per active request and resetting it between requests can give very high performance.

Using alloca will result in stack overflows if you use more than a couple megabytes, so it isn't a very good idea.

jacquesm#
Those buffers are tiny and they won't result in stack overflows because the whole thing is single threaded, it is beginner level code.