Skip to content

Commit 5b078ad

Browse files
committed
feat: reverted native, python perf improvements, added benchmarking, profiling and security tests
1 parent bcb5946 commit 5b078ad

36 files changed

Lines changed: 4530 additions & 12009 deletions

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ __pycache__/
55

66
# C extensions
77
*.so
8+
*.so.*
89

910
# Distribution / packaging
1011
.Python
@@ -227,6 +228,7 @@ $RECYCLE.BIN/
227228
# Project-specific
228229
.benchmarks/
229230
.ruff_cache/
231+
benchmarks/out/bench_profiles.json
230232

231233
# Publishing artifacts (for development only)
232234
.twine/

Makefile

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
.PHONY: help clean build test rebuild install dev bench bench-compare bench-baseline
2+
3+
help:
4+
@echo "Available targets:"
5+
@echo " clean - Remove all build artifacts and compiled modules"
6+
@echo " build - Build C extensions in-place"
7+
@echo " rebuild - Clean and rebuild everything"
8+
@echo " test - Run pytest"
9+
@echo " install - Install package in development mode"
10+
@echo " dev - Clean, rebuild, and run tests"
11+
@echo ""
12+
@echo "Benchmarks:"
13+
@echo " bench - Run benchmarks"
14+
@echo " bench-compare - Compare with baseline"
15+
@echo " bench-baseline - Set current results as baseline"
16+
17+
clean:
18+
@echo "Cleaning build artifacts..."
19+
rm -rf build/ dist/ *.egg-info/ .pytest_cache/
20+
rm -f *.so *.dylib *.so.disabled
21+
rm -f tsrkit_types/*.so tsrkit_types/*.dylib tsrkit_types/*.so.disabled
22+
find . -type d -name __pycache__ -exec rm -rf {} + 2>/dev/null || true
23+
find . -type f -name "*.pyc" -delete 2>/dev/null || true
24+
@echo "Clean complete."
25+
26+
build:
27+
@echo "Building C extensions..."
28+
python setup.py build_ext --inplace
29+
@echo "Build complete."
30+
31+
rebuild: clean build
32+
33+
test:
34+
@echo "Running tests..."
35+
uv run --frozen pytest
36+
37+
install:
38+
@echo "Installing in development mode..."
39+
uv pip install -e ".[dev]"
40+
41+
dev: rebuild test
42+
@echo "Development setup complete."
43+
44+
# Benchmarks
45+
bench:
46+
@uv run python benchmarks/bench_types.py --no-profile $(ARGS)
47+
48+
bench-compare:
49+
@python benchmarks/compare.py benchmarks/out/bench_results.json benchmarks/out/bench_baseline.json
50+
51+
bench-baseline:
52+
@cp benchmarks/out/bench_results.json benchmarks/out/bench_baseline.json
53+
@echo "✅ Baseline updated from bench_results.json"

benchmark.md

Lines changed: 32 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,39 @@
11
# Benchmarks
22

3-
This document records the latest benchmark run and the Python/stdlib baselines used for comparison.
4-
All timings are **seconds for 20,000 runs** on the local machine; results are not directly comparable across hardware or Python versions.
3+
Compare performance between runs. All timings are **seconds for 20,000 runs**.
54

6-
## How To Run
5+
## Quick Start
76

87
```bash
9-
PYTHONPATH=. python3.12 benchmarks/bench_types.py
8+
make bench # Run benchmarks
9+
make bench-compare # Compare with baseline
10+
make bench-baseline # Set current as baseline
1011
```
1112

12-
Raw data is written to:
13-
- `benchmarks/out/bench_results.json`
14-
- `benchmarks/out/bench_profiles.json`
15-
16-
## Latest Results (Local)
17-
18-
**Run date:** 2026-02-04
19-
**Python:** 3.12.11
20-
**Runs:** 20,000
21-
22-
### TSRKit Types (selected)
23-
24-
| Case | init_s | encode_s | decode_s | json_encode_s | json_decode_s |
25-
| --- | --- | --- | --- | --- | --- |
26-
| Uint | 0.008567 | 0.004479 | 0.014276 | 0.002534 | 0.008913 |
27-
| U16 | 0.008530 | 0.004632 | 0.014242 | 0.002791 | 0.009892 |
28-
| Bytes(var,64B) | 0.004458 | 0.010630 | 0.008252 | 0.003609 | 0.010214 |
29-
| ByteArray | 0.003199 | 0.006915 | 0.006440 | 0.003522 | 0.009588 |
30-
| Bits(var,64b) | 0.011615 | 0.011080 | 0.016224 | 0.007608 | 0.024439 |
31-
| TypedArray[U16,10] | 0.014253 | 0.006967 | 0.017195 | 0.003189 | 0.152636 |
32-
| Dictionary[String,U16] | 0.023382 | 0.061991 | 0.149705 | 0.068052 | 0.179980 |
33-
34-
### Python/Stdlib Baselines (selected)
35-
36-
| Case | init_s | encode_s | decode_s |
37-
| --- | --- | --- | --- |
38-
| PyInt(varint) | 0.002778 | 0.006405 | 0.007207 |
39-
| PyU16(to_bytes) | 0.004112 | 0.003861 | 0.006797 |
40-
| PyBytes(var,64B) | 0.003516 | 0.005396 | 0.005538 |
41-
| PyByteArray(var,64B) | 0.003091 | 0.006694 | 0.006594 |
42-
| PyBits(var,64b) | 0.006438 | 0.088161 | 0.150918 |
43-
| PyArray('H',10) | 0.006611 | 0.008616 | 0.006599 |
44-
| PyDict[str,U16] | 0.003587 | 0.095823 | 0.077303 |
45-
46-
## Notes On Baselines
47-
48-
- `PyInt(varint)`: pure-Python varint encoder/decoder (same scheme as `Uint`).
49-
- `PyU16(to_bytes)`: `int.to_bytes` / `int.from_bytes` for fixed-width integers.
50-
- `PyBytes(var,64B)` / `PyByteArray(var,64B)`: length-prefixed bytes (varint length).
51-
- `PyBits(var,64b)`: list[bool] packed/unpacked to bytes in Python.
52-
- `PyArray('H',10)`: stdlib `array('H')` with `.tobytes()` / `.frombytes()`.
53-
- `PyDict[str,U16]`: sorted key encode; keys length-prefixed; values fixed 2-byte little-endian.
13+
Results: `benchmarks/out/bench_results.json` (14KB)
14+
15+
## Workflow
16+
17+
```bash
18+
# 1. Set baseline
19+
make bench
20+
make bench-baseline
21+
22+
# 2. Make changes...
23+
24+
# 3. Compare
25+
make bench
26+
make bench-compare
27+
```
28+
29+
## Custom runs
30+
31+
```bash
32+
make bench ARGS="--runs 1000 --no-profile"
33+
```
34+
35+
## What's Tested
36+
37+
**Types**: Integers, Bytes, Bits, Strings, Bool, Collections, Dictionary, Option, Choice, Enum, Structures
38+
39+
**Operations**: init, encode, decode, json_encode/decode, container ops (append/extend/pop/getitem)

0 commit comments

Comments
 (0)