Skip to content

Fix optimization-dependent invalid Map decoding - #1190

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

Fix optimization-dependent invalid Map decoding#1190
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

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.

fromJSONKey = fmap MyText' fromJSONKey
fromJSONKeyList = error "not used"

newtype ReverseText = ReverseText 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.

Why not use Down Text?

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.

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