Skip to content

Instantly share code, notes, and snippets.

View chadjoan's full-sized avatar
Currently building things to help avoid permanent sleepiness.

Chad Joan chadjoan

Currently building things to help avoid permanent sleepiness.
  • Ohio, United States
View GitHub Profile
@chadjoan
chadjoan / speculations-about-memory-allocation-in-php.md
Last active May 15, 2024 02:44
Speculations about memory allocation in PHP

Speculations about memory allocation in PHP

Premise

Whenever I write code, I habitually try to minimize the amount of memory allocations required for that code to fulfill its purpose.

Memory allocations are very slow:

  • They are complicated. So the CPU has to execute a bunch of instructions inside malloc.
  • They are complicated. The mass of instructions occupies bunch of memory, which then must occupy the CPU's instruction cache (icache) whenever it is executed. This will (probably) be a cache miss to load the allocator, and then it will evict a bunch of "hot" code that will then cache miss again once the allocator is done. Cache misses are SLOW.
  • If the allocator can't retrieve memory from its own pool, it will need to use a system call and execute kernel code to retrieve more memory. Suddenly we've gone from idyllic single-threaded concerns, to "every thread on the host machine wants this thing that you're accessing" kind of bigmess.
@chadjoan
chadjoan / dreaming-of-fixed-point-math.md
Last active January 15, 2024 08:10
Dreaming of Fixed Point Math

I've always wanted a physics engine that uses fixed-point integers. No floats.

Why?

  • Consistent behavior across platforms.[^1][^2][^3][^4]
  • Deterministic calculations become possible by avoiding non-deterministic quirks[^1][^5].
    • If the citations from the last two points weren't enough, this page[^6] is a treasure trove! There are just too many examples of non-determinism and platform divergence for me to cite in this article. The amount of time and effort spent by talented individuals to force disparate systems to behave the same way, or find workarounds, would appear to be immense.
  • Have equivalent expressions yield equivalent numbers.[^7][^8]
  • Incrementing the number by 1 will always increment the number by 1.[^9]
  • Easier representation of exact base-10 numbers, and potentially other rational numbers. [Footnote: Exactness]
    • If we chose a base-10 representation, we can now build all of your content with numbers lik