Everything old is new again: memory optimization

C++ goes lean, Python keeps it comfy, and the comments go feral

TLDR: A dev’s demo shows a tiny C++ program using less memory than a simple Python script, reigniting the “optimize or switch” debate as RAM gets pricier. Commenters split between Python hacks (like memoryview and one‑liners) and C++ purism, with jokes about AI hoarding memory and nods to zram’s comeback

A nostalgic coding throwback just lit up the internet: in a cheeky post on Nibble Stew, Jussi Pakkanen compared a tiny word-count task in Python to a stripped‑down C++ version. The punchline? The Python script peaked around 1.3 MB of memory, while C++ used clever tricks (like reading the file directly and avoiding “real” strings) to stay ultra‑trim. Cue the crowd: half cheering the diet plan, half asking, “But how big was the file, and what about Python’s startup?”

The Python faithful came armed with one‑liners and calm “we can do that too” energy. One commenter dropped a minimalist [Counter] spell, while another escalated with memoryview, a Python trick to avoid making tons of strings—basically saying, don’t count Python out. On the other side, performance purists loved the C++ approach and the no‑nonsense “string views only” mantra. Comedy relief arrived with the now‑viral jab at AI “copyright infringement factories,” a dunk that had the thread cackling. And when someone shouted out zram—compressed memory that’s suddenly trendy again—it felt like your grandpa’s toolbox just went TikTok famous. The takeaway? Everyone agrees memory is hot again; the brawl is over whether you switch languages or just sharpen your tricks

Key Points

  • The article examines memory optimization via a word-count task implemented in Python and in native C++.
  • The Python baseline (under 30 lines) recorded a 1.3 MB peak memory usage on a small text file.
  • The C++ implementation uses Pystd and employs mmap, UTF-8 validation, and lazy splitting to process text.
  • Hash table keys are stored as C++ string_view slices, avoiding creation of separate string objects.
  • Only the hash table and final vector for sorting/printing require dynamic allocations in the native approach; a memory profile is shown.

Hottest takes

"copyright infringement factories" — biorach
"we can use memoryview for the dict generation avoiding creation of string objects" — tzot
"Nowadays when RAM is expensive good old zram is gaining popularity ;)" — fix4fun
Made with <3 by @siedrix and @shesho from CDMX. Powered by Forge&Hive.