Skip to content

Latest commit

 

History

History
84 lines (65 loc) · 5.08 KB

Accuracy.md

File metadata and controls

84 lines (65 loc) · 5.08 KB

Accuracy

SwiftStepdown decides who calls whom from names alone, without building the code, so it can be wrong where only the compiler's types tell. This guide measures how often, against the compiler's own index, on seven Swift packages and about 100,000 lines of code.

The short answer

Name matching agrees with the compiler on 98.2% of uses and is never simply wrong; nearly every disagreement is a link it misses rather than one it invents. Across all seven packages that changed the order of one declaration in 2,274, under the default style. Every reordered file in every package still builds, tests included.

How it was measured

The packages range from a few hundred lines to 50,000, libraries and apps by several authors. Each was built in debug with the compiler's index, which records what every name in the source refers to. For each file the index holds, stepdown-measure (Advanced) reads the bodies the way the formatter does, then compares two things:

  • Uses. Every use of a name, inside a declaration, that either side links to a helper in the same body: a function, computed property, or subscript beside it. These are the only links that can move anything; a call into another type or file cannot.
  • Orders. Each body ordered twice, once by the links name matching finds and once by the compiler's, and the two orders compared.
Package size Uses Exact Precision Recall Bodies reordered Same order
50,000 lines 1,059 99.0% 99.3% 99.9% 55 55
24,000 lines 757 95.9% 98.0% 97.9% 53 52
16,000 lines 616 98.5% 99.1% 99.8% 16 16
3,800 lines 44 100% 100% 100% 4 4
3,700 lines 171 100% 100% 100% 7 7
2,100 lines 90 100% 100% 100% 4 4
700 lines 22 100% 100% 100% 2 2
All, 100,000 lines 2,759 98.2% 99.0% 99.3% 141 140

Exact is a use matched to the compiler's declaration and nothing else. Precision and recall are over links, one per pair of declarations: of the links name matching finds, how many the compiler has, and of the compiler's, how many name matching finds. Bodies reordered uses the default style, where only private helpers move.

Where they disagree

51 of 2,759 uses are not exact. Most of them are not mistakes:

Uses What happened A mistake?
16 Overloads only types tell apart, as write(from:) for two buffer types: both are candidates, the right one among them. No; both are placed below the caller
20 Written on another value of the same type, as lhs.shape == rhs.shape or copy.repeating(1). Only self and bare names count, since delegate.update() usually means another type. Yes, a missed link
8 Code the index never saw: 4 in an #if branch the debug build left out, 4 in property observers @Observable moves into generated code. The calls are real. No; the index is the one missing them
5 The index's own rename support: after if let selection, it records uses of the new local as uses of the property, so renaming one renames both. The property's use in the if let itself is matched. No
1 A leading-dot call, .make(), which is left unmatched on purpose: an earlier run matched them, and was right once and wrong four times. Yes, a missed link
1 An overload in another file that the compiler prefers to the one beside the caller. Yes, an extra link

That leaves 22 real mistakes in 2,759 uses, 0.8%, and 21 of them are links missed, not invented.

What it does to the order

The order is more robust than the links, because most links are redundant for placing a helper: a missed link usually points at a declaration that does not move, or at a helper another use already pulls into the same place.

Style Bodies reordered Same order Declarations placed differently
Private helpers move (the default) 141 140 (99.3%) 1 of 2,274
Everything moves 273 263 (96.3%) 10 of 3,582 (0.3%)

The one difference under the default style is a pair of overloads placed together where the compiler links only one. With everything moving, the differences are the same overloads plus the lhs.shape kind of miss: a declaration used only through another value stays where it was rather than moving below that use. In every case the result still reads top down; one helper is just a few declarations from where the compiler's links would put it.

Limits of the study

  • Seven packages. They differ in size, author, and style, and the largest, at 50,000 lines, scored 99.0%; still, a codebase with habits none of them share could turn up cases they did not.
  • Debug builds. The index sees only the #if branches a debug build compiles; name matching reads all of them.
  • Swift packages only. No Xcode app targets, Objective-C interop, or result builders heavier than SwiftUI's were in the set.

Advanced shows how to run the study on your own code.