New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Supporting alternative msgpack implementations #15
Comments
Hi @FaresAhmedb, Thanks for opening this! I'm actually quite curious about measuring performance improvements we'd get from this extra alternative as well. In the past this library was marked as limited by some commentators because it saves bandwidth (thanks to msgpack) by inducing a greater burden on the CPU due to msgpack-JSON serde, which some people found too big in some situations (I'd need to dig up those discussions tho). So a "faster on the CPU" option would definitely be interesting to explore. I think a pip extra sounds fair, but that and the wording actually depends on a broader choice: There's a few options on dependency detection strategies we can take, with different trade offs and assumptions. I'll list them below. What do you think is most appropriate given your knowledge of the Options:
|
The ormsgpack README says that it
I'm in favor of the Explicit option; It seems to not bring any issues a long the way and perfectly follows python's philosophy...
This is the problems I think the other options could bring:
Assuming the user wants to use try:
import ormsgpack as msgpack
except ImportError:
import msgpack
This would mean dropping support for PyPy (also Python2 but won't be the case since it doesn't support asgi/async anyway). |
@FaresAhmedb Thanks, that's useful info. Actually, I assume most users would be in a "I'm fine-tuning performance" mindset, which means writing some custom code would be acceptable — if not appropriate. So I wonder about another option: a public "backend" interface. Like this: class Packer(Protocol):
def packb(self, obj: Any) -> bytes: ...
def unpackb(self, data: bytes) -> Any: ... We'd add a import ormsgpack
import msgpack
class OptimizedPacker:
def packb(self, obj: Any) -> bytes:
option = ormsgpack.OPT_NAIVE_UTC | ormsgpack.OPT_SERIALIZE_NUMPY
return ormsgpack.packb(obj, option=option)
def unpackb(self, data: bytes) -> Any:
return msgpack.unpackb(data) Usage: from msgpack_asgi import MessagePackMiddleware
app = MessagePackMiddleware(..., packer=OptimizedPacker()) Or maybe, without classes, using import ormsgpack
from typing import Any
from msgpack_asgi import MessagePackMiddleware
def packb(obj: Any) -> bytes:
option = ormsgpack.OPT_NAIVE_UTC | ormsgpack.OPT_SERIALIZE_NUMPY
return ormsgpack.packb(obj, option=option)
app = MessagePackMiddleware(..., packb=packb) # Default `unpackb=partial(msgpack.unpackb, raw=False)` Thoughts? |
Very interesting! I like the latter implementation. Supporting But I assume this would be a breaking change, Right? |
@FaresAhmedb Nope, as far as I can tell we can make this change in a purely additive manner (merely two new optional arguments, |
@FaresAhmedb I think we're settled on the solution, then. :-) At this point anyone up for it can give it a try! |
@florimondmanca Sorry for the late reply, I was busy. I will open a PR now! 😃 |
@florimondmanca Just opened a pull request at #20 😀 |
Just stumbled upon this package (from the fast-api docs). I have no stake in this issue, but y'all may be interested in trying out The |
Thanks @jcrist, import msgpec
_enc = msgpec.Encoder()
_dec = msgpec.Decoder()
app = MessagePackMiddleware(..., packb=_enc.encode, unpackb=_dec.decode)
If this is all good, that's an interesting case to validate the callable-based design from #20. :-) |
ormsgpack is a faster alternative to python-msgpack and since speed is critical/required in Web/ASGI application I think it would be nice to have support for using ormsgpack!
My current idea is to provide ormsgpack as an extra/optional dependency to
msgpack-asgi
(installed likepip install msgpack-asgi[ormsgpack]
?) and provide two more classes to the public APIORMessagePackMiddleware
andORMessagePackResponse
if ormsgpack can be imported.I will happily create a PR and start working on this feature if this is something can be added to the project! 😄
The text was updated successfully, but these errors were encountered: