Skip to content

Instantly share code, notes, and snippets.

@ap29600
Created September 9, 2026 18:37
Show Gist options
  • Select an option

  • Save ap29600/edc4fa59119045f23ee0759b9c59ace2 to your computer and use it in GitHub Desktop.

Select an option

Save ap29600/edc4fa59119045f23ee0759b9c59ace2 to your computer and use it in GitHub Desktop.
preprocessor for k flamegraphs
\d aj
lj:{+((!x),(!y)^!x)!(.x),.((!x)_y)} / left join
ag:{$[~(**x)~*x; =x; ~|/(&/~<':x;&/~>':x); =x; (x@c)!(!'1_-':c,#x)+c:&~=':x]} / adjacent group
aj: {[c;t1;t2]
k:-1_c
v:*|c
v1:t1[v]@g1: ag k1: $[1=#k;*:;+:] t1[k]
v2:t2[v]@g2:(!g1)#ag k2: $[1=#k;*:;+:] t2[k]
$[&/(&/~>/)'v2; ; (g2;v2):(g2;v2)@'\:<'v2]
p: @[(#t1)#0N;g1;:;g2@'v2{x'y}'v1]
lj[t1;t2 p]}
\d .
aj: aj.aj
\l aj.k
sanitize:{("‿"/" "\)(";"/";"\)("↲"/"\n"\)@x}
dat:1:"out.dat"
defs:+`cnt`id`lam!(!0;!0;())
sams:+`cnt`ty`id`call`stamp!5#,!0
/ read frames
r0:{256/256!dat@7 6 5 4 3 2 1 0+\:x}
r:{(n;ty;id):nti:r0@0 8 16+x
$[4=ty;x
1=ty;[defs,:,`n`id`lam!n,id,`$sanitize@dat@x+32+!l:1+r0@x+24;x+32+l]
[sams,:,`n`ty`id`call`stamp!nti,r0@x+24 32 ;x+40 ]]}
r/0;
/ resolve functions
sams: aj[`id`cnt;sams;defs]
/ resolve stacks
sams[`stack]: {:[2=y;x,z;3=y;-1_x]}\[0#`;sams`ty;sams`lam]
(beg;end):sams@(=sams`ty)@2 3
/ compute time spent in function calls
beg[`time]:(end[`stamp;i:end[`call]?beg[`call]])-beg[`stamp]
beg[`time;&^i]:0
(stack;time):+/'beg[`time;=beg`stack]
/ make time exclusive
time[stack?(-1_'stack)@i]-:time@i:&1<#'stack
`0: (";"/'$1_'stack),'" ",'$time
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment