The RAM myth
The RAM myth is a belief that modern computer memory resembles perfect random-access memory. Cache is seen as an optimization for small data: if it fits in L2, it’s going to be processed faster; if it doesn’t, there’s nothing we can do.
Most likely, you believe that code like this is the fastest way to shard data:
groups = [[] for _ in range(n_groups)]
+purplesyringa's blog The RAM myth
The RAM myth is a belief that modern computer memory resembles perfect random-access memory. Cache is seen as an optimization for small data: if it fits in L2, it’s going to be processed faster; if it doesn’t, there’s nothing we can do.
Most likely, you believe that pseudocode like this is the fastest way to shard data:
groups = [[] for _ in range(n_groups)]
for element in elements:
groups[element.group].append(element)
Indeed, it’s linear (i.e. asymptotically optimal), and we have to access random indices anyway, so cache isn’t going to help us in any case.
In reality, this is leaving a lot of performance on the table, and certain asymptotically slower algorithms can perform sharding significantly faster on large input. They are mostly used by on-disk databases, but, surprisingly, they are useful even for in-RAM data.
Thoughts on Rust hashing
Reddit IRLOIn languages like Python, Java, or C++, values are hashed by calling a “hash me” method on them, implemented by the type author. This fixed-hash size is then immediately used by the hash table or what have you. This design suffers from some obvious problems, like:
How do you hash an integer? If you use a no-op hasher (booo), DoS attacks on hash tables are inevitable. If you hash it thoroughly, consumers that only cache hashes to optimize equality checks lose out of performance.
Any Python program fits in 24 characters*
* If you don’t take whitespace into account.
My friend challenged me to find the shortest solution to a certain Leetcode-style problem in Python. They were generous enough to let me use whitespace for free, so that the code stays readable. So that’s exactly what we’ll abuse to encode any Python program in bytes, ignoring whitespace.
The Rust Trademark Policy is still harmful
RedditFour days ago, the Rust Foundation released a new draft of the Rust Language Trademark Policy. The previous draft caused division within the community several years ago, prompting its retraction with the aim of creating a new, milder version.
Well, that failed. While certain issues were addressed (thank you, we appreciate it!), the new version remains excessively restrictive and, in my opinion, will harm both the Rust community as a whole and compiler and crate developers. While I expect the stricter rules to not be enforced in practice, I don’t want to constantly feel like I’m under threat while contributing to the Rust ecosystem, and this is exactly what it would feel like if this draft is finalized.
Below are some of my core objections to the draft.
Bringing faster exceptions to Rust
RedditThree months ago, I wrote about why you might want to use panics for error handling. Even though it’s a catchy title, panics are hardly suited for this goal, even if you try to hack around with macros and libraries. The real star is the unwinding mechanism, which powers panics. This post is the first in a series exploring what unwinding is, how to speed it up, and how it can benefit Rust and C++ programmers.
We built the best "Bad Apple!!" in Minecraft
Hacker NewsDemoscene is the art of pushing computers to perform tasks they weren’t designed to handle. One recurring theme in demoscene is the shadow-art animation “Bad Apple!!”. We’ve played it on the Commodore 64, Vectrex (a unique game console utilizing only vector graphics), Impulse Tracker, and even exploited Super Mario Bros. to play it.
But how about Bad Apple!!.. in Minecraft?
Minecraft сравнивает массивы за куб
TelegramКоллизии в играх обнаруживаются тяжелыми алгоритмами. Для примера попробуйте представить себе, насколько сложно это для просто двух произвольно повернутых кубов в пространстве. Они могут контактировать двумя ребрами, вершиной и гранью или еще как-то более сложно.
В майнкрафте вся геометрия хитбоксов параллельна осям координат, т.е. наклона не бывает. Это сильно упрощает поиск коллизий.
Я бы такое писала просто. Раз хитбокс блока — это объединение нескольких параллелепипедов, то можно его так и хранить: как список 6-элементных тьюплов. В подавляющем большинстве случаев этот список будет очень коротким. Для обычных кубов его длина — 1, для стеклопаналей может достигать 2, наковальня, о боги, состоит из 3 элементов, а стены могут иметь их аж целых 4. Для проверки хитбоксов на пересечение достаточно перебрать пары параллелепипедов двух хитбоксов (кажется, их может быть максимум 16). Для параллелепипедов с параллельными осями задача решается тривиально.
Но Minecraft JE писала не я, поэтому там реализация иная.
WebP: The WebPage compression format
Hacker News Reddit Lobsters RussianI want to provide a smooth experience to my site visitors, so I work on accessibility and ensure it works without JavaScript enabled. I care about page load time because some pages contain large illustrations, so I minify my HTML.
But one thing makes turning my blog light as a feather a pain in the ass.
Division is hard, but it doesn't have to be
RedditDevelopers don’t usually divide numbers all the time, but hashmaps often need to compute remainders modulo a prime. Hashmaps are really common, so fast division is useful.
For instance, rolling hashes might compute u128 % u64
with a fixed divisor. Compilers just drop the ball here:
fn modulo(n: u128) -> u64 {
diff --git a/blog/the-ram-myth/index.html b/blog/the-ram-myth/index.html
index c3f0a37..0ffc07c 100644
--- a/blog/the-ram-myth/index.html
+++ b/blog/the-ram-myth/index.html
@@ -1,11 +1,11 @@
The RAM myth | purplesyringa's blog The RAM myth
The RAM myth is a belief that modern computer memory resembles perfect random-access memory. Cache is seen as an optimization for small data: if it fits in L2, it’s going to be processed faster; if it doesn’t, there’s nothing we can do.
Most likely, you believe that code like this is the fastest way to shard data:
groups = [[] for _ in range(n_groups)]
+In reality, this is leaving a lot of performance on the table, and certain asymptotically slower algorithms can perform sharding significantly faster on large input. They are mostly used by on-disk databases, but, surprisingly, they are useful even for in-RAM data."property=og:description>The RAM myth
The RAM myth is a belief that modern computer memory resembles perfect random-access memory. Cache is seen as an optimization for small data: if it fits in L2, it’s going to be processed faster; if it doesn’t, there’s nothing we can do.
Most likely, you believe that pseudocode like this is the fastest way to shard data:
groups = [[] for _ in range(n_groups)]
for element in elements:
groups[element.group].append(element)
Indeed, it’s linear (i.e. asymptotically optimal), and we have to access random indices anyway, so cache isn’t going to help us in any case.
In reality, this is leaving a lot of performance on the table, and certain asymptotically slower algorithms can perform sharding significantly faster on large input. They are mostly used by on-disk databases, but, surprisingly, they are useful even for in-RAM data.
SolutionThe algorithm from above has cache misses on random input. The only way to reduce this number is to make the memory accesses more ordered. If you can ensure the elements are ordered by group
, that’s great. If you can’t, you can still sort the accesses before the for
loop:
elements.sort(key = lambda element: element.group)
diff --git a/blog/the-ram-myth/index.md b/blog/the-ram-myth/index.md
index cc79afb..d02df34 100644
--- a/blog/the-ram-myth/index.md
+++ b/blog/the-ram-myth/index.md
@@ -4,7 +4,7 @@ time: December 19, 2024
intro: |
The RAM myth is a belief that modern computer memory resembles perfect random-access memory. Cache is seen as an optimization for small data: if it fits in L2, it's going to be processed faster; if it doesn't, there's nothing we can do.
- Most likely, you believe that code like this is the fastest way to shard data:
+ Most likely, you believe that pseudocode like this is the fastest way to shard data:
```python
groups = [[] for _ in range(n_groups)]
@@ -19,7 +19,7 @@ intro: |
The RAM myth is a belief that modern computer memory resembles perfect random-access memory. Cache is seen as an optimization for small data: if it fits in L2, it's going to be processed faster; if it doesn't, there's nothing we can do.
-Most likely, you believe that code like this is the fastest way to shard data:
+Most likely, you believe that pseudocode like this is the fastest way to shard data:
```python
groups = [[] for _ in range(n_groups)]