Skip to content

putBatch computes PK/SK three times per item #46

@mckalexee

Description

@mckalexee

Location

`lib/put.ts:171-180`

Problem

```ts
for (const batchItem of batchToPut) {
const item = facet.in(batchItem); // builds + marshalls PK/SK
const key = facet.pk(batchItem) + facet.sk(batchItem); // recomputes PK/SK as strings
writeRequests[key] = { PutRequest: { Item: item } };
itemsByKey[key] = facet.out(item); // unmarshalls (back to original)
}
```

Per record:

  1. `facet.in()` runs `buildKey` for PK, SK, and every index PK/SK, then marshalls.
  2. `facet.pk()` runs `buildKey` for PK again.
  3. `facet.sk()` runs `buildKey` for SK again.
  4. `facet.out()` unmarshalls and strips synthetic keys.

Steps 2 and 3 are redundant — the marshalled item already contains `item.PK = { S: ... }` and `item.SK = { S: ... }`. The dedupe key can be read directly:

```ts
const key = (item.PK as { S: string }).S + (item.SK as { S: string }).S;
```

For shard-bearing keys, `buildKey` runs CRC32 per call, so this isn't free.

Fix

Reuse the PK/SK from the marshalled item. While there, consider also whether `facet.out(item)` on an item you just constructed is meaningful — it roundtrips through the validator to produce the "record that would come back", but if `in()` is lossless this is equivalent to the original record. Discussion welcome.

Test plan

Micro-benchmark a 10k-item put. Before vs after. Expect modest improvement (low-single-digit percent, mostly due to skipping CRC32 for sharded facets).

Priority

Low — measurable but not dramatic; cleanup value is bigger than the perf value.

Metadata

Metadata

Assignees

No one assigned

    Labels

    performancePerformance improvementrefactorCode organization / internal refactor

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions