Comment

Comment by timmaxw

I wonder if you could apply a simpler version of this to ordinary C/C++ programs? If you allocate a chunk on the heap with 8-byte alignment, then you can throw away the lowest 3 bits of precision, and then you can address 32GB with a 32-bit pointer. You'd need the cooperation of the memory allocator to ensure it gives you addresses in the first 32GB of address space, and it might interfere with things like ASLR, but it doesn't seem impossible.

Something like this in C++:

  template<class T>
  class CompressedPointer {
  public:
    T &operator*() const {
      return *static_cast<T *>((uint64_t)ptr << 3);
    }

    /* It's not safe to construct a CompressedPointer<T> from an arbitrary T*;
    you probably want to malloc and construct via the CompressedPointer itself */
    template<Args...>
    static CompressedPointer malloc(Args...) {
      /* allocate 8-byte aligned sizeof(T) in first 32GB, then construct T in it */
    }

  private:
    uint32_t ptr;
  }
[Edit] Extra crazy idea: Take an existing large C++ codebase. For each struct T in the codebase, use static analysis to determine if T is ever stack-allocated (or otherwise incompatible with CompressedPointer). If not, automatically rewrite all "T*" to "CompressedPointer<T>" and all "new T(...)" with "CompressedPointer<T>::malloc(...)". I suspect this would work for 90% of the heap allocations in a typical C++ codebase...
parent timmaxw 4 years ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton