News for hackers. Stuff that matters.
Re:Writing a Simple Garbage Collector in C (2020) by cancerhacker on Saturday April 08, 2023 @04:58PM (#35497562)
A common idiom that is no longer as common is to always create a stack buffer up to a certain small bound and then an allocation if it goes beyond:

   void dwim(unsigned count) {
     struct Something _buffer[10];
     struct Something* buffer;
     if (count < sizeof(_buffer) / sizeof(_buffer[0]))
       buffer = _buffer;
     else {
       buffer = (struct Something*) malloc(count * sizeof(struct Something));
     }
     doSomething(buffer, count);
     if (buffer != _buffer)
       free(buffer);
    }
It used to be really common to write that without considering if it was a necessary optimization or not - nowadays I would just malloc up front.
Re:Writing a Simple Garbage Collector in C (2020) by gmueckl on Saturday April 08, 2023 @09:47PM (#35499383)
This pattern still exists in C++ in the form of custom containers that implement "small array optimization". The LLVM project is a heavy user of this pattern as far as I know and I have seen it in several proprietary codebases.
Re:Writing a Simple Garbage Collector in C (2020) by alpaca128 on Sunday April 09, 2023 @05:26AM (#35501125)
Same with Rust's smallvec crate.
Re:Writing a Simple Garbage Collector in C (2020) by spacechild1 on Sunday April 09, 2023 @07:50AM (#35501848)
It's also used in std::string (small string optimization).