Skip to content

Fix optimization-dependent invalid Map decoding - #1190

Closed
PaleRoses wants to merge 1 commit into
haskell:masterfrom
PaleRoses:codex/fix-fromjsonkey-fmap-rule
Closed

PaleRoses wants to merge 1 commit into
haskell:masterfrom
PaleRoses:codex/fix-fromjsonkey-fmap-rule

Conversation

@PaleRoses

@PaleRoses PaleRoses commented Aug 28, 2026

Copy link
Copy Markdown
Contributor

Fixes #1169.

Remove the unsound "FromJSONKeyCoerce: fmap coerce" RULE. mapKeyO reuses the source tree only when mapped keys stay strictly ordered; otherwise it rebuilds the map.

Tests cover reversed ordering and collisions. The full suite passes at -O0 and -O2. All 32 Map benchmarks pass the 5% regression check; affected cases are 29–41% faster.

if maybe True (currentKey <) nextKey
then Just (Just currentKey)
else Nothing
{-# INLINE mapKeyO #-}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I don't think this is the right place for this kind of optimization.

  • It's orthogonal to fixing the main issue at hand.
  • When the keys are not ordered you end up paying the cost of applying fk twice to each key, which might be expensive if it's doing nontrivial parsing. I'm also not sure this optimization is worthwhile since mapKeysWith already takes care of running in linear time if the mapping is monotonic.

Comment thread tests/UnitTests/FromJSONKey.hs
assertDecodedCollidingMap :: Assertion
assertDecodedCollidingMap = decodedMap @?= Just (Map.singleton (FoldedText "a") "upper")
where
decodedMap = decode "{\"A\":\"upper\",\"a\":\"lower\"}" :: Maybe (Map FoldedText Text)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This test relies on KeyMap being ordered. That's why it sometimes fails the flag ordered-keymap disabled.

@Lysxia

Lysxia commented Sep 12, 2026

Copy link
Copy Markdown
Collaborator

Subsumed by #1193

@Lysxia Lysxia closed this Sep 12, 2026
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.

unsafe rewrite rule for Functor instance of FromJSONKeyFunction

2 participants