Live data from Hacker News

Is Python Code Sensitive to CPU Caching? (2024)

lukasatkinson.de

31–39 of 39 posts

Re: Is Python Code Sensitive to CPU Caching? (2024)

#34

IME CPU caches can be observed in almost all languages, regardless of how detached they are from the machine. What I'm really wondering is, if branch prediction can be observed from interpreted code.

Yep, this is why Spectre mitigations are applied to browsers.

Re: Is Python Code Sensitive to CPU Caching? (2024)

#36
I really don't see why you wouldn't expect to find cache-sensitivity in Python, or in some other similarly high-level language.

Python sequences are defined as "support[ing] efficient element access using integer indices" [1]. Python lists are sequences and thus must support random access. In practice that means the implementation is a (dynamically resizing) array allocated contiguously. That means spatial locality is relevant.

If the list type were defined simply as an iterable collection, with no requirement of constant-time random access, the definition would be abstract enough that an implementation might end up being something else than a contiguous array. But if you define the type so that it supports constant-time random access [2], you pretty much end up with cache-friendly sequential access as well.

If you don't define the list type as supporting random access, you also sacrifice asymptotic algorithmic efficiency for lookups by index. Any language that cares about efficiency at all separates collections that support efficient random access from those that don't. (For example, Python has lists and dicts/sets for different access patterns. The Java standard library has separate types for contiguous arrays/lists, hashtable-backed collections and tree-backed collections because the three have different algorithmic efficiencies for different access patterns. In practice it leads to different properties in terms of cache-friendliness as well.)

[1] https://docs.python.org/3/glossary.html#term-sequence

[2] As usual, the Python documentation is a bit vague on the details. It doesn't really say random access has to be constant-time, only that it has to be "efficient". So you might be able to have a non-constant time implementation such as an indexable skiplist while arguing that it's efficient, but you'd have to go out of your way to do that.

Re: Is Python Code Sensitive to CPU Caching? (2024)

#37
post #36

I really don't see why you wouldn't expect to find cache-sensitivity in Python, or in some other similarly high-level language. Python sequences are defined as "support[ing] efficient element access using integer indices" [1]. Python lists are sequences and thus must support random access. In practice that means the implementation is a (dynamically resizing) array allocated contiguously. That means spatial locality i…

>I really don't see why you wouldn't expect to find cache-sensitivity in Python, or in some other similarly high-level language... Python lists are sequences and thus must support random access. In practice that means the implementation is a (dynamically resizing) array allocated contiguously.

The contents of the dynamically allocated array, in the C implementation, are pointers (PyObject), since the lists are heterogeneous and must be able to store any Python object. That entails indirection, which defeats spatial locality. Even if you iterate over cached data, you have to jump around to retrieve the actual objects to do anything* with them.

Re: Is Python Code Sensitive to CPU Caching? (2024)

#38
post #37
post #36

I really don't see why you wouldn't expect to find cache-sensitivity in Python, or in some other similarly high-level language. Python sequences are defined as "support[ing] efficient element access using integer indices" [1]. Python lists are sequences and thus must support random access. In practice that means the implementation is a (dynamically resizing) array allocated contiguously. That means spatial locality i…

>I really don't see why you wouldn't expect to find cache-sensitivity in Python, or in some other similarly high-level language... Python lists are sequences and thus must support random access. In practice that means the implementation is a (dynamically resizing) array allocated contiguously. The contents of the dynamically allocated array, in the C implementation, are pointers (PyObject ), since the lists are heter…

Sure. But that's one less level of indirection than if you also had to jump around to get the reference to the object in the first place.

Re: Is Python Code Sensitive to CPU Caching? (2024)

#39

[flagged]

No, they can’t. The “caching inefficiencies” (?!) are part of the hardware and can’t be optimized away by a JIT. I think you have a very confused understanding of the post.

A compiler (JIT or AOT) can certainly make decisions which are more or less cache-friendly. It does depend on the language and the amount of freedom the compiler has, but just as one example, struct/object field reordering can reduce padding and thus fit more useful data in a cache line.
Post reply on HN