HEQL global prelude
Generated from commit e8a491d6e1e2.
The HEQL global prelude: shared let helpers prepended to every
query — the default catalog, a --source/--file, and the web
ad-hoc box alike — so they are in scope everywhere without being part
of any one source. Two kinds live here:
- derived root-set queries (
aliveVia,rootsOf) that are justreachableover therootNodesprimitive; and - the detector combinators and grouping keys the shipped
catalog is written with (
amplifier,lazyKids,chainRun,site, …), factored out so any query can use them.
Each helper's grade is inferred from the primitives it calls (grades
flow through lets), so aliveVia picks up roots/paths/
reverse-graph from reachable + rootNodes with no hand-written
grade. This reference is rendered from the prelude source the engine
loads, so it cannot drift.
#aliveVia
Type RPQ -> Set Node
let aliveVia p = reachable p rootNodes
#rootsOf
Type Set Node -> Set Node
let rootsOf n = reachable /(~ptr)*/ n ∩ rootNodes
#retainerPathsWith
Type Set Node -> (Q 1) -> (Q 1) -> Seq Path
let retainerPathsWith t maxPaths maxDepth =
paths /(~ptr)*/ t rootNodes maxPaths maxDepth
#retainerPaths
Type Set Node -> Seq Path
let retainerPaths t = retainerPathsWith t 5 30
#retainers
Type Node -> Bag Edge
let retainers n = [ e | e <- edges, e.tgt == n ]
#deletionWorlds
Type (Node -> t0) -> (Bag t0) -> Bag (t0, Set Node)
let deletionWorlds f cands =
freedWorlds /strong*/ cands
[ (r, allWorlds) | r <- toBag rootNodes ]
(\n -> allBut (f n))
#retainedByEach
Type (Node -> t0) -> (Bag t0) -> Bag (t0, Set Node)
let retainedByEach f cands =
reachWorlds /strong*/ cands
[ (r, world (f r)) | r <- toBag rootNodes ]
(\_ -> allWorlds)
#aliveAvoiding
Type (Node -> Bool) -> Set Node
let aliveAvoiding p =
reachable /strong[\n -> not (p n)]*/
(toSet [ r | r <- toBag rootNodes, not (p r) ])
#freedIf
Type (Node -> Bool) -> Bag Node
let freedIf p = [ x | x <- heap, not (x in aliveAvoiding p) ]
#thunkBroad
Type Node -> Bool
let thunkBroad c = c is (Thunk | Ap | BlackHole)
#thunkLike
Type Node -> Bool
let thunkLike c = c.type in {THUNK, AP}
#indirection
Type Node -> Bool
let indirection c = c.type in {IND, BLACKHOLE}
#container
Type Node -> Bool
let container c =
c.label == "Bin" || c.label == "Tip"
|| c.label endsWith ".Bin" || c.label endsWith ".Tip"
|| c.label endsWith ".HashMap" || c.label endsWith ".HashSet"
|| c.label endsWith ".Node" || c.label endsWith ".Full"
|| c.label endsWith ".BitmapIndexed"
#ascents
Type Node -> { retainedBy : Query retained-by, retainerPaths : Query retainer-paths }
let ascents c = { retainedBy = expand retained-by { node = c }
, retainerPaths = expand retainer-paths { target = c } }
#descents
Type Node -> { references : Query node-refs, dominates : Query dom-under }
let descents c = { references = expand node-refs { node = c }
, dominates = expand dom-under { parent = c } }
#drills
Type Node -> { retainedBy : Query retained-by, retainerPaths : Query retainer-paths, references : Query node-refs, dominates : Query dom-under }
let drills c = { ..ascents c, ..descents c }
#ratioChip
Type (Q 1) -> (Q 1) -> Node -> Chip
let ratioChip hi mid c =
chip "ratio" (number (ratio c))
(tiers [ ratio c >= hi ~> bad, ratio c >= mid ~> warn ])
#amplifier
Type (Node -> Bool) -> (Q B) -> (Q 1) -> (Q 1) -> (Q 1) -> Bag Finding
let amplifier (match) (minBytes = 16KiB) (minRatio = 16)
(hi = 32) (mid = 16) =
[ card c { chips = [ratioChip hi mid c], ..drills c }
| c <- heap, match c, c.retained >= minBytes, ratio c >= minRatio ]
#thunksChip
Type (Q 1) -> (Q 1) -> Node -> Chip
let thunksChip hi mid c =
chip "thunks" (percent (frac thunkLike (children c)))
(tiers [ frac thunkLike (children c) >= hi ~> bad
, frac thunkLike (children c) >= mid ~> warn ])
#lazyKids
Type (Node -> Bool) -> (Q 1) -> (Q 1) -> (Q B) -> (Q 1) -> (Q 1) -> Bag Finding
let lazyKids (match) (minKids = 4) (minFrac = 0.5) (minBytes = 8KiB)
(hi = 0.9) (mid = 0.75) =
[ card c { count = count (filter thunkLike (children c))
, evidence = [c] <> top 2 .retained (children c)
, chips = [thunksChip hi mid c]
, ..drills c }
| c <- heap, match c, c.retained >= minBytes
, count (children c) >= minKids
, frac thunkLike (children c) >= minFrac ]
#chainRun
Type t0 -> (Node -> Bool) -> (Q 1) -> (Q B) -> (Q 1) -> (Q 1) -> Bag Finding
let chainRun (what = "Chain") (cell = thunkLike) (minLen = 3)
(minBytes = 4KiB) (hi = 16) (mid = 8) =
[ card (first p)
{ title = "{what} chain ({count (nodes p)} deep)"
, count = count (nodes p), evidence = take 3 (nodes p)
, chips = [chip "depth" (number (count (nodes p)))
(tiers [ count (nodes p) >= hi ~> bad
, count (nodes p) >= mid ~> warn ])]
, ..drills (first p) }
| p <- chains /dom[cell]/ cell
, count (nodes p) >= minLen, (first p).retained >= minBytes ]
#consish
Type Node -> Bool
let consish c = c.label == ":" || c.label == "GHC.Types.:"
|| c.label endsWith "Types.:"
#domKey
Type Node -> { label : t1, type : t2, pos : t3 }
let domKey c = { c.label, c.type, c.pos }
#site
Type Node -> { module : t1, name : t2, pos : t3 }
let site c = { c.module, c.name, c.pos }
#nDead
Type t0 -> Q 1
let nDead g = count [ x | x <- g, not x.rooted ]
#bDead
Type t0 -> Q u0
let bDead g = sum [ x.self | x <- g, not x.rooted ]