Skip to content
HomeVideosBooksBlogs
AllTop postsTopics
Podcasts
HistoryListsWatch laterLiked videosFollowing

The videos, books, blogs and podcasts Hacker News keeps recommending, all in one place.

VideosBooksBlogsPodcastsAboutPrivacySettings

Data from Hacker News, HackerTube, HackerBooks, HackerBlogs and Open Library. Credits · Not affiliated with Y Combinator.

HomeVideosBooksBlogsPodcasts
Ctrl K
    L

    Laurent Luce

    laurentluce.com · Personal blog · on HN since 2011

    Laurent Luce writes about Python internals and tools, covering dictionary, list, and integer implementations, concurrency primitives, and Twitter sentiment analysis.

    Visit blogHN submissions
    ProgrammingLanguages & Compilers
    What HN says

    HN links it to explain how CPython implements lists and small integers, such as why small integers are preallocated.

    Stats for nerds
    22HN people shared it
    16linked in comments
    156story points
    Oct 2022last shared
    2014people per year2022

    Top posts on HN

    1. 1
      Twitter sentiment analysis using Python and NLTK
      3 people · 85 points · first shared 2012 · 8 comments on HN
    2. 2
      Python Locks, RLocks, Semaphores, Conditions, Events and Queues
      1 person · 33 points · first shared 2011 · 4 comments on HN
    3. 3
      Solving mazes using Python: Simple recursivity and A* search
      2 people · 16 points · first shared 2011 · 7 comments on HN
    4. 4
      Python’s dictionary implementation
      1 person · 8 points · first shared 2011 · Discussion on HN
    5. 5
      Python's Dictionary Implementation
      6 people · 5 points · first shared 2013 · Discussion on HN
    6. 6
      Python’s list implementation
      1 person · 3 points · first shared 2011 · Discussion on HN
    7. 7
      Python String Objects Implementation
      2 people · 2 points · first shared 2016 · Discussion on HN
    8. 8
      Create a Debian package for your Django application (2010)
      1 person · 1 points · first shared 2016 · Discussion on HN
    9. 9
      Geo data of the Massachusetts elementary school districts using Python
      1 person · 1 points · first shared 2013 · Discussion on HN
    10. 10
      Python and cryptography with pycrypto
      1 person · 1 points · first shared 2014 · Discussion on HN

    What HN people wrote when they linked it

    • iheartmemcacheJul 2016

      My gut-ballpark was going to be around an order of magnitude, not two. Here's are two naive comparisons (granted, from the late aughts and not a direct comparison but a more general one) that show ~7-8x[0,1]. The overhead of PyStringObject is not trivial[2] (though the implementation details likely have changed between Py2.x and Python 3).

      For things like building accumulators a set of data/log parsing rather than data munging (hits per hour or enumerative tasks), I'd imagine (g|n)?awk might hit your 100x since you'd just grab the fd and traverse being IO bound. I'm not sure how awk does it, but if it's just saving an accumulator value (or 10) in a register. Assuming x64-64 treats, say, an r3 fetch analogously to a fetch to ecx (err..rcx now I guess), rather than having to keep a full object in L1 cache, awk has a huge advantage.

      ---

      N.b., if you're benching tasks like this, don't use 'time' and STDOUT and think you're getting real performance numbers. Your bottleneck (terminals can only render $x lines a minute, so the kernel call to write(STDOUT, ....) will be where you choke, not at the language. Also disk fragmentation would be another issue. Put the both the test file and the output file on a RAMdisk.) Cache flush with Something like sync; echo 3 > /proc/sys/vm/drop_caches (on Linux, I forget the BSD way of doing it) then `time benchmark.py /mnt/ramdisk1/file > /mnt/ramdisk2' over multiple runs, under various loads, with different data sets, etc

      Another interesting…

    • photochemsynOct 2022

      These kinds of diagrams are OK for the most basic introduction to data structures, but they're not much help when it comes to creating an efficient implementation. Learning about data structure implementation without using a relatively low-level language that allows pointers seems to be a recipe for confusion.

      Hence, one is probably always better off using the built-ins or libraries for any data structure in a higher-level language rather than implementing them from scratch. For example, here's a nice blog post on how Python dicts are implemented using hash tables written in C.

      https://www.laurentluce.com/posts/python-dictionary-implementation/

      While it's possible to build something like a binary search tree from scratch in Python using classes for nodes and with references instead of pointers, along with recursive algorithms for traversing it, it doesn't seem like that great of an idea to teach students in this manner. If the goal is to really teach students how to do this, C or similar (Rust?) is probably the better option for code examples, and a lot of time would have to be spent on memory management concepts (i.e. have students build their own dynamically allocated stack for the traversal, to avoid blowing through the stack, maybe).

    • fencepostSep 2017

      So likely thinking of it in terms of checking a tree based on characters instead of hashes of full words. Which is more viable would likely depend on what kind of memory constraints and dictionary size was being considered. Also very implementation dependent, what's the size of the hashes generated?

      Edit: curiosity got me looking at this for Python, with an answer of "it depends, and both have correct aspects" based on Python's implementation [1]. The hashing varies by string length, the lookup is fixed unless there's a collision and probing, probing will be minimized by growing the dictionary as items are added. In any case, better than O(n) but worse than O(1) and with some possible slowdowns during loading unless you can set an expected final size during initialization.

      Edit again: since I brought it up, turns out initialization doesn't really matter, resizing is not a speed factor. Relevant discussion starts from https://stackoverflow.com/questions/1298636/how-to-set-initial-size-for-a-dictionary-in-python

      [1] http://www.laurentluce.com/posts/python-dictionary-implementation/

    • gtirloniJan 2018

      It's puzzling that "x is 17" should work but then "1000 + 1 is 1001" doesn't.

      The reason is that Python pre-allocates 262 numbers at initialization (-5 to 256) and the allocation uses a different method/structure for bigger numbers.

      >>> x = 256
      >>> x is 256
      True
      >>> id(x)
      94900419138400
      >>> id(256)
      94900419138400

      >>> x = 257
      >>> x is 257
      False
      >>> id(x)
      94900419504344
      >>> id(257)
      94900419504368

      Reference: https://www.laurentluce.com/posts/python-integer-objects-implementation/

    • e12eDec 2019

      Since there isn't any traversal when looking up an element n, I guess the best "short hand" would be to call it an array - it's an array of pointers to objects on the heap.

      https://www.laurentluce.com/posts/python-list-implementation/

      As for collections within collections, python allows for that in "lists" (arrays) and "dictionaries" (maps).

    • bcjordanJan 2014

      Raw source: http://svn.python.org/view/%2acheckout%2a/python/tags/r266/Objects/dictobject.c?revision=84293&content-type=text/plain

      Walkthrough of the implementation: http://www.laurentluce.com/posts/python-dictionary-implementation/

    Related

    PyPy Blogmorepypy.blogspot.com242 people

    Let's Remove the Global Interpreter Lock

    PyPy Blog
    Company · last shared 2024
    FP Complete Blogfpcomplete.com201 people

    Monads and GATs in Nightly Rust

    FP Complete Blog
    Company · last shared 2025
    Ian Bickingblog.ianbicking.org107 people

    Firefox Was Always Enough

    Ian Bicking
    Personal · last shared 2026
    Alexis Kinglexi-lambda.github.io172 people

    Parse, Don’t Validate

    Alexis King
    Personal · last shared 2026
    Julien Danjoujulien.danjou.info81 people

    Emacs gets Google Maps support.

    Julien Danjou
    Personal · last shared 2026
    Chris Pennerchrispenner.ca27 people

    Tail recursion in Python

    Chris Penner
    Personal · last shared 2026
    Bryan O'Sullivanserpentine.com49 people

    Github is making me feel stupid(er)

    Bryan O'Sullivan
    Personal · last shared 2021
    Fredrik Lundheffbot.org64 people

    Simple Top-Down Parsing in Python

    Fredrik Lundh
    Personal · last shared 2023