question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Supporting alternative msgpack implementations

See original GitHub issue

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 like pip install msgpack-asgi[ormsgpack]?) and provide two more classes to the public API ORMessagePackMiddleware and ORMessagePackResponse 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! 😄

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:11 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
FaresAhmedbcommented, Sep 20, 2021

Very interesting! I like the latter implementation. Supporting ormsgpack directly into the library wouldn’t be needed then. There’s also a lot of MessagePack flavors out there like u-msgpack-python (A Pure Python implementation) so that would be definitely a win win for everyone.

But I assume this would be a breaking change, Right?

1reaction
florimondmancacommented, Sep 20, 2021

@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 MsgPacker for our current behavior, and advanced users could do all sorts of customized things, e.g. what you suggested, ormsgpack for serialization vs msgpack for deserialization:

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 packb=... and unpackb=... options (I think I prefer this style):

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?

Read more comments on GitHub >

github_iconTop Results From Across the Web

MessagePack: It's like JSON. but fast and small.
MessagePack is an efficient binary serialization format. It lets you exchange data among multiple languages like JSON. But it's faster and smaller. Small ......
Read more >
MessagePack Alternatives and Similar Projects - LibHunt
Which is the best alternative to MessagePack? Based on common mentions it is: ✓FlatBuffers, ✓Protobuf, ✓Cereal, ✓Boost.Serialization, ✓ArduinoJson or ...
Read more >
MessagePack: an alternative to JSON? - Facile.it Engineering
MessagePack is an efficient binary serialization format. It lets you exchange data among multiple languages like JSON.
Read more >
MessagePack (XML/JSON alternative) - Spriter - BrashMonkey
While doing research on JSON, I stumbled upon the MessagePack format. It's a binary object serialization system with support for quite a few ......
Read more >
msgpack-asgi - PyPI
msgpack -asgi supports customizing the default encoding/decoding implementation. This is useful for fine-tuning application performance via an alternative ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found