From c7f8eff883fd0b9494b537ae856874806a775049 Mon Sep 17 00:00:00 2001 From: Vincent St-Amour Date: Tue, 11 Sep 2012 17:49:37 -0400 Subject: [PATCH] Prune profile info to only keep hot functions. --- .../typed-racket/optimizer/tool/profiling.rkt | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/collects/typed-racket/optimizer/tool/profiling.rkt b/collects/typed-racket/optimizer/tool/profiling.rkt index e8f19f042a..3f4458e1dd 100644 --- a/collects/typed-racket/optimizer/tool/profiling.rkt +++ b/collects/typed-racket/optimizer/tool/profiling.rkt @@ -6,6 +6,7 @@ (provide generate-profile node-source node-line node-col node-pos node-span + prune-profile (all-from-out profile/analyzer)) (define ((mk accessor) node) @@ -41,3 +42,27 @@ (equal? (and src (srcloc-source src)) res-mpi)) (define orig-profile (analyze-samples snapshots)) (filter right-file? (profile-nodes orig-profile))) + + +;; In some cases, we only want to consider "hot" functions for further +;; analysis. `prune-profile' prunes non-hot functions from the profile. +;; To determine what is hot, we pick, in order, the hottest functions +;; (by self time. total time could be used, but may not work as well) +;; until our picks cover `total-relative-time-cutoff' (e.g. half) of +;; the total running time. +(define total-relative-time-cutoff .95) ; picked arbitrarily, subject to tweaking +(define (prune-profile profile) + (define total-time (profile-total-time profile)) + (define target-time (* total-time total-relative-time-cutoff)) + (define sorted-nodes (sort (profile-nodes profile) > #:key node-self)) + (define top-nodes + (let loop ([nodes sorted-nodes] [res '()] [sum 0]) + ;; The last function we pick can go beyond the target. + ;; O/w, if we had a single function, taking up 100% time, it would + ;; be discarded. + (cond [(or (null? nodes) (> sum target-time)) + res] + [else + (define h (car nodes)) + (loop (cdr nodes) (cons h res) (+ sum (node-self h)))]))) + top-nodes)