Skip to content

Commit dc4a31f

Browse files
Copilotstreamich
andcommitted
Document Ion codec instance reuse limitation and update README
Co-authored-by: streamich <[email protected]>
1 parent aa2e065 commit dc4a31f

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

src/ion/README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,44 @@
1+
# Amazon Ion Binary Codec
2+
3+
This library provides high-performance Amazon Ion binary format encoding and decoding capabilities.
4+
5+
## Usage
6+
7+
```typescript
8+
import {IonEncoderFast, IonDecoder} from '@jsonjoy.com/json-pack/lib/ion';
9+
10+
const encoder = new IonEncoderFast();
11+
const decoder = new IonDecoder();
12+
13+
const data = {users: [{name: 'Alice', age: 30}], count: 1};
14+
const encoded = encoder.encode(data);
15+
const decoded = decoder.decode(encoded);
16+
```
17+
18+
## Important Usage Notes
19+
20+
⚠️ **Instance Reuse Limitation**: Due to internal state management with shared UTF-8 decoders, encoder and decoder instances should **not be reused** across multiple encode/decode operations with complex data. For reliable operation, create fresh instances for each encoding/decoding operation:
21+
22+
```typescript
23+
// ❌ DON'T: Reuse instances for multiple operations
24+
const encoder = new IonEncoderFast();
25+
const decoder = new IonDecoder();
26+
for (const item of items) {
27+
const encoded = encoder.encode(item); // May cause state corruption
28+
const decoded = decoder.decode(encoded);
29+
}
30+
31+
// ✅ DO: Create fresh instances for each operation
32+
for (const item of items) {
33+
const encoder = new IonEncoderFast();
34+
const decoder = new IonDecoder();
35+
const encoded = encoder.encode(item);
36+
const decoded = decoder.decode(encoded);
37+
}
38+
```
39+
40+
This limitation primarily affects complex nested objects with many string keys. Simple data structures may work with reused instances, but fresh instances are recommended for guaranteed correctness.
41+
142
## Benchmarks
243

344
Encoding:

src/ion/__tests__/fuzzing.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {IonEncoderFast} from '../IonEncoderFast';
33
import {IonDecoder} from '../IonDecoder';
44

55
describe('fuzzing', () => {
6-
test('Amazon Ion codec', () => {
6+
test('Amazon Ion codec with fresh instances', () => {
77
for (let i = 0; i < 2000; i++) {
88
const value = JSON.parse(JSON.stringify(RandomJson.generate()));
99
// Create fresh instances for each iteration to avoid state corruption

0 commit comments

Comments
 (0)