https://opendal.apache.org/ logo
Python binding performance
# general
b
Hi again 😸 so I tried to do a little benchmark between vanille Python
open
and
opendal
on a dummy file of ~150MB
Copy code
python
import pathlib
import timeit

import opendal

root = pathlib.Path(__file__).parent
op = opendal.Operator("fs", root=str(root))
filename = "lorem_ipsum_150mb.txt"

def read_file_with_opendal() -> bytes:
    with op.open(filename, "rb") as fp:
        result = fp.read()
    return result

def read_file_with_normal() -> bytes:
    with open(root / filename, "rb") as fp:
        result = fp.read()
    return result

if __name__ == "__main__":
    print("normal: ", timeit.timeit(read_file_with_normal, number=100))
    print("opendal: ", timeit.timeit(read_file_with_opendal, number=100))
and got the following results
Copy code
shell
(venv) $ python benchmark.py
normal:  4.470868484000675
opendal:  8.993250704006641
I was a bit surprised by the result. I'm using
opendal==0.42.0
x
Let’s check it out
I'm still working on this to find out why.
The profiling show thats opendal.so doesn't take too much CPU times.
The heavy work happen inside python
b
Thank you for looking into it. I'm glad you're noticing the same observations as I did. Interesting, I'm not quite sure where those dictionary lookups might be coming from. I'm also curious about the software you're using for benchmarking.
x
I'm using
valgrind --tool=callgrind
and
kcachegrind
for this.
Now I feel like the issue is opendal core itself. I will bench the opendal fs with std fs first.
I improve the perf of opendal's blocking read in https://github.com/apache/incubator-opendal/pull/3617 a lot.
The next issue is to figure out how to speed up pyo3.
It seems like our usage of PyBuffer adds a big pressure over python's GC.
b
Thank you! looking forward to trying it when it's available
x
The comparison between python’s fs read is still similar 🥶
So the bottleneck is not the rust core but pyo3.
I’m trying to find a more efficient way to pass bytes between rust and python
Hi! here are some updates about this issue: https://github.com/apache/incubator-opendal/issues/3665
Reading a whole file into memory is a special cases in which the bottleneck is not syscall.
In our perf you will see the most time in this case is alloc & free memory.
For example,
copyout
and
clear_page_erms
are all kernel functions that operate on buffer between users and kernel.
The difference between python & rust speed could related to different allocator, hardware and it's systime config.
b
Interesting, I'm not familiar with the low level details but I initially thought that they'd just read from the same
memoryview
with very little overhead.
x
Yes, the overhead does low, and the bottleneck is not here.
Based on my benckmark of different platforms, the result could be very different.
Would you like to share your setup? Which system, which CPU and the memory's bandwidth?
b
Sure, it was on my laptop running Ubuntu 20.04 with Intel CPU. I'm on my phone right now but can help with the details later today. I'm not sure how to get memory's bandwidth though. Any recommendations?
x
You can run:
sudo dmidecode --type memory
b
Thanks, will share later today
x
We made some progress about that: https://github.com/apache/incubator-opendal/issues/3665#issuecomment-1829797790 This might be a CPU bug .
b
Oh that's interesting. Here's my specs as requested:
Copy code
shell
$ neofetch --backend off
OS: Ubuntu 20.04.6 LTS x86_64 
Host: XPS 15 9510 
Kernel: 5.15.0-83-generic 
Uptime: 2 days, 2 hours, 44 mins 
Packages: 2697 (dpkg), 14 (flatpak), 12 (snap) 
Shell: bash 5.0.17 
Resolution: 1920x1200 
DE: GNOME 
WM: Mutter 
WM Theme: Adwaita 
Theme: Yaru-dark [GTK2/3] 
Icons: Yaru [GTK2/3] 
Terminal: gnome-terminal 
CPU: 11th Gen Intel i7-11800H (16) @ 4.600GHz 
GPU: Intel TigerLake-H GT1 [UHD Graphics] 
GPU: NVIDIA GeForce RTX 3050 Ti Mobile 
Memory: 10237MiB / 15712MiB
The output of
dmidecode
command is too big so I shared it as attachment https://cdn.discordapp.com/attachments/1174840499576770560/1179144399393919096/dmidecode.txt?ex=6578b6cc&is=656641cc&hm=154a42460f9b7119b914ebdcb5e881c4c37947c621b7dc280918d5ebd7a36572&
I still don't get how the
0x20
offset magically speeds things up
x
I'm wirting a write up for this 😋
b
Looking forward to reading it, and thanks again for looking into this. Glad we managed to find something.
By the way, our next version of python binding will improve the read speed 🥰
b
Amazing! will definitely read
I do see the post on Reddit as well: https://www.reddit.com/r/rust/comments/186l8ff/rust_std_fs_slower_than_python_really/ and thanks for the shout-out
2 Views