sled icon indicating copy to clipboard operation
sled copied to clipboard

Provide CFFI/PyO3 bindings for Python

Open thedrow opened this issue 5 years ago • 9 comments

The current Python bindings use ctypes which are not necessary on CPython and are slow on PyPy. In addition, the package in it's current form cannot be released to PyPi which is a shame. PyO3 provides a way to write CPython extensions. These extensions can be released as wheels which everyone can use. On PyPy CFFI is more efficient so we can provide CFFI bindings as well although PyO3 supports PyPy through PyPy's slower cpyext compatibility layer. Since the API surface of sled is small, a CFFI extension could be enough for both PyPy and CPython.

I'm willing to help in this regard if there's interest in it.

thedrow avatar Aug 05 '19 15:08 thedrow

Hey @thedrow thanks for the info! Python was my first "professional" language that I used at work almost 10 years ago but I never got into the FFI side of things, so I really appreciate the perspective on this.

How big is the performance impact of using PyO3 from pypy? If you can do 1 million function calls through it per second on one thread then that's more than enough for the current state of sled, and I'd prefer to minimize extra code unless it can significantly improve performance on a real use case.

spacejam avatar Aug 06 '19 06:08 spacejam

Well the answer to that question is tricky. We'll need to write that extension and measure. However, CFFI should be enough to implement the wrapper you wrote using ctypes. Is there something missing from the bindings currently?

thedrow avatar Aug 06 '19 07:08 thedrow

I probably neglected to mention that CFFI is supported on CPython as well.

thedrow avatar Aug 06 '19 07:08 thedrow

I think it's probably best to start with the option that results in the least code that needs to be maintained, and from there measure how much of the overall cpu time is spent handling ffi to measure if it would be worth the time to implement the second option as well.

The FFI code has not been touched in over a year, and both sled-native and the ctypes implementation are quite out of date. The first thing to do is update sled-native to use the latest sled master branch. I think as long as it can run the basic insert and remove commands then that's a good initial step. I might have time to update sled-native at some point in the next 2 weeks, but if you'd like to tackle it I can help answer questions, ideally on the discord chat to minimize latency but in this issue is fine too if you would prefer.

spacejam avatar Aug 07 '19 07:08 spacejam

Sure, what's the discord channel for this project?

thedrow avatar Aug 08 '19 08:08 thedrow

@thedrow https://discord.gg/Z6VsXds

spacejam avatar Aug 08 '19 14:08 spacejam

I haven't had the time to take a look into this. It is on my TODO list :)

thedrow avatar Sep 20 '19 19:09 thedrow

Hi, I'd like to try Sled on a python project for parallel writes from multiple processes (very tricky with RocksDB). Is there an update on this issue?

Hoeze avatar Sep 26 '21 13:09 Hoeze

Not yet but this patch is in my long-term plans :)

thedrow avatar Sep 27 '21 09:09 thedrow