pidgin-codec 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pidgin_codec-0.1.0/LICENSE +21 -0
- pidgin_codec-0.1.0/PKG-INFO +297 -0
- pidgin_codec-0.1.0/README.md +262 -0
- pidgin_codec-0.1.0/pyproject.toml +52 -0
- pidgin_codec-0.1.0/setup.cfg +4 -0
- pidgin_codec-0.1.0/setup.py +12 -0
- pidgin_codec-0.1.0/src/pidgin/__init__.py +47 -0
- pidgin_codec-0.1.0/src/pidgin/_codec.py +132 -0
- pidgin_codec-0.1.0/src/pidgin/_fast.c +541 -0
- pidgin_codec-0.1.0/src/pidgin/_prompts.py +92 -0
- pidgin_codec-0.1.0/src/pidgin/_protocol.py +39 -0
- pidgin_codec-0.1.0/src/pidgin/_types.py +25 -0
- pidgin_codec-0.1.0/src/pidgin/channel.py +168 -0
- pidgin_codec-0.1.0/src/pidgin/contrib/__init__.py +1 -0
- pidgin_codec-0.1.0/src/pidgin/contrib/_engine.py +185 -0
- pidgin_codec-0.1.0/src/pidgin/contrib/django.py +142 -0
- pidgin_codec-0.1.0/src/pidgin/contrib/fastapi.py +201 -0
- pidgin_codec-0.1.0/src/pidgin/py.typed +0 -0
- pidgin_codec-0.1.0/src/pidgin/ratchet.py +227 -0
- pidgin_codec-0.1.0/src/pidgin/schema_codec.py +820 -0
- pidgin_codec-0.1.0/src/pidgin_codec.egg-info/PKG-INFO +297 -0
- pidgin_codec-0.1.0/src/pidgin_codec.egg-info/SOURCES.txt +29 -0
- pidgin_codec-0.1.0/src/pidgin_codec.egg-info/dependency_links.txt +1 -0
- pidgin_codec-0.1.0/src/pidgin_codec.egg-info/requires.txt +15 -0
- pidgin_codec-0.1.0/src/pidgin_codec.egg-info/top_level.txt +1 -0
- pidgin_codec-0.1.0/tests/test_channel.py +76 -0
- pidgin_codec-0.1.0/tests/test_engine.py +92 -0
- pidgin_codec-0.1.0/tests/test_evolution.py +187 -0
- pidgin_codec-0.1.0/tests/test_protocol.py +35 -0
- pidgin_codec-0.1.0/tests/test_ratchet.py +63 -0
- pidgin_codec-0.1.0/tests/test_schema_codec.py +86 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Ivo Gergov
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pidgin-codec
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Runtime-learned compression that beats Protobuf — no .proto files, no codegen, zero config
|
|
5
|
+
Author: Ivo Gergov
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Keywords: compression,json,api,protobuf,binary,schema,codec
|
|
8
|
+
Classifier: Development Status :: 4 - Beta
|
|
9
|
+
Classifier: Intended Audience :: Developers
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
16
|
+
Classifier: Topic :: Internet :: WWW/HTTP
|
|
17
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
18
|
+
Classifier: Typing :: Typed
|
|
19
|
+
Requires-Python: >=3.10
|
|
20
|
+
Description-Content-Type: text/markdown
|
|
21
|
+
License-File: LICENSE
|
|
22
|
+
Requires-Dist: brotli>=1.1.0
|
|
23
|
+
Provides-Extra: crypto
|
|
24
|
+
Requires-Dist: cryptography>=42.0; extra == "crypto"
|
|
25
|
+
Provides-Extra: benchmark
|
|
26
|
+
Requires-Dist: zstandard>=0.23.0; extra == "benchmark"
|
|
27
|
+
Requires-Dist: msgpack>=1.0; extra == "benchmark"
|
|
28
|
+
Requires-Dist: orjson>=3.9; extra == "benchmark"
|
|
29
|
+
Requires-Dist: protobuf>=5.0; extra == "benchmark"
|
|
30
|
+
Requires-Dist: grpcio-tools>=1.60; extra == "benchmark"
|
|
31
|
+
Provides-Extra: dev
|
|
32
|
+
Requires-Dist: pytest>=8.0; extra == "dev"
|
|
33
|
+
Requires-Dist: pytest-asyncio>=0.24; extra == "dev"
|
|
34
|
+
Dynamic: license-file
|
|
35
|
+
|
|
36
|
+
# Pidgin
|
|
37
|
+
|
|
38
|
+
[](LICENSE)
|
|
39
|
+
[](https://python.org)
|
|
40
|
+
|
|
41
|
+
**Runtime-learned compression that beats Protobuf.** No `.proto` files, no codegen, zero config. One line to enable in your web server.
|
|
42
|
+
|
|
43
|
+
---
|
|
44
|
+
|
|
45
|
+
## How It Works
|
|
46
|
+
|
|
47
|
+
1. Pidgin observes your JSON API responses and learns the schema automatically
|
|
48
|
+
2. It strips all keys (both sides know the schema) and encodes values with type-optimal binary
|
|
49
|
+
3. Brotli compression on top for maximum density
|
|
50
|
+
4. Clients negotiate via `Accept-Encoding: pidgin`
|
|
51
|
+
|
|
52
|
+
**Result:** 10-19% of original JSON size. Smaller than gzip, brotli, and Protobuf+zstd.
|
|
53
|
+
|
|
54
|
+
## Quick Start
|
|
55
|
+
|
|
56
|
+
### Web Server (one line)
|
|
57
|
+
|
|
58
|
+
```nginx
|
|
59
|
+
# nginx
|
|
60
|
+
pidgin on;
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
```lua
|
|
64
|
+
-- Kong plugin
|
|
65
|
+
plugins = { { name = "pidgin" } }
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
```python
|
|
69
|
+
# FastAPI
|
|
70
|
+
from pidgin.contrib.fastapi import PidginMiddleware
|
|
71
|
+
app.add_middleware(PidginMiddleware)
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
```python
|
|
75
|
+
# Django
|
|
76
|
+
MIDDLEWARE = ["pidgin.contrib.django.PidginMiddleware"]
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
Zero changes to your backend code. Pidgin auto-learns, auto-compresses, auto-evolves.
|
|
80
|
+
|
|
81
|
+
### Python Library
|
|
82
|
+
|
|
83
|
+
```bash
|
|
84
|
+
pip install pidgin
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
```python
|
|
88
|
+
from pidgin import SchemaCodec
|
|
89
|
+
|
|
90
|
+
codec = SchemaCodec.learn(sample_records) # learn from 20-50 samples
|
|
91
|
+
compressed = codec.compress(records) # 10-19% of JSON size
|
|
92
|
+
original = codec.decompress(compressed) # lossless roundtrip
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
### JavaScript Client
|
|
96
|
+
|
|
97
|
+
```bash
|
|
98
|
+
npm install @pidgin/codec
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
```typescript
|
|
102
|
+
import { PidginClient } from '@pidgin/codec';
|
|
103
|
+
|
|
104
|
+
const client = new PidginClient({
|
|
105
|
+
baseUrl: 'https://api.example.com',
|
|
106
|
+
profiles: { users: profileJson },
|
|
107
|
+
});
|
|
108
|
+
const users = await client.get('/api/users', 'users');
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
## Benchmarks
|
|
112
|
+
|
|
113
|
+
All benchmarks: same machine, same data, single-threaded. Lower is better.
|
|
114
|
+
|
|
115
|
+
### Compression Ratio (% of original JSON)
|
|
116
|
+
|
|
117
|
+
| Dataset | gzip | brotli | Protobuf+zstd | **Pidgin** |
|
|
118
|
+
|---|---|---|---|---|
|
|
119
|
+
| Users x1000 | 21.4% | 19.5% | 20.3% | **18.9%** |
|
|
120
|
+
| Orders x500 (nested) | 19.1% | 17.5% | 17.4% | **16.1%** |
|
|
121
|
+
| Events x5000 | 13.6% | 12.3% | 12.3% | **10.8%** |
|
|
122
|
+
|
|
123
|
+
### Speed (ms, C extension)
|
|
124
|
+
|
|
125
|
+
| Dataset | brotli | zstd | Proto+zstd | **Pidgin** |
|
|
126
|
+
|---|---|---|---|---|
|
|
127
|
+
| Users x1000 | 7.78 | 3.67 | 5.76 | **6.41** |
|
|
128
|
+
| Orders x500 | 10.52 | 6.41 | 6.97 | **7.96** |
|
|
129
|
+
| Events x5000 | 30.49 | 14.85 | 24.92 | **25.62** |
|
|
130
|
+
|
|
131
|
+
Speed competitive with brotli, faster at scale.
|
|
132
|
+
|
|
133
|
+
### Real Public APIs (verified, all roundtrips lossless)
|
|
134
|
+
|
|
135
|
+
| API | Fields | brotli | Pidgin | Winner |
|
|
136
|
+
|---|---|---|---|---|
|
|
137
|
+
| GitHub Repos | 46 | 6.7% | **6.0%** | Pidgin |
|
|
138
|
+
| GitHub Search | 82 | 8.8% | **8.4%** | Pidgin |
|
|
139
|
+
| JSONPlaceholder Posts | 4 | 27.5% | 27.5% | Tie |
|
|
140
|
+
| JSONPlaceholder Comments | 5 | **27.5%** | 28.4% | brotli |
|
|
141
|
+
| JSONPlaceholder Todos | 4 | 20.5% | **20.2%** | Pidgin |
|
|
142
|
+
| JSONPlaceholder Photos | 5 | **13.5%** | 14.3% | brotli |
|
|
143
|
+
|
|
144
|
+
Pidgin wins on field-heavy APIs (many keys to eliminate). brotli wins on text/URL-heavy data with few fields. Both produce lossless roundtrips.
|
|
145
|
+
|
|
146
|
+
## Schema Evolution
|
|
147
|
+
|
|
148
|
+
API structure changes are handled automatically:
|
|
149
|
+
|
|
150
|
+
| Change | What happens |
|
|
151
|
+
|--------|-------------|
|
|
152
|
+
| **New field added** | Falls to JSON fallback, then auto-evolve incorporates it |
|
|
153
|
+
| **Field removed** | ABSENT marker (1 byte), old clients unaffected |
|
|
154
|
+
| **Field returns** | Already in schema as nullable, encodes typed immediately |
|
|
155
|
+
| **New enum value** | Appended to enum list (old indices preserved) |
|
|
156
|
+
| **Type widened** (int to float) | Auto-widened safely |
|
|
157
|
+
| **Schema drift detected** | Auto-evolve every 500 requests, profile version bumped |
|
|
158
|
+
|
|
159
|
+
```python
|
|
160
|
+
# Manual evolution
|
|
161
|
+
v2_profile = codec.profile.evolve(new_samples)
|
|
162
|
+
v2_codec = SchemaCodec(v2_profile)
|
|
163
|
+
|
|
164
|
+
# See what changed
|
|
165
|
+
for line in codec.profile.diff(v2_profile):
|
|
166
|
+
print(line)
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
Old clients with v1 profiles can still decode v2 data (unknown fields in JSON fallback).
|
|
170
|
+
New clients with v2 profiles can decode v1 data (missing fields as absent).
|
|
171
|
+
|
|
172
|
+
## Why Not Just Use X?
|
|
173
|
+
|
|
174
|
+
| Approach | Limitation Pidgin solves |
|
|
175
|
+
|---|---|
|
|
176
|
+
| **gzip/brotli** | Treats data as opaque bytes. Cannot exploit key repetition or type knowledge. |
|
|
177
|
+
| **Protobuf** | Requires `.proto` files, codegen, schema management. Pidgin learns at runtime. |
|
|
178
|
+
| **Protobuf+zstd** | Closes compression gap but adds toolchain complexity. Pidgin beats it with zero setup. |
|
|
179
|
+
| **msgpack/CBOR** | Binary JSON that still encodes every key. Pidgin eliminates keys entirely. |
|
|
180
|
+
| **Avro** | Needs schema registry infrastructure. Pidgin schemas are self-contained. |
|
|
181
|
+
|
|
182
|
+
## Architecture
|
|
183
|
+
|
|
184
|
+
```
|
|
185
|
+
Client Web Server Backend
|
|
186
|
+
| | |
|
|
187
|
+
| Accept-Encoding: pidgin | |
|
|
188
|
+
| -------------------------> | proxy_pass |
|
|
189
|
+
| | -------------------------> |
|
|
190
|
+
| | <-------- JSON response -- |
|
|
191
|
+
| | |
|
|
192
|
+
| | Pidgin engine: |
|
|
193
|
+
| | if learning: observe |
|
|
194
|
+
| | if ready: compress |
|
|
195
|
+
| | if drift: evolve |
|
|
196
|
+
| | |
|
|
197
|
+
| <-- Content-Encoding: pidgin |
|
|
198
|
+
| (binary, 10-19% of JSON) |
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
Backend returns normal JSON. Pidgin compresses transparently at the server level.
|
|
202
|
+
|
|
203
|
+
## Server Modules
|
|
204
|
+
|
|
205
|
+
| Server | Integration | Config |
|
|
206
|
+
|--------|------------|--------|
|
|
207
|
+
| **Kong** | Lua FFI plugin | `pidgin = true` |
|
|
208
|
+
| **nginx** | Dynamic C module | `pidgin on;` |
|
|
209
|
+
| **Apache** | Output filter | `PidginEnable On` |
|
|
210
|
+
| **Caddy** | Go middleware (cgo) | `pidgin` |
|
|
211
|
+
| **Traefik** | Pure Go plugin | middleware config |
|
|
212
|
+
| **HAProxy** | SPOE agent | filter config |
|
|
213
|
+
| **FastAPI** | Python middleware | `add_middleware(PidginMiddleware)` |
|
|
214
|
+
| **Django** | Python middleware | `MIDDLEWARE = [...]` |
|
|
215
|
+
|
|
216
|
+
## API Reference
|
|
217
|
+
|
|
218
|
+
### SchemaCodec
|
|
219
|
+
|
|
220
|
+
```python
|
|
221
|
+
from pidgin import SchemaCodec
|
|
222
|
+
|
|
223
|
+
codec = SchemaCodec.learn(samples) # learn schema
|
|
224
|
+
compressed = codec.compress(data) # dict or list[dict] -> bytes
|
|
225
|
+
original = codec.decompress(compressed) # bytes -> dict or list[dict]
|
|
226
|
+
profile_json = codec.profile.to_json() # share with clients
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
### Schema Evolution
|
|
230
|
+
|
|
231
|
+
```python
|
|
232
|
+
v2 = codec.profile.evolve(new_samples) # backward + forward compatible
|
|
233
|
+
diff = codec.profile.diff(v2) # human-readable changes
|
|
234
|
+
codec_v2 = SchemaCodec(v2) # use evolved profile
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
### RatchetCipher (optional)
|
|
238
|
+
|
|
239
|
+
```python
|
|
240
|
+
from pidgin import RatchetCipher # pip install pidgin[crypto]
|
|
241
|
+
|
|
242
|
+
cipher = RatchetCipher(shared_secret=b"key")
|
|
243
|
+
encrypted = cipher.encrypt(compressed) # forward secrecy
|
|
244
|
+
original = cipher.decrypt(encrypted) # ratchet stays in sync
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
### SecureChannel (optional)
|
|
248
|
+
|
|
249
|
+
```python
|
|
250
|
+
from pidgin import SecureChannel # pip install pidgin[crypto]
|
|
251
|
+
|
|
252
|
+
alice = SecureChannel.create("alice")
|
|
253
|
+
bob = SecureChannel.create("bob")
|
|
254
|
+
alice.handshake(bob.public_key) # X25519 key exchange
|
|
255
|
+
bob.handshake(alice.public_key)
|
|
256
|
+
envelope = alice.send("Hello") # compress + encrypt + HMAC
|
|
257
|
+
msg = bob.receive(envelope) # verify + decrypt + decompress
|
|
258
|
+
```
|
|
259
|
+
|
|
260
|
+
## C Library
|
|
261
|
+
|
|
262
|
+
For non-Python environments, `libpidgin` provides a standalone C API:
|
|
263
|
+
|
|
264
|
+
```c
|
|
265
|
+
#include <pidgin.h>
|
|
266
|
+
|
|
267
|
+
pidgin_engine_t *engine = pidgin_engine_create(30, 500);
|
|
268
|
+
pidgin_engine_observe(engine, "/api/users", json_str);
|
|
269
|
+
|
|
270
|
+
if (pidgin_engine_ready(engine, "/api/users")) {
|
|
271
|
+
pidgin_buf_t compressed = pidgin_engine_compress(engine, "/api/users", json_str);
|
|
272
|
+
// send compressed.data (compressed.len bytes) to client
|
|
273
|
+
pidgin_buf_free(&compressed);
|
|
274
|
+
}
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
## Project Structure
|
|
278
|
+
|
|
279
|
+
```
|
|
280
|
+
pidgin/
|
|
281
|
+
libpidgin/ C library (foundation for all server modules)
|
|
282
|
+
modules/ nginx, Apache, Kong, Caddy, Traefik, HAProxy
|
|
283
|
+
src/pidgin/ Python package with C extension
|
|
284
|
+
clients/js/ TypeScript decoder (@pidgin/codec)
|
|
285
|
+
benchmarks/ Protobuf comparison suite
|
|
286
|
+
docker/ Docker Compose test stack
|
|
287
|
+
docs/ Landing page
|
|
288
|
+
paper/ Academic paper
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
## License
|
|
292
|
+
|
|
293
|
+
MIT -- see [LICENSE](LICENSE).
|
|
294
|
+
|
|
295
|
+
---
|
|
296
|
+
|
|
297
|
+
Built by Evo Tech Labs.
|
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
# Pidgin
|
|
2
|
+
|
|
3
|
+
[](LICENSE)
|
|
4
|
+
[](https://python.org)
|
|
5
|
+
|
|
6
|
+
**Runtime-learned compression that beats Protobuf.** No `.proto` files, no codegen, zero config. One line to enable in your web server.
|
|
7
|
+
|
|
8
|
+
---
|
|
9
|
+
|
|
10
|
+
## How It Works
|
|
11
|
+
|
|
12
|
+
1. Pidgin observes your JSON API responses and learns the schema automatically
|
|
13
|
+
2. It strips all keys (both sides know the schema) and encodes values with type-optimal binary
|
|
14
|
+
3. Brotli compression on top for maximum density
|
|
15
|
+
4. Clients negotiate via `Accept-Encoding: pidgin`
|
|
16
|
+
|
|
17
|
+
**Result:** 10-19% of original JSON size. Smaller than gzip, brotli, and Protobuf+zstd.
|
|
18
|
+
|
|
19
|
+
## Quick Start
|
|
20
|
+
|
|
21
|
+
### Web Server (one line)
|
|
22
|
+
|
|
23
|
+
```nginx
|
|
24
|
+
# nginx
|
|
25
|
+
pidgin on;
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
```lua
|
|
29
|
+
-- Kong plugin
|
|
30
|
+
plugins = { { name = "pidgin" } }
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
```python
|
|
34
|
+
# FastAPI
|
|
35
|
+
from pidgin.contrib.fastapi import PidginMiddleware
|
|
36
|
+
app.add_middleware(PidginMiddleware)
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
```python
|
|
40
|
+
# Django
|
|
41
|
+
MIDDLEWARE = ["pidgin.contrib.django.PidginMiddleware"]
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Zero changes to your backend code. Pidgin auto-learns, auto-compresses, auto-evolves.
|
|
45
|
+
|
|
46
|
+
### Python Library
|
|
47
|
+
|
|
48
|
+
```bash
|
|
49
|
+
pip install pidgin
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
```python
|
|
53
|
+
from pidgin import SchemaCodec
|
|
54
|
+
|
|
55
|
+
codec = SchemaCodec.learn(sample_records) # learn from 20-50 samples
|
|
56
|
+
compressed = codec.compress(records) # 10-19% of JSON size
|
|
57
|
+
original = codec.decompress(compressed) # lossless roundtrip
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### JavaScript Client
|
|
61
|
+
|
|
62
|
+
```bash
|
|
63
|
+
npm install @pidgin/codec
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
```typescript
|
|
67
|
+
import { PidginClient } from '@pidgin/codec';
|
|
68
|
+
|
|
69
|
+
const client = new PidginClient({
|
|
70
|
+
baseUrl: 'https://api.example.com',
|
|
71
|
+
profiles: { users: profileJson },
|
|
72
|
+
});
|
|
73
|
+
const users = await client.get('/api/users', 'users');
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
## Benchmarks
|
|
77
|
+
|
|
78
|
+
All benchmarks: same machine, same data, single-threaded. Lower is better.
|
|
79
|
+
|
|
80
|
+
### Compression Ratio (% of original JSON)
|
|
81
|
+
|
|
82
|
+
| Dataset | gzip | brotli | Protobuf+zstd | **Pidgin** |
|
|
83
|
+
|---|---|---|---|---|
|
|
84
|
+
| Users x1000 | 21.4% | 19.5% | 20.3% | **18.9%** |
|
|
85
|
+
| Orders x500 (nested) | 19.1% | 17.5% | 17.4% | **16.1%** |
|
|
86
|
+
| Events x5000 | 13.6% | 12.3% | 12.3% | **10.8%** |
|
|
87
|
+
|
|
88
|
+
### Speed (ms, C extension)
|
|
89
|
+
|
|
90
|
+
| Dataset | brotli | zstd | Proto+zstd | **Pidgin** |
|
|
91
|
+
|---|---|---|---|---|
|
|
92
|
+
| Users x1000 | 7.78 | 3.67 | 5.76 | **6.41** |
|
|
93
|
+
| Orders x500 | 10.52 | 6.41 | 6.97 | **7.96** |
|
|
94
|
+
| Events x5000 | 30.49 | 14.85 | 24.92 | **25.62** |
|
|
95
|
+
|
|
96
|
+
Speed competitive with brotli, faster at scale.
|
|
97
|
+
|
|
98
|
+
### Real Public APIs (verified, all roundtrips lossless)
|
|
99
|
+
|
|
100
|
+
| API | Fields | brotli | Pidgin | Winner |
|
|
101
|
+
|---|---|---|---|---|
|
|
102
|
+
| GitHub Repos | 46 | 6.7% | **6.0%** | Pidgin |
|
|
103
|
+
| GitHub Search | 82 | 8.8% | **8.4%** | Pidgin |
|
|
104
|
+
| JSONPlaceholder Posts | 4 | 27.5% | 27.5% | Tie |
|
|
105
|
+
| JSONPlaceholder Comments | 5 | **27.5%** | 28.4% | brotli |
|
|
106
|
+
| JSONPlaceholder Todos | 4 | 20.5% | **20.2%** | Pidgin |
|
|
107
|
+
| JSONPlaceholder Photos | 5 | **13.5%** | 14.3% | brotli |
|
|
108
|
+
|
|
109
|
+
Pidgin wins on field-heavy APIs (many keys to eliminate). brotli wins on text/URL-heavy data with few fields. Both produce lossless roundtrips.
|
|
110
|
+
|
|
111
|
+
## Schema Evolution
|
|
112
|
+
|
|
113
|
+
API structure changes are handled automatically:
|
|
114
|
+
|
|
115
|
+
| Change | What happens |
|
|
116
|
+
|--------|-------------|
|
|
117
|
+
| **New field added** | Falls to JSON fallback, then auto-evolve incorporates it |
|
|
118
|
+
| **Field removed** | ABSENT marker (1 byte), old clients unaffected |
|
|
119
|
+
| **Field returns** | Already in schema as nullable, encodes typed immediately |
|
|
120
|
+
| **New enum value** | Appended to enum list (old indices preserved) |
|
|
121
|
+
| **Type widened** (int to float) | Auto-widened safely |
|
|
122
|
+
| **Schema drift detected** | Auto-evolve every 500 requests, profile version bumped |
|
|
123
|
+
|
|
124
|
+
```python
|
|
125
|
+
# Manual evolution
|
|
126
|
+
v2_profile = codec.profile.evolve(new_samples)
|
|
127
|
+
v2_codec = SchemaCodec(v2_profile)
|
|
128
|
+
|
|
129
|
+
# See what changed
|
|
130
|
+
for line in codec.profile.diff(v2_profile):
|
|
131
|
+
print(line)
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
Old clients with v1 profiles can still decode v2 data (unknown fields in JSON fallback).
|
|
135
|
+
New clients with v2 profiles can decode v1 data (missing fields as absent).
|
|
136
|
+
|
|
137
|
+
## Why Not Just Use X?
|
|
138
|
+
|
|
139
|
+
| Approach | Limitation Pidgin solves |
|
|
140
|
+
|---|---|
|
|
141
|
+
| **gzip/brotli** | Treats data as opaque bytes. Cannot exploit key repetition or type knowledge. |
|
|
142
|
+
| **Protobuf** | Requires `.proto` files, codegen, schema management. Pidgin learns at runtime. |
|
|
143
|
+
| **Protobuf+zstd** | Closes compression gap but adds toolchain complexity. Pidgin beats it with zero setup. |
|
|
144
|
+
| **msgpack/CBOR** | Binary JSON that still encodes every key. Pidgin eliminates keys entirely. |
|
|
145
|
+
| **Avro** | Needs schema registry infrastructure. Pidgin schemas are self-contained. |
|
|
146
|
+
|
|
147
|
+
## Architecture
|
|
148
|
+
|
|
149
|
+
```
|
|
150
|
+
Client Web Server Backend
|
|
151
|
+
| | |
|
|
152
|
+
| Accept-Encoding: pidgin | |
|
|
153
|
+
| -------------------------> | proxy_pass |
|
|
154
|
+
| | -------------------------> |
|
|
155
|
+
| | <-------- JSON response -- |
|
|
156
|
+
| | |
|
|
157
|
+
| | Pidgin engine: |
|
|
158
|
+
| | if learning: observe |
|
|
159
|
+
| | if ready: compress |
|
|
160
|
+
| | if drift: evolve |
|
|
161
|
+
| | |
|
|
162
|
+
| <-- Content-Encoding: pidgin |
|
|
163
|
+
| (binary, 10-19% of JSON) |
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
Backend returns normal JSON. Pidgin compresses transparently at the server level.
|
|
167
|
+
|
|
168
|
+
## Server Modules
|
|
169
|
+
|
|
170
|
+
| Server | Integration | Config |
|
|
171
|
+
|--------|------------|--------|
|
|
172
|
+
| **Kong** | Lua FFI plugin | `pidgin = true` |
|
|
173
|
+
| **nginx** | Dynamic C module | `pidgin on;` |
|
|
174
|
+
| **Apache** | Output filter | `PidginEnable On` |
|
|
175
|
+
| **Caddy** | Go middleware (cgo) | `pidgin` |
|
|
176
|
+
| **Traefik** | Pure Go plugin | middleware config |
|
|
177
|
+
| **HAProxy** | SPOE agent | filter config |
|
|
178
|
+
| **FastAPI** | Python middleware | `add_middleware(PidginMiddleware)` |
|
|
179
|
+
| **Django** | Python middleware | `MIDDLEWARE = [...]` |
|
|
180
|
+
|
|
181
|
+
## API Reference
|
|
182
|
+
|
|
183
|
+
### SchemaCodec
|
|
184
|
+
|
|
185
|
+
```python
|
|
186
|
+
from pidgin import SchemaCodec
|
|
187
|
+
|
|
188
|
+
codec = SchemaCodec.learn(samples) # learn schema
|
|
189
|
+
compressed = codec.compress(data) # dict or list[dict] -> bytes
|
|
190
|
+
original = codec.decompress(compressed) # bytes -> dict or list[dict]
|
|
191
|
+
profile_json = codec.profile.to_json() # share with clients
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
### Schema Evolution
|
|
195
|
+
|
|
196
|
+
```python
|
|
197
|
+
v2 = codec.profile.evolve(new_samples) # backward + forward compatible
|
|
198
|
+
diff = codec.profile.diff(v2) # human-readable changes
|
|
199
|
+
codec_v2 = SchemaCodec(v2) # use evolved profile
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
### RatchetCipher (optional)
|
|
203
|
+
|
|
204
|
+
```python
|
|
205
|
+
from pidgin import RatchetCipher # pip install pidgin[crypto]
|
|
206
|
+
|
|
207
|
+
cipher = RatchetCipher(shared_secret=b"key")
|
|
208
|
+
encrypted = cipher.encrypt(compressed) # forward secrecy
|
|
209
|
+
original = cipher.decrypt(encrypted) # ratchet stays in sync
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
### SecureChannel (optional)
|
|
213
|
+
|
|
214
|
+
```python
|
|
215
|
+
from pidgin import SecureChannel # pip install pidgin[crypto]
|
|
216
|
+
|
|
217
|
+
alice = SecureChannel.create("alice")
|
|
218
|
+
bob = SecureChannel.create("bob")
|
|
219
|
+
alice.handshake(bob.public_key) # X25519 key exchange
|
|
220
|
+
bob.handshake(alice.public_key)
|
|
221
|
+
envelope = alice.send("Hello") # compress + encrypt + HMAC
|
|
222
|
+
msg = bob.receive(envelope) # verify + decrypt + decompress
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
## C Library
|
|
226
|
+
|
|
227
|
+
For non-Python environments, `libpidgin` provides a standalone C API:
|
|
228
|
+
|
|
229
|
+
```c
|
|
230
|
+
#include <pidgin.h>
|
|
231
|
+
|
|
232
|
+
pidgin_engine_t *engine = pidgin_engine_create(30, 500);
|
|
233
|
+
pidgin_engine_observe(engine, "/api/users", json_str);
|
|
234
|
+
|
|
235
|
+
if (pidgin_engine_ready(engine, "/api/users")) {
|
|
236
|
+
pidgin_buf_t compressed = pidgin_engine_compress(engine, "/api/users", json_str);
|
|
237
|
+
// send compressed.data (compressed.len bytes) to client
|
|
238
|
+
pidgin_buf_free(&compressed);
|
|
239
|
+
}
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
## Project Structure
|
|
243
|
+
|
|
244
|
+
```
|
|
245
|
+
pidgin/
|
|
246
|
+
libpidgin/ C library (foundation for all server modules)
|
|
247
|
+
modules/ nginx, Apache, Kong, Caddy, Traefik, HAProxy
|
|
248
|
+
src/pidgin/ Python package with C extension
|
|
249
|
+
clients/js/ TypeScript decoder (@pidgin/codec)
|
|
250
|
+
benchmarks/ Protobuf comparison suite
|
|
251
|
+
docker/ Docker Compose test stack
|
|
252
|
+
docs/ Landing page
|
|
253
|
+
paper/ Academic paper
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
## License
|
|
257
|
+
|
|
258
|
+
MIT -- see [LICENSE](LICENSE).
|
|
259
|
+
|
|
260
|
+
---
|
|
261
|
+
|
|
262
|
+
Built by Evo Tech Labs.
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61.0"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "pidgin-codec"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Runtime-learned compression that beats Protobuf — no .proto files, no codegen, zero config"
|
|
9
|
+
requires-python = ">=3.10"
|
|
10
|
+
license = "MIT"
|
|
11
|
+
readme = "README.md"
|
|
12
|
+
authors = [
|
|
13
|
+
{name = "Ivo Gergov"},
|
|
14
|
+
]
|
|
15
|
+
classifiers = [
|
|
16
|
+
"Development Status :: 4 - Beta",
|
|
17
|
+
"Intended Audience :: Developers",
|
|
18
|
+
|
|
19
|
+
"Programming Language :: Python :: 3",
|
|
20
|
+
"Programming Language :: Python :: 3.10",
|
|
21
|
+
"Programming Language :: Python :: 3.11",
|
|
22
|
+
"Programming Language :: Python :: 3.12",
|
|
23
|
+
"Programming Language :: Python :: 3.13",
|
|
24
|
+
"Programming Language :: Python :: 3.14",
|
|
25
|
+
"Topic :: Internet :: WWW/HTTP",
|
|
26
|
+
"Topic :: Software Development :: Libraries",
|
|
27
|
+
"Typing :: Typed",
|
|
28
|
+
]
|
|
29
|
+
keywords = ["compression", "json", "api", "protobuf", "binary", "schema", "codec"]
|
|
30
|
+
dependencies = [
|
|
31
|
+
"brotli>=1.1.0",
|
|
32
|
+
]
|
|
33
|
+
|
|
34
|
+
[project.optional-dependencies]
|
|
35
|
+
crypto = ["cryptography>=42.0"]
|
|
36
|
+
benchmark = [
|
|
37
|
+
"zstandard>=0.23.0",
|
|
38
|
+
"msgpack>=1.0",
|
|
39
|
+
"orjson>=3.9",
|
|
40
|
+
"protobuf>=5.0",
|
|
41
|
+
"grpcio-tools>=1.60",
|
|
42
|
+
]
|
|
43
|
+
dev = [
|
|
44
|
+
"pytest>=8.0",
|
|
45
|
+
"pytest-asyncio>=0.24",
|
|
46
|
+
]
|
|
47
|
+
|
|
48
|
+
[tool.setuptools.packages.find]
|
|
49
|
+
where = ["src"]
|
|
50
|
+
|
|
51
|
+
[tool.pytest.ini_options]
|
|
52
|
+
asyncio_mode = "auto"
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"""Pidgin — Dual-layer compression for AI communication.
|
|
2
|
+
|
|
3
|
+
Semantic compression (AI-to-AI):
|
|
4
|
+
from pidgin import Protocol
|
|
5
|
+
prompt = Protocol().system_prompt("bidirectional")
|
|
6
|
+
|
|
7
|
+
Schema-aware API compression:
|
|
8
|
+
from pidgin import SchemaCodec
|
|
9
|
+
codec = SchemaCodec.learn(sample_responses)
|
|
10
|
+
compressed = codec.compress(response)
|
|
11
|
+
original = codec.decompress(compressed)
|
|
12
|
+
|
|
13
|
+
Ratchet cipher (forward secrecy):
|
|
14
|
+
from pidgin import RatchetCipher
|
|
15
|
+
cipher = RatchetCipher(shared_secret=b"key")
|
|
16
|
+
encrypted = cipher.encrypt(data)
|
|
17
|
+
|
|
18
|
+
Secure channel (compress + encrypt + authenticate):
|
|
19
|
+
from pidgin import SecureChannel
|
|
20
|
+
channel = SecureChannel.create("alice")
|
|
21
|
+
|
|
22
|
+
LLM-powered encode/decode:
|
|
23
|
+
from pidgin import Codec
|
|
24
|
+
codec = Codec()
|
|
25
|
+
result = await codec.encode("message")
|
|
26
|
+
"""
|
|
27
|
+
|
|
28
|
+
from pidgin._protocol import Protocol
|
|
29
|
+
from pidgin._types import CompressedMessage, DecodedMessage
|
|
30
|
+
from pidgin.ratchet import RatchetCipher
|
|
31
|
+
from pidgin.schema_codec import SchemaCodec
|
|
32
|
+
|
|
33
|
+
__version__ = "0.1.0"
|
|
34
|
+
__all__ = [
|
|
35
|
+
"Protocol", "Codec", "SchemaCodec", "RatchetCipher",
|
|
36
|
+
"SecureChannel", "CompressedMessage", "DecodedMessage",
|
|
37
|
+
]
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def __getattr__(name: str):
|
|
41
|
+
if name == "Codec":
|
|
42
|
+
from pidgin._codec import Codec
|
|
43
|
+
return Codec
|
|
44
|
+
if name == "SecureChannel":
|
|
45
|
+
from pidgin.channel import SecureChannel
|
|
46
|
+
return SecureChannel
|
|
47
|
+
raise AttributeError(f"module 'pidgin' has no attribute {name!r}")
|