Live data from Hacker News

9cc: A Small C Compiler

github.com

61–70 of 70 posts

Re: 9cc: A Small C Compiler

#61
post #48

Earlier quoted context omitted.

It will put more pressure on the allocator when running, doing that a lot will likely have some kind of cumulative consequence down the line. I know from experience [0] that even reusing allocated memory rather than bouncing it back to malloc can have dramatic effects. [0] https://gitlab.com/sifoo/snigl/blob/master/src/snigl/pool.h

Malloc doesn't interact with the kernel at all. The kernel sees pages, not the data structures that malloc manages. The kernel doesn't even know whether you free()'d the memory by the time the process exits. There is exactly zero difference from the operating system's perspective between freeing and not freeing the memory before program termination (except that one might have a higher peak memory usage). The classic…

Did I mention kernels anywhere? I simply shared my experience.

If that doesn't fit neatly into your delusions it's really not my problem.

Re: 9cc: A Small C Compiler

#62
post #43

Earlier quoted context omitted.

That is of course the C11 (draft) spec, not C++. Interesting find there. The wording is also in the C99 draft; it is not new. It is in fact saying that the empty list in a definition is a special case and does declare that the function takes no parameters. To "specify" here can be understood to mean as inserting information about type into the declaration scope; that which a declaration does. So that's a bit of a bug…

My understanding is that, unless you have a function prototype, you will lose type info about the parameters: "The empty list in a function declarator that is not part of a definition of that function specifies that no information about the number or types of the parameters is supplied." i.e. the most important is the end of that sentence, so GCC behavior should be right.

Note the: "that is not part of a definition".

This is about declarations only.

Re: 9cc: A Small C Compiler

#63
post #53
post #39

Earlier quoted context omitted.

Wait, doesn't it do the opposite? The big thing that "pressures" an allocator is fragmentation, which you don't get at all if you never free.

With no freeing, memory used is the sum of all allocations. With freeing, it is max of the allocation for active objects at any given time. The former can ask for much more memory from the OS for some programs that generate a lot of temp garbage. Though this probably doesn’t matter for a C compiler - unless it is required to compile megabytes of generated code!

An allocation you don't have to free is, at the margin, a simple pointer increment. It's the need to free memory that makes allocators expensive, not the amount of memory they allocate. This is why performance-sensitive programs use pools, arenas, and other preallocation strategies.

Re: 9cc: A Small C Compiler

#65
post #54
post #48

Earlier quoted context omitted.

Malloc doesn't interact with the kernel at all. The kernel sees pages, not the data structures that malloc manages. The kernel doesn't even know whether you free()'d the memory by the time the process exits. There is exactly zero difference from the operating system's perspective between freeing and not freeing the memory before program termination (except that one might have a higher peak memory usage). The classic…

> There is exactly zero difference from the operating system's perspective (...) Close, but not quite: in case of larger allocations, malloc() tends to use mmap( , , , | MAP_ANON) rather than brk() to request memory from the OS. For example, the glibc's malloc() uses mmap() when requested size exceeds MMAP_THRESHOLD, which is 128kB by default. The mmap() approach gives large, continuous memory blocks that can also be…

An implementation certainly could return memory to the OS on calls to free(), but to my knowledge none of the widely used implementations do so. (I would be interested to learn of counterexamples!)

Re: 9cc: A Small C Compiler

#66
post #58

Earlier quoted context omitted.

https://nostarch.com/gnumake

That is not, contrary to what the name might imply, the official GNU Make manual. That one can be read here, for free: https://www.gnu.org/software/make/manual/

I don't take the name as implying that.

Anyway, it is excellent and has some good advice and tips.

Unlike the official manual which is more a reference.

Re: 9cc: A Small C Compiler

#67
post #65
post #54

Earlier quoted context omitted.

> There is exactly zero difference from the operating system's perspective (...) Close, but not quite: in case of larger allocations, malloc() tends to use mmap( , , , | MAP_ANON) rather than brk() to request memory from the OS. For example, the glibc's malloc() uses mmap() when requested size exceeds MMAP_THRESHOLD, which is 128kB by default. The mmap() approach gives large, continuous memory blocks that can also be…

An implementation certainly could return memory to the OS on calls to free(), but to my knowledge none of the widely used implementations do so. (I would be interested to learn of counterexamples!)

jemalloc can use madvise(MADV_FREE) to return pages to the OS.

Re: 9cc: A Small C Compiler

#68
post #46
post #2

It lists a goal as "compiling real-world programs such as the linux kernel" The last time I investigated the linux kernel had so many gcc-isms that it was probably true that if you could compile the linux kernel, you could probably compile any program targeted to gcc.

Other "alternative" compilers like tcc have pulled it off, supposedly without patching the kernel, so it is doable.

Does tcc compile a vanilla kernel? I know tccboot added some minor patches:

https://bellard.org/tcc/tccboot_readme.html

Re: 9cc: A Small C Compiler

#69
post #63
post #53

Earlier quoted context omitted.

With no freeing, memory used is the sum of all allocations. With freeing, it is max of the allocation for active objects at any given time. The former can ask for much more memory from the OS for some programs that generate a lot of temp garbage. Though this probably doesn’t matter for a C compiler - unless it is required to compile megabytes of generated code!

An allocation you don't have to free is, at the margin, a simple pointer increment. It's the need to free memory that makes allocators expensive, not the amount of memory they allocate. This is why performance-sensitive programs use pools, arenas, and other preallocation strategies.

What I was getting at is that non-freeing programs would use much more memory. This doesn't increase the "pressure" on the allocator for such a program but does increase the pressure on the OS (that has to handle memory requests for many concurrently running programs).

Re: 9cc: A Small C Compiler

#70
post #52

I really wish people would choose better names, we just had an hyphen and it becomes a C dialect from the early 80's. https://en.wikipedia.org/wiki/Small-C

"a small c compiler" is just the description, "9cc" is the name itself, so there's not much similarity there...
Post reply on HN