Skip to content

perf: drop per-element copies in Clarity value deserialization#7426

Merged
francesco-stacks merged 4 commits into
stacks-network:developfrom
francesco-stacks:perf/clarity-deser-streaming
Jul 22, 2026
Merged

perf: drop per-element copies in Clarity value deserialization#7426
francesco-stacks merged 4 commits into
stacks-network:developfrom
francesco-stacks:perf/clarity-deser-streaming

Conversation

@francesco-stacks

Copy link
Copy Markdown
Contributor

Description

This removes the main memory and CPU overheads in Clarity value deserialization. No behavior changes...same bytes accepted and rejected, same resulting values

Three changes:

  • construct_parent_list_type no longer collects a TypeSignature for every list element before computing the entry type. It folds while iterating, and skips the fold when the next type equals the accumulator (safe because least_supertype_v2_1(T, T) == T, pinned by a new test).
  • The deserializer used to pop and re-push the whole stack frame for every element it read. Frames are now updated in place and only popped when a container completes.
  • next_expected_type returned a deep clone of the expected type for every element. It now returns a reference.

On a 1M-element bool list (counting-allocator harness, vs current develop): untyped reads go from 141 MiB / 62 ms to 61 MiB / 28 ms. Typed reads are 16-21% faster with the same peak.

Applicable issues

  • fixes #

Additional info (benefits, drawbacks, caveats)

Checklist

  • Test coverage for new or modified code paths
  • For new Clarity features or consensus changes, add property tests (see docs/property-testing.md)
  • Changelog fragment(s) or "no changelog" label added (see changelog.d/README.md)
  • Required documentation changes (e.g., rpc/openapi.yaml for RPC endpoints, event-dispatcher.md for new events)
  • New clarity functions have corresponding PR in clarity-benchmarking repo

@francesco-stacks francesco-stacks changed the title perf: stream the list-type fold and drop per-element copies in Value deserialization perf: drop per-element copies in Clarity value deserialization Jul 21, 2026
@francesco-stacks francesco-stacks self-assigned this Jul 21, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Improves performance of Clarity Value deserialization by eliminating avoidable per-element allocations/copies while preserving accepted/rejected bytes and resulting values, and adds targeted tests to pin correctness of the new fast paths.

Changes:

  • Stream construct_parent_list_type()/parent_list_type() entry-type computation via a shared fold iterator (with an equality fast-path).
  • Update the deserializer to mutate stack frames in place (avoid pop/re-push per element) and make next_expected_type() return references instead of deep clones.
  • Add tests for least_supertype_v2_1 idempotence and for tuple truncation during unwind, plus a changelog entry.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.

Show a summary per file
File Description
clarity-types/src/types/signatures.rs Streams list entry-type folding via a shared iterator-based helper to avoid per-element type collection.
clarity-types/src/types/serialization.rs Removes per-element frame pop/re-push and avoids per-element deep clones of expected types during deserialization.
clarity-types/src/tests/types/signatures.rs Adds idempotence/property-style coverage ensuring the new equality fast-path is sound and that streamed/collected list type construction matches.
clarity-types/src/tests/types/serialization.rs Adds a regression test for EOF while reading the next tuple key during unwind.
changelog.d/clarity-value-deser-perf.changed Documents the deserialization performance improvement.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@brice-stacks brice-stacks left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great!

@francesco-stacks
francesco-stacks added this pull request to the merge queue Jul 22, 2026
Merged via the queue into stacks-network:develop with commit 91f4c11 Jul 22, 2026
219 of 222 checks passed
@francesco-stacks
francesco-stacks deleted the perf/clarity-deser-streaming branch July 22, 2026 12:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants