agronholm / agronholm/cbor2

Significantly slower rust implementation compared to minicbor

Abierto
#310 3 comentarios 0 reacciones 0 asignados Ver en GitHub
bug
Lenguaje dominante
Rust
Estrellas
305
Forks
79
Merge medio
21 h 59 min
PR fusionados (30 d)
2

Descripción

### Things to check first

- [x] I have searched the existing issues and didn't find my bug already reported there

- [x] I have checked that my bug is still present in the latest release

### cbor2 version

6.1.2

### Python version

3.12

### What happened?

The new rust implementation appears to be quite slow compared to [minicbor](https://github.com/twittner/minicbor).

The [msglc](https://github.com/TLCFEM/msglc) serializes JSON objects with additional structure metadata.
It effectively calls minicbor, see implementation [here](https://github.com/TLCFEM/msglc/blob/master/src/cbor.rs).

### How can we reproduce the bug?

The following is a benchcmark using pytest.
To run, install the dependencies.

```bash
pip install msglc cbor2 pytest pytest-benchmark
```

```python
from json import loads
from urllib.request import urlopen

import cbor2
import pytest

from msglc import dump
from msglc.codec import CBORCodec

@pytest.fixture(scope="module")
def repo_data() -> dict:
with urlopen(
"https://conda.anaconda.org/conda-forge/noarch/repodata.json"
) as response:
return loads(response.read().decode("utf-8"))

def test_conda_data_msglc(tmpdir, benchmark, repo_data):
with tmpdir.as_cwd():
benchmark(dump, "data.cbor", repo_data, backend="rust", packer=CBORCodec)

def test_conda_data_cbor2(tmpdir, benchmark, repo_data):
def reference_dump():
with open("data.cbor", "wb") as f:
cbor2.dump(repo_data, f)

with tmpdir.as_cwd():
benchmark(reference_dump)
```

It looks like the cbor2 implementation is at least 60% slower.
Considering that msglc also processes additional metadata, the raw minicbor performance would be faster.
The following is a typical comparison.

```text
============================================================================================ test session starts ============================================================================================
platform linux -- Python 3.12.13, pytest-9.0.3, pluggy-1.6.0
benchmark: 5.2.3 (defaults: timer=time.perf_counter disable_gc=False min_rounds=5 min_time=0.000005 max_time=1.0 calibration_precision=10 warmup=False warmup_iterations=100000)
rootdir: /home/theodore/Projects/msglc
configfile: pyproject.toml
plugins: asyncio-1.4.0, cov-7.1.0, benchmark-5.2.3
asyncio: mode=Mode.STRICT, debug=False, asyncio_default_fixture_loop_scope=function, asyncio_default_test_loop_scope=function
collected 2 items

tests/benchmark.py .. [100%]

----------------------------------------------------------------------------------------- benchmark: 2 tests -----------------------------------------------------------------------------------------
Name (time in ms) Min Max Mean StdDev Median IQR Outliers OPS Rounds Iterations
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
test_conda_data_msglc 939.0303 (1.0) 987.3192 (1.0) 960.4481 (1.0) 18.3903 (1.0) 959.2344 (1.0) 25.5546 (1.0) 2;0 1.0412 (1.0) 5 1
test_conda_data_cbor2 1,528.1846 (1.63) 1,618.8248 (1.64) 1,562.3435 (1.63) 35.7466 (1.94) 1,554.2367 (1.62) 48.3697 (1.89) 1;0 0.6401 (0.61) 5 1
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Legend:
Outliers: 1 Standard Deviation from Mean; 1.5 IQR (InterQuartile Range) from 1st Quartile and 3rd Quartile.
OPS: Operations Per Second, computed as 1 / Mean
============================================================================================ 2 passed in 48.73s =============================================================================================
```

Guía de contribución

No hay ninguna guía de contribución indexada para este repositorio

Evaluación

Este issue todavía no se ha evaluado.

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.