     zero-counts [ sample-counts-slice v+ ] reduce ;
 
 TUPLE: profile-node
-    total-time gc-time jit-time foreign-time foreign-thread-time children ;
+    total-time gc-time jit-time foreign-time foreign-thread-time children
+    depth ;
 
-: <profile-node> ( times children -- node )
-    [ 5 firstn [ samples>time ] 5 napply ] dip profile-node boa ;
+: <profile-node> ( times children depth -- node )
+    [ 5 firstn [ samples>time ] 5 napply ] 2dip profile-node boa ;
 
 : <profile-root-node> ( samples collector-quot -- node )
-    [ sum-counts ] swap bi <profile-node> ; inline
+    [ sum-counts ] swap bi 0 <profile-node> ; inline
 
 :: (collect-subtrees) ( samples child-quot -- children )
     samples [ sample-callstack leaf-callstack? not ] filter
     [ f ] [ child-quot call ] if-empty ; inline
 
-: collect-tops ( samples -- node )
-    [ unclip-callstack ] collect-pairs [
+:: collect-tops ( samples depth -- node )
+    samples [ unclip-callstack ] collect-pairs [
         [ sum-counts ]
-        [ [ collect-tops ] (collect-subtrees) ] bi <profile-node>
+        [ [ depth 1 + collect-tops ] (collect-subtrees) ] bi depth <profile-node>
     ] assoc-map ;
 
 : redundant-root-node? ( assoc -- ? )


: flat ( -- tree )
    get-raw-profile-data (flat) ;

: depth. ( depth -- )
    [ "| " write ] times ;

: trim-root ( root -- root' )
    dup redundant-root-node? [ children>> values first trim-root ] when ;

:: (top-down) ( samples depth -- tree )
    samples collect-threads
    [ [ depth collect-tops ] <profile-root-node> trim-root ] assoc-map ;

: top-down ( -- tree )
    get-raw-profile-data 0 (top-down) ;

:: counts+at ( key assoc sample -- )
    key assoc [ zero-counts or sample sample-counts-slice v+ ] change-at ;

:: collect-flat ( samples -- flat )
    IH{ } clone :> per-word-samples
    samples [| sample |
        sample sample-callstack unique keys [ ignore-word? not ] filter [
            per-word-samples sample counts+at
        ] each
    ] each
    per-word-samples [ f 0 <profile-node> ] assoc-map ;

: redundant-flat-node? ( child-node root-node -- ? )
    [ total-time>> ] bi@ = ;
