Avoid re-validating when decoding from Binary or Read - #12169
Open
andreabedini wants to merge 1 commit into
Open
Avoid re-validating when decoding from Binary or Read#12169andreabedini wants to merge 1 commit into
andreabedini wants to merge 1 commit into
Conversation
The Binary and Read instances rebuilt the graph with fromDistinctList, which re-checks key uniqueness and calls 'error' (stringifying the key with 'show') on a duplicate. That check is pointless on data we serialised ourselves from an already-valid graph, yet it forced a Show (Key a) constraint onto both instances purely to format that panic. Decode now reconstructs directly or via a non-validating helper, dropping the Show (Key a) constraint. While this is API changing, it is a backwards-compatible as it is a widening of the Binary/Read instances.
andreabedini
force-pushed
the
andrea/wip/prep/decode-no-revalidate
branch
from
July 28, 2026 05:16
7ec10cc to
178fdcf
Compare
There was a problem hiding this comment.
Pull request overview
This PR streamlines decoding of serialized graphs and install plans by removing redundant invariant checks during Binary/Read decoding, thereby relaxing instance constraints (notably dropping Show (Key a) requirements) while keeping encoding/decoding behavior equivalent for valid, self-produced serialized data.
Changes:
- Relax
Read (Graph a)/Binary (Graph a)constraints by decoding via a non-validating reconstruction helper. - Decode
GenericInstallPlandirectly instead of routing throughmkInstallPlan(avoids re-running plan validity checks on decode). - Add a changelog entry documenting the decode behavior and constraint widening.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| changelog.d/decode-no-revalidate.md | Records the change in decode behavior and instance constraint widening. |
| Cabal-syntax/src/Distribution/Compat/Graph.hs | Drops Show (Key a) from Read/Binary instances by decoding via a trusted reconstruction helper. |
| cabal-install/src/Distribution/Client/InstallPlan.hs | Adjusts Binary decoding to reconstruct GenericInstallPlan directly rather than validating via mkInstallPlan. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+395
to
+402
| -- | Like 'fromDistinctList', but for reconstructing a graph whose keys are | ||
| -- already known to be distinct — in particular a graph produced by 'toList' | ||
| -- and round-tripped through the 'Binary'/'Read' instances. It performs no | ||
| -- duplicate-key check, and so (unlike 'fromDistinctList') does not require | ||
| -- @Show (Key a)@. | ||
| fromDistinctListTrusted :: IsNode a => [a] -> Graph a | ||
| fromDistinctListTrusted = | ||
| fromMap . Map.fromList . map (\n -> n `seq` (nodeKey n, n)) |
Comment on lines
+317
to
+324
| -- Reconstruct the plan directly rather than routing through 'mkInstallPlan': | ||
| -- the bytes were serialised from a valid plan, so re-running the invariant | ||
| -- check on decode is redundant (and would 'error' rather than fail the | ||
| -- decode). This keeps 'get' the inverse of 'put'. | ||
| get = do | ||
| graph <- get | ||
| indepGoals <- get | ||
| return $! mkInstallPlan "(instance Binary)" graph indepGoals | ||
| pure $! GenericInstallPlan graph indepGoals |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The Binary and Read instances rebuilt the graph with fromDistinctList, which re-checks key uniqueness and calls 'error' (stringifying the key with 'show') on a duplicate. That check is pointless on data we serialised ourselves from an already-valid graph, yet it forced a Show (Key a) constraint onto both instances purely to format that panic. Decode now reconstructs directly or via a non-validating helper, dropping the Show (Key a) constraint.
Binary (GenericInstallPlan …)had the same shape one layer up: itsgetroutedthrough
mkInstallPlan, re-running the plan-validity check (whicherrors ratherthan failing the decode) on cached data. It now reconstructs the plan directly.
This is an interface change to
Cabal-syntax— theBinary/Read (Graph a)instances require fewer constraints — but a backwards-compatible widening: no
existing caller breaks.
changelog.d/decode-no-revalidate.md).Distribution.Compat.Graph(4 props) andInstallPlan(5 props) unit suites pass.