Skip to content

Type-assert collected keys before sort! to avoid invalidations - #501

Open
Beforerr wants to merge 1 commit into
JuliaIO:masterfrom
Beforerr:fix-sort-keys-invalidation
Open

Beforerr wants to merge 1 commit into
JuliaIO:masterfrom
Beforerr:fix-sort-keys-invalidation

Conversation

@Beforerr

@Beforerr Beforerr commented Oct 7, 2026

Copy link
Copy Markdown

When json! is inferred with x::Any (e.g. JSON.json!(::Vector{UInt8}, ::Int64, ::Any, ::WriteOptions{JSONWriteStyle}, ...)), the sort-keys path calls sort!(collect(keys(x)); by=...) with an Any argument. Since only a handful of sort! kwmethods exist, inference enumerates them and records a backedge on kwcall(::NamedTuple, ::typeof(sort!), ::Any), so any package that adds a sort! kwmethod invalidates json!.

Repro (Julia 1.13, JSON main):

using OrderedCollections, SnoopCompileCore
invs = @snoop_invalidations using JSON
using SnoopCompile
invalidation_trees(invs)

gives 5 trees (4 sort! methods from OrderedCollections, 1 from LibGit2), each invalidating 2 json! instances with 23 children total.

Fix: assert collect(keys(x))::Vector, so the call is sort!(::Vector; by), which no OrderedCollections/LibGit2 method matches. With this change the repro above reports no invalidation trees. Tests pass.

@codecov

codecov Bot commented Oct 7, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.57%. Comparing base (4f3dfdc) to head (f4f6df4).

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #501   +/-   ##
=======================================
  Coverage   92.57%   92.57%           
=======================================
  Files           7        7           
  Lines        1927     1927           
=======================================
  Hits         1784     1784           
  Misses        143      143           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

This branch has not been deployed

No deployments
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.

1 participant