Live data from Hacker News

Is Python Code Sensitive to CPU Caching? (2024)

lukasatkinson.de

1–10 of 39 posts

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

#3

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.

It should be, as most interpreted code uses a JIT native compiler at this point, which implies the branch prediction is native as well.

I would be happy to stand corrected if someone knows more about this, though!

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

#4

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.

It should be, as most interpreted code uses a JIT native compiler at this point, which implies the branch prediction is native as well. I would be happy to stand corrected if someone knows more about this, though!

AFAIK CPython doesn't JIT compile.

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

#5

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.

It should be, as most interpreted code uses a JIT native compiler at this point, which implies the branch prediction is native as well. I would be happy to stand corrected if someone knows more about this, though!

> most interpreted code uses a JIT native compiler at this point

I suspect most JavaScript code is JIT-compiled - but most Python, Ruby etc is interpreted (via bytecode).

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

#6
post #4

Earlier quoted context omitted.

It should be, as most interpreted code uses a JIT native compiler at this point, which implies the branch prediction is native as well. I would be happy to stand corrected if someone knows more about this, though!

AFAIK CPython doesn't JIT compile.

Recent versions of CPython do have an experimental JIT compiler. I'm not sure how widely-used it is, though.

https://peps.python.org/pep-0744/

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

#7
post #6
post #4

Earlier quoted context omitted.

AFAIK CPython doesn't JIT compile.

Recent versions of CPython do have an experimental JIT compiler. I'm not sure how widely-used it is, though. https://peps.python.org/pep-0744/

It's disabled by default, not production ready. You need to compile CPython yourself if you want to try it.

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

#9

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.

Why would you think that branch prediction wouldn’t be? Do you think the interpreter is adding too much false sharing to the branch predictor to render it worthless?

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

#10

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.

It is! Although my test case is probably an unrealistically bad scenario:

It's the classic, why is processing sorted array faster than unsorted one

    def f(arr):
        r = True
        for x in arr:
           if x > 128: r = not r
        return r

    arr = [ random.randint(0, 255) for i in range(0, 1000_000) ]
    arr_sorted = list(sorted(arr))

    %timeit f(arr)
    %timeit f(arr_sorted)

Results are (on my machine): 17.5 ms for unsorted, and 13.5 ms for sorted. For comparison, in a compiled language, the difference is 4x
Post reply on HN