Skip to content

Commit 79a5b09

Browse files
committed
feat: add opt-in Rust detection and 4.9 migration bridge
1 parent 46fd8b0 commit 79a5b09

33 files changed

Lines changed: 1826 additions & 79 deletions

‎.github/workflows/ci.yml‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,43 @@ concurrency:
1111
cancel-in-progress: true
1212

1313
jobs:
14+
rust-bridge:
15+
name: Rust bridge (${{ matrix.os }}, ${{ matrix.python-version }})
16+
runs-on: ${{ matrix.os }}
17+
strategy:
18+
fail-fast: false
19+
matrix:
20+
include:
21+
- os: ubuntu-latest
22+
python-version: "3.10"
23+
- os: ubuntu-latest
24+
python-version: "3.14"
25+
- os: macos-latest
26+
python-version: "3.12"
27+
- os: windows-latest
28+
python-version: "3.12"
29+
steps:
30+
- uses: actions/checkout@v6
31+
- uses: actions/setup-python@v6
32+
with:
33+
python-version: ${{ matrix.python-version }}
34+
- name: Build and install the wheel with native extra
35+
shell: bash
36+
run: |
37+
python -m pip install build
38+
python -m build --wheel
39+
python -c "import glob, subprocess, sys; wheel = glob.glob('dist/*.whl')[0]; subprocess.check_call([sys.executable, '-m', 'pip', 'install', wheel + '[test,cli,rust]'])"
40+
- name: Verify installed package outside checkout imports
41+
run: python -I scripts/check_rust_install.py
42+
- name: Test compatibility, routing, preview and native parity
43+
run: python -m pytest tests/test_contract_481.py tests/test_rust_backend.py tests/test_api_bridge_49.py tests/test_rust_contract.py -q
44+
- name: Generate native parity report
45+
run: python -m tests.rust_contract --output rust-parity.json
46+
- uses: actions/upload-artifact@v4
47+
with:
48+
name: rust-parity-${{ matrix.os }}-${{ matrix.python-version }}
49+
path: rust-parity.json
50+
1451
lint:
1552
runs-on: ubuntu-latest
1653
steps:

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ docs/*
6161
!docs/make.bat
6262
!docs/optional-surfaces.rst
6363
!docs/migration-4.8.1-contract.md
64+
!docs/migration-4.9.md
6465
!docs/agents/
6566
!docs/agents/**
6667
!docs/audit/

‎CHANGELOG.MD‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,24 @@
22

33
## [Unreleased]
44

5+
#### Added
6+
7+
- Experimental `datafog[rust]` detection with keyword-only `backend="rust"`
8+
on scan/redact entry points; Python remains the default. Core 0.3.1 is pinned,
9+
German requests are explicitly unsupported, and ML composition is unchanged.
10+
- `datafog.compat.v4` preserves the existing scan/redact facade and result classes;
11+
`datafog.v5` previews actual Core types and transformation APIs.
12+
- Native parity tests, installed-wheel CI checks, and public-call benchmarks.
13+
14+
#### Deprecated
15+
16+
- `detect()` and `process()` are now scheduled for removal in 5.0. This explicitly
17+
revises the earlier promise to retain these shims throughout 5.x. They continue
18+
working in 4.9; migrate to scan/redact and review transformation differences.
19+
- OCR/image and Spark/distributed surfaces remain available in 4.9 with use-time
20+
notices and are scheduled for removal in 5.0. Users needing those features can
21+
remain on the final 4.x release.
22+
523
#### Fixed
624

725
- Allow installation on Python 3.14 and certify the core SDK and CLI with

‎README.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,10 @@ Telemetry does not include input text or detected PII values.
256256

257257
## Development
258258

259+
The [4.9 migration guide](docs/migration-4.9.md) explains opt-in Rust detection,
260+
the native `datafog.v5` preview, and the revised 5.0 retirement schedule for
261+
`detect`/`process`, OCR, and Spark. The Python detector remains the default.
262+
259263
The [4.8.1 compatibility contract](docs/migration-4.8.1-contract.md) records
260264
published Python behavior for the Rust migration, with frozen fixtures and
261265
instructions for independently reproducing them from the release wheel.
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
"""Measure public Python/Rust bridge calls, including adaptation and cold startup."""
2+
3+
import argparse
4+
import importlib.metadata
5+
import json
6+
import os
7+
import platform
8+
import statistics
9+
import subprocess
10+
import sys
11+
import time
12+
from pathlib import Path
13+
14+
os.environ["DATAFOG_NO_TELEMETRY"] = "1"
15+
16+
PAYLOADS = {
17+
"short": ("Contact alice@example.com", 200),
18+
"mixed": (
19+
"Email alice@example.com or call (555) 123-4567. "
20+
"SSN 123-45-6789; card 4111 1111 1111 1111; host 192.168.1.1.",
21+
100,
22+
),
23+
"large_sparse": ("ordinary prose " * 70_000 + " alice@example.com", 3),
24+
}
25+
26+
27+
def measure(fn, text, backend, loops, samples):
28+
result = fn(text, backend=backend)
29+
durations = []
30+
for _ in range(samples):
31+
start = time.perf_counter()
32+
for _ in range(loops):
33+
fn(text, backend=backend)
34+
durations.append((time.perf_counter() - start) / loops * 1_000_000)
35+
return {
36+
"median_us": statistics.median(durations),
37+
"samples_us": durations,
38+
"entities": len(result.entities),
39+
"iterations_per_sample": loops,
40+
}
41+
42+
43+
def cold_start(backend, samples):
44+
source = (
45+
"import datafog; "
46+
f"datafog.scan('Contact alice@example.com', backend={backend!r})"
47+
)
48+
durations = []
49+
for _ in range(samples):
50+
start = time.perf_counter()
51+
subprocess.run([sys.executable, "-c", source], check=True, capture_output=True)
52+
durations.append((time.perf_counter() - start) * 1_000)
53+
return {"median_ms": statistics.median(durations), "samples_ms": durations}
54+
55+
56+
def main():
57+
import datafog
58+
59+
parser = argparse.ArgumentParser(description=__doc__)
60+
parser.add_argument("--samples", type=int, default=5)
61+
parser.add_argument("--output", type=Path, required=True)
62+
args = parser.parse_args()
63+
if args.samples < 1:
64+
parser.error("samples must be positive")
65+
report = {
66+
"python": platform.python_version(),
67+
"platform": platform.platform(),
68+
"core_version": importlib.metadata.version("datafog-core"),
69+
"method": "One warmup call, median repeated calls; no ML. Cold = process+import+first scan.",
70+
"warm": [],
71+
"cold": {},
72+
}
73+
for name, (text, loops) in PAYLOADS.items():
74+
for operation in ("scan", "redact"):
75+
row = {
76+
"payload": name,
77+
"operation": operation,
78+
"utf8_bytes": len(text.encode()),
79+
}
80+
for backend in ("python", "rust"):
81+
row[backend] = measure(
82+
getattr(datafog, operation), text, backend, loops, args.samples
83+
)
84+
report["warm"].append(row)
85+
for backend in ("python", "rust"):
86+
report["cold"][backend] = cold_start(backend, args.samples)
87+
args.output.write_text(json.dumps(report, indent=2) + "\n")
88+
print(f"Saved end-to-end measurements to {args.output}")
89+
90+
91+
if __name__ == "__main__":
92+
main()

‎benchmarks/results-4.9.json‎

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
{
2+
"python": "3.12.13",
3+
"platform": "macOS-26.6.2-arm64-arm-64bit",
4+
"core_version": "0.3.1",
5+
"method": "One warmup call, median repeated calls; no ML. Cold = process+import+first scan.",
6+
"warm": [
7+
{
8+
"payload": "short",
9+
"operation": "scan",
10+
"utf8_bytes": 25,
11+
"python": {
12+
"median_us": 19.735415116883814,
13+
"samples_us": [
14+
19.692290225066245, 19.87395982723683, 19.735415116883814,
15+
19.453955464996397, 20.307295490056276
16+
],
17+
"entities": 1,
18+
"iterations_per_sample": 200
19+
},
20+
"rust": {
21+
"median_us": 2.3012497695162892,
22+
"samples_us": [
23+
2.292499993927777, 2.3364595836028457, 2.3025000700727105,
24+
2.3012497695162892, 2.210834645666182
25+
],
26+
"entities": 1,
27+
"iterations_per_sample": 200
28+
}
29+
},
30+
{
31+
"payload": "short",
32+
"operation": "redact",
33+
"utf8_bytes": 25,
34+
"python": {
35+
"median_us": 22.42000016849488,
36+
"samples_us": [
37+
22.9199999012053, 21.847704774700105, 22.270414629019797,
38+
22.495624725706875, 22.42000016849488
39+
],
40+
"entities": 1,
41+
"iterations_per_sample": 200
42+
},
43+
"rust": {
44+
"median_us": 3.6647950764745474,
45+
"samples_us": [
46+
3.821664722636342, 3.54208517819643, 3.6647950764745474,
47+
3.674789913929999, 3.5991653567180037
48+
],
49+
"entities": 1,
50+
"iterations_per_sample": 200
51+
}
52+
},
53+
{
54+
"payload": "mixed",
55+
"operation": "scan",
56+
"utf8_bytes": 108,
57+
"python": {
58+
"median_us": 39.49917037971318,
59+
"samples_us": [
60+
39.52000057324767, 39.80332985520363, 39.49917037971318,
61+
38.85417012497783, 39.10957952030003
62+
],
63+
"entities": 5,
64+
"iterations_per_sample": 100
65+
},
66+
"rust": {
67+
"median_us": 7.581659592688084,
68+
"samples_us": [
69+
7.581659592688084, 7.61916977353394, 7.474579615518451,
70+
7.530420552939177, 7.725419709458948
71+
],
72+
"entities": 5,
73+
"iterations_per_sample": 100
74+
}
75+
},
76+
{
77+
"payload": "mixed",
78+
"operation": "redact",
79+
"utf8_bytes": 108,
80+
"python": {
81+
"median_us": 42.573329992592335,
82+
"samples_us": [
83+
43.91749971546233, 42.02042007818818, 42.573329992592335,
84+
42.929170886054635, 41.73457971774042
85+
],
86+
"entities": 5,
87+
"iterations_per_sample": 100
88+
},
89+
"rust": {
90+
"median_us": 12.040419969707727,
91+
"samples_us": [
92+
11.556250974535942, 12.040419969707727, 11.692079715430737,
93+
12.406249297782779, 12.319160159677267
94+
],
95+
"entities": 5,
96+
"iterations_per_sample": 100
97+
}
98+
},
99+
{
100+
"payload": "large_sparse",
101+
"operation": "scan",
102+
"utf8_bytes": 1050018,
103+
"python": {
104+
"median_us": 121914.90270197392,
105+
"samples_us": [
106+
120976.49998031557, 121914.90270197392, 124721.45829815418,
107+
123755.50003101428, 121670.05562999596
108+
],
109+
"entities": 1,
110+
"iterations_per_sample": 3
111+
},
112+
"rust": {
113+
"median_us": 5334.583343937993,
114+
"samples_us": [
115+
5415.152680749695, 5282.319655331473, 5320.333332444231,
116+
5334.583343937993, 5347.4583352605505
117+
],
118+
"entities": 1,
119+
"iterations_per_sample": 3
120+
}
121+
},
122+
{
123+
"payload": "large_sparse",
124+
"operation": "redact",
125+
"utf8_bytes": 1050018,
126+
"python": {
127+
"median_us": 122195.88865991682,
128+
"samples_us": [
129+
122800.74999822925, 123273.65266780059, 122195.88865991682,
130+
120245.19433422635, 121185.16664486378
131+
],
132+
"entities": 1,
133+
"iterations_per_sample": 3
134+
},
135+
"rust": {
136+
"median_us": 5254.485993646085,
137+
"samples_us": [
138+
5248.666663343708, 5268.95831959943, 5254.485993646085,
139+
5269.472332050403, 5233.638648254176
140+
],
141+
"entities": 1,
142+
"iterations_per_sample": 3
143+
}
144+
}
145+
],
146+
"cold": {
147+
"python": {
148+
"median_ms": 80.99587506148964,
149+
"samples_ms": [
150+
79.40420799423009, 78.51329201366752, 80.99587506148964,
151+
82.50495803076774, 83.314000046812
152+
]
153+
},
154+
"rust": {
155+
"median_ms": 81.11529203597456,
156+
"samples_ms": [
157+
80.53745795041323, 81.226791953668, 81.9647500757128, 81.11529203597456,
158+
80.21229202859104
159+
]
160+
}
161+
}
162+
}

0 commit comments

Comments
 (0)