Skip to content

Faster message serialization #1064

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

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open

Conversation

fleming79
Copy link

@fleming79 fleming79 commented Jun 9, 2025

This PR provides faster pack and unpack defaults by utilizing orjson a "fast, correct JSON library".

Support is also added for msgpack.

New functions

  • orjson_packer
  • orjson_unpacker
  • msgpack_packer
  • msgpack_unpacker

Performance comparison

image

Code

import datetime
from jupyter_client import session
msg = {'header': {'msg_id': '7bd90b92-3625dcab810fadd2f13d2c53_139086_1',
  'msg_type': 'msg',
  'username': 'username',
  'session': '7bd90b92-3625dcab810fadd2f13d2c53',
  'date': datetime.datetime(2025, 6, 9, 3, 46, 52, 133647, tzinfo=datetime.timezone.utc),
  'version': '5.3'},
 'msg_id': '7bd90b92-3625dcab810fadd2f13d2c53_139086_1',
 'msg_type': 'msg',
 'parent_header': {'msg_id': '7bd90b92-3625dcab810fadd2f13d2c53_139086_0',
  'msg_type': 'msg',
  'username': 'username',
  'session': '7bd90b92-3625dcab810fadd2f13d2c53',
  'date': datetime.datetime(2025, 6, 9, 3, 46, 52, 133525, tzinfo=datetime.timezone.utc),
  'version': '5.3'},
 'content': {'t': datetime.datetime(2025, 6, 9, 3, 46, 52, 133280, tzinfo=datetime.timezone.utc)},
 'metadata': {'t': datetime.datetime(2025, 6, 9, 3, 46, 52, 133305, tzinfo=datetime.timezone.utc)}}


%timeit session.orjson_packer(msg)

%timeit session.json_packer(msg)

%timeit session.msgpack_packer(msg)

%timeit session.pickle_packer(msg)

packed = session.orjson_packer(msg)

%timeit session.orjson_unpacker(packed)

%timeit session.json_unpacker(packed)

packed = session.msgpack_packer(msg)
%timeit session.msgpack_unpacker(packed)

packed = session.pickle_packer(msg)
%timeit session.pickle_unpacker(packed)

@fleming79
Copy link
Author

The failing tests on Windows appear to be unrelated to this PR:

I've run both the main branch and this PR on Windows locally and observe similar errors related to "unclosed sockets".

If the test is run individually it passes but when running in groups the error can move around, though generally the reported error is the same.

c:\code\jupyter_client\tests\test_multikernelmanager.py::TestKernelManager::test_start_parallel_thread_kernels failed: cls = <class '_pytest.runner.CallInfo'>
func = <function call_and_report.<locals>.<lambda> at 0x0000013B0182FB00>
when = 'call'
reraise = (<class '_pytest.outcomes.Exit'>, <class 'KeyboardInterrupt'>)

    @classmethod
    def from_call(
        cls,
        func: Callable[[], TResult],
        when: Literal["collect", "setup", "call", "teardown"],
        reraise: Optional[
            Union[Type[BaseException], Tuple[Type[BaseException], ...]]
        ] = None,
    ) -> "CallInfo[TResult]":
        """Call func, wrapping the result in a CallInfo.
    
        :param func:
            The function to call. Called without arguments.
        :param when:
            The phase in which the function is called.
        :param reraise:
            Exception or exceptions that shall propagate if raised by the
            function, instead of being wrapped in the CallInfo.
        """
        excinfo = None
        start = timing.time()
        precise_start = timing.perf_counter()
        try:
>           result: Optional[TResult] = func()

cls        = <class '_pytest.runner.CallInfo'>
duration   = 0.7103413999939221
excinfo    = <ExceptionInfo PytestUnraisableExceptionWarning('Exception ignored in: <socket.socket fd=-1, family=2, type=1, proto=0...et.socket fd=2124, family=2, type=1, proto=0, laddr=(\'127.0.0.1\', 59458), raddr=(\'127.0.0.1\', 59459)>\n') tblen=11>
func       = <function call_and_report.<locals>.<lambda> at 0x0000013B0182FB00>
precise_start = 60518.1109472
precise_stop = 60518.8212886
reraise    = (<class '_pytest.outcomes.Exit'>, <class 'KeyboardInterrupt'>)
result     = None
start      = 1749603690.353999
stop       = 1749603691.0643415
when       = 'call'

c:\code\jupyter_client\.venv\Lib\site-packages\_pytest\runner.py:340: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
c:\code\jupyter_client\.venv\Lib\site-packages\_pytest\runner.py:240: in <lambda>
    lambda: runtest_hook(item=item, **kwds), when=when, reraise=reraise
        item       = <TestCaseFunction test_start_parallel_thread_kernels>
        kwds       = {}
        runtest_hook = <HookCaller 'pytest_runtest_call'>
c:\code\jupyter_client\.venv\Lib\site-packages\pluggy\_hooks.py:512: in __call__
    return self._hookexec(self.name, self._hookimpls.copy(), kwargs, firstresult)
        firstresult = False
        kwargs     = {'item': <TestCaseFunction test_start_parallel_thread_kernels>}
        self       = <HookCaller 'pytest_runtest_call'>
c:\code\jupyter_client\.venv\Lib\site-packages\pluggy\_manager.py:120: in _hookexec
    return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
        firstresult = False
        hook_name  = 'pytest_runtest_call'
        kwargs     = {'item': <TestCaseFunction test_start_parallel_thread_kernels>}
        methods    = [<HookImpl plugin_name='runner', plugin=<module '_pytest.runner' from 'c:\\code\\jupyter_client\\.venv\\Lib\\site-pack...nraisableexception' from 'c:\\code\\jupyter_client\\.venv\\Lib\\site-packages\\_pytest\\unraisableexception.py'>>, ...]
        self       = <_pytest.config.PytestPluginManager object at 0x0000013B7AD034D0>
c:\code\jupyter_client\.venv\Lib\site-packages\_pytest\threadexception.py:87: in pytest_runtest_call
    yield from thread_exception_runtest_hook()
c:\code\jupyter_client\.venv\Lib\site-packages\_pytest\threadexception.py:63: in thread_exception_runtest_hook
    yield
        cm         = <_pytest.threadexception.catch_threading_exception object at 0x0000013B018048F0>
c:\code\jupyter_client\.venv\Lib\site-packages\_pytest\unraisableexception.py:90: in pytest_runtest_call
    yield from unraisable_exception_runtest_hook()
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

    def unraisable_exception_runtest_hook() -> Generator[None, None, None]:
        with catch_unraisable_exception() as cm:
            try:
                yield
            finally:
                if cm.unraisable:
                    if cm.unraisable.err_msg is not None:
                        err_msg = cm.unraisable.err_msg
                    else:
                        err_msg = "Exception ignored in"
                    msg = f"{err_msg}: {cm.unraisable.object!r}\n\n"
                    msg += "".join(
                        traceback.format_exception(
                            cm.unraisable.exc_type,
                            cm.unraisable.exc_value,
                            cm.unraisable.exc_traceback,
                        )
                    )
>                   warnings.warn(pytest.PytestUnraisableExceptionWarning(msg))
E                   pytest.PytestUnraisableExceptionWarning: Exception ignored in: <socket.socket fd=-1, family=2, type=1, proto=0>
E                   
E                   Traceback (most recent call last):
E                     File "c:\code\jupyter_client\.venv\Lib\site-packages\traitlets\traitlets.py", line 632, in get
E                       value = obj._trait_values[self.name]
E                               ~~~~~~~~~~~~~~~~~^^^^^^^^^^^
E                   KeyError: 'kernel_spec_manager'
E                   
E                   During handling of the above exception, another exception occurred:
E                   
E                   Traceback (most recent call last):
E                     File "c:\code\jupyter_client\.venv\Lib\site-packages\traitlets\config\configurable.py", line 179, in _load_config
E                       with self.hold_trait_notifications():
E                            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^
E                   ResourceWarning: unclosed <socket.socket fd=2124, family=2, type=1, proto=0, laddr=('127.0.0.1', 59458), raddr=('127.0.0.1', 59459)>

cm         = <_pytest.unraisableexception.catch_unraisable_exception object at 0x0000013B018055B0>
err_msg    = 'Exception ignored in'
msg        = 'Exception ignored in: <socket.socket fd=-1, family=2, type=1, proto=0>\n\nTraceback (most recent call last):\n  File ...losed <socket.socket fd=2124, family=2, type=1, proto=0, laddr=(\'127.0.0.1\', 59458), raddr=(\'127.0.0.1\', 59459)>\n'

c:\code\jupyter_client\.venv\Lib\site-packages\_pytest\unraisableexception.py:80: PytestUnraisableExceptionWarning

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants