Hacker News new | ask | show | jobs
by willtim 2853 days ago
High-locality of reference can be achieved in any language that supports unboxed types, it doesn't require C (even a very high-level language like Haskell has support for this). But this is a long way from having complete control how each memory heirarchy is used.

Likewise most static languages defer to the compiler for CPU-specific performance optimisations and will permit foreign native calls into C or ASM where necessary. So I don't see how this is an argument in C's favour.

1 comments

> High-locality of reference can be achieved in any language that supports unboxed types, it doesn't require C (even a very high-level language like Haskell supports this).

You often also need correct alignment. Cache-line or page. Your unboxed access across two pages can cause two TLB misses, L1 misses etc. Not to mention two page faults.

Sometimes you need to ensure two (or more) buffers are NOT aligned in a particular way to avoid interfering with CPU caching mechanisms.

The only support C is giving you for this is that it has sized unboxed types (and raw pointer access). Even then, you'd have to trust the compiler and take measurements to be sure.
That's not true, since C11 we have:

    #include <stdlib.h>

    void *aligned_alloc(size_t alignment, size_t size);
which works like malloc() but lets you specify the required alignment.
Could I not just call such a function from my alternative language?
Only for sizes <4KB
That's not true. You can also control data alignment in various ways. For example, you don't need to write to the beginning of an allocated buffer, but skip to the point where low order address bits are what you want.

Something like this for example:

  char* aligned_buf;
  char* buf;
  size_t max_align_offset = (1<<align) - 1;
  buf = malloc(length_needed + max_align_offset);
  aligned_buf = (buf + max_align_offset) & ~max_align_offset;
In the example, if align==8, you have 256 byte alignment. If it's 12, 4kB alignment.
Good luck ensuring that doesn't trigger UB across all target architectures and compilers being used.
I agree, but that's besides the point.

It was just an example to show one way how C can control alignment.

This example looks like it should be turned into a malloc variant, i.e. needs only to be written once. Raw pointer access is also available in other languages of course, albeit it is usually made much more difficult.