agronholm / agronholm/cbor2

Significantly slower rust implementation compared to minicbor

未關閉
#310 3 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視
bug
主要語言
Rust
星號
305
分支
79
平均合併
21 小時 59 分鐘
30 天內合併 PR
2

描述

### 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 =============================================================================================
```

貢獻指南

這個儲存庫沒有索引到貢獻指南

評估

這個 Issue 還沒有評估資料。

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。