IntersectMBO / IntersectMBO/evolution-sdk
[Tangent] Eliminate duplicate WithLength decoder family in CBOR.ts
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- TypeScript
- Estrellas
- 22
- Forks
- 30
- Merge medio
- 13 h
- PR fusionados (30 d)
- 14
Descripción
What was discovered
CBOR.ts has two complete decoder implementations side-by-side: an offset-based family (`decodeItemAt`, `decodeBytesAt`, `decodeArrayAt`, …) and a slice-based family (`decodeItemWithLengthSync`, `decodeBytesWithLengthSync`, `decodeArrayWithLengthSync`, …). The slice-based family calls `data.slice(offset)` at every recursion, allocating a new `Uint8Array` for each nested item.
Surfaced during
CBOR module review while preparing the `bounded_bytes` / `BoundedBytes` fix (PR for Conway CDDL compliance). Found during full read of CBOR.ts decoder section (~lines 1600–1900).
Why it's worth exploring
For deeply nested PlutusData (constr with 5 levels, many fields), the slice-based path allocates hundreds of `Uint8Array` copies. The offset-based family is strictly better — it threads an integer offset through the call chain with zero allocation overhead. The two families are functionally equivalent. Deleting the `WithLength` family and routing its callers to the offset-based one removes ~200 lines and eliminates the O(n²) allocation pattern.
What would be needed
- Identify all callers of the `WithLength` family inside CBOR.ts
- Route them to the offset-based equivalents (`decodeItemAt` etc.)
- Delete the `WithLength` family
- Run the full CBOR test suite (968 tests) to confirm no regression
- Benchmark with a deeply nested PlutusData value to quantify the win (optional but useful)
Context snapshot
Relevant section: `packages/evolution/src/CBOR.ts` ~lines 1600–1900.
The slice-based family:
```typescript
const decodeItemWithLengthSync = (data: Uint8Array, options: CodecOptions): { item: CBOR; bytesConsumed: number } => {
// ... dispatches to decodeBytesWithLengthSync, decodeArrayWithLengthSync, etc.
// Each calls data.slice(offset) before recursing — one allocation per node
}
```
The offset-based family (keeper):
```typescript
const decodeItemAt = (data: Uint8Array, offset: number, options: CodecOptions): DecodeAtResult => {
// ... threads offset integer — zero allocation
}
```
Both produce identical decoded values. The `WithLength` family exists as a legacy parallel path and has no unique capabilities the offset family lacks.
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
Comienza en packages/evolution/src/CBOR.ts alrededor de las líneas 1600–1900 e identifica todos los llamadores de decodeItemWithLengthSync y de las funciones WithLength relacionadas. Compara esos llamadores con la familia decodeItemAt basada en offsets y, después, ejecuta la suite completa de pruebas de CBOR, con 968 pruebas. La tarea estará terminada cuando se haya eliminado la familia WithLength heredada, los llamadores usen la ruta basada en offsets y la suite no muestre ninguna regresión.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- typescript
- Área
- backend
- Tipo de issue
- Refactorización
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Estado de actividad
- Tranquilo
- Claridad
- Bastante claro
- Aptitud para principiantes
- 45/100