Skip to content

Commit e949171

Browse files
committed
Add option to report costs of space-efficient contracts specifically.
1 parent 89638fb commit e949171

File tree

3 files changed

+50
-9
lines changed

3 files changed

+50
-9
lines changed

main.rkt

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,12 @@
6666
(define (analyze-contract-samples
6767
contract-samples
6868
samples
69+
#:report-space-efficient? [report-space-efficient? #f]
6970
#:module-graph-view-file [module-graph-view-file #f]
7071
#:boundary-view-file [boundary-view-file #f]
7172
#:boundary-view-key-file [boundary-view-key-file #f])
7273
(define correlated (correlate-contract-samples contract-samples samples))
73-
(print-breakdown correlated)
74+
(print-breakdown correlated #:report-space-efficient? report-space-efficient?)
7475
(when module-graph-view-file
7576
(module-graph-view correlated module-graph-view-file))
7677
(when boundary-view-file
@@ -81,18 +82,33 @@
8182
;; Break down contract checking time by contract, then by callee and by chain
8283
;; of callers.
8384

84-
(define (print-breakdown correlated [show-by-caller? #f])
85+
(define (/. num den) (/ num (max den 1) 1.0))
86+
87+
(define (print-breakdown correlated
88+
[show-by-caller? #f]
89+
#:report-space-efficient? [report-space-efficient? #f])
8590
(match-define (contract-profile
8691
total-time live-contract-samples all-blames regular-profile)
8792
correlated)
8893

8994
(define total-contract-time (samples-time live-contract-samples))
90-
(define contract-ratio (/ total-contract-time (max total-time 1) 1.0))
95+
(define contract-ratio (/. total-contract-time total-time))
9196
(printf "Running time is ~a% contracts\n"
9297
(~r (* 100 contract-ratio) #:precision 2))
9398
(printf "~a/~a ms\n\n"
9499
(~r total-contract-time #:precision 0)
95100
total-time)
101+
(define (only-space-efficient samples)
102+
(filter contract-sample-space-efficient? samples))
103+
(define space-efficient-samples (only-space-efficient live-contract-samples))
104+
(when report-space-efficient?
105+
(define total-space-efficient-time (samples-time space-efficient-samples))
106+
(define space-efficient-ratio
107+
(/. total-space-efficient-time total-contract-time))
108+
(printf "(of those, ~a% (~a/~a ms) are space-efficient)\n\n"
109+
(~r (* 100 space-efficient-ratio) #:precision 2)
110+
(~r total-space-efficient-time #:precision 0)
111+
(~r total-contract-time #:precision 0)))
96112

97113
(define shorten-source
98114
(make-srcloc-shortener all-blames blame-source))
@@ -105,7 +121,15 @@
105121
#:limit-prefix? #t
106122
#:width (- location-width 1))))
107123
(define (format-samples-time s)
108-
(format "~a ms" (~r (samples-time s) #:precision 2)))
124+
(define total-time (samples-time s))
125+
(format "~a ms~a"
126+
(~r total-time #:precision 2)
127+
(if report-space-efficient?
128+
(format " (~a% space-efficient)"
129+
(~r (* 100 (/. (samples-time (only-space-efficient s))
130+
total-time))
131+
#:precision 2))
132+
"")))
109133

110134
(define samples-by-contract
111135
(sort (group-by (lambda (x) (blame-contract (contract-sample-blame x)))
@@ -178,7 +202,9 @@
178202
(~optional (~seq #:boundary-view-file boundary-view-file:expr)
179203
#:defaults ([boundary-view-file #'#f]))
180204
(~optional (~seq #:boundary-view-key-file boundary-view-key-file:expr)
181-
#:defaults ([boundary-view-key-file #'#f])))
205+
#:defaults ([boundary-view-key-file #'#f]))
206+
(~optional (~seq #:report-space-efficient? report-space-efficient?:expr)
207+
#:defaults ([report-space-efficient? #'#f])))
182208
...
183209
body:expr ...)
184210
#`(let ([sampler (create-sampler
@@ -212,14 +238,17 @@
212238
samples
213239
#:module-graph-view-file module-graph-view-file
214240
#:boundary-view-file boundary-view-file
215-
#:boundary-view-key-file boundary-view-key-file))))]))
241+
#:boundary-view-key-file boundary-view-key-file
242+
#:report-space-efficient? report-space-efficient?))))]))
216243

217244
(define (contract-profile-thunk f
218245
#:module-graph-view-file [module-graph-view-file #f]
219246
#:boundary-view-file [boundary-view-file #f]
220-
#:boundary-view-key-file [boundary-view-key-file #f])
247+
#:boundary-view-key-file [boundary-view-key-file #f]
248+
#:report-space-efficient? [report-space-efficient? #f])
221249
(contract-profile/user
222250
#:module-graph-view-file module-graph-view-file
223251
#:boundary-view-file boundary-view-file
224252
#:boundary-view-key-file boundary-view-key-file
253+
#:report-space-efficient? report-space-efficient?
225254
(f)))

raco.rkt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
(define module-graph-view-file #f)
1212
(define boundary-view-file #f)
1313
(define boundary-view-key-file #f)
14+
(define report-space-efficient? #f)
1415
(define file
1516
(command-line #:program (short-program+command-name)
1617
#:once-each
@@ -23,6 +24,9 @@
2324
[("--boundary-view-key-file") file
2425
"Output boundary view key to <file>"
2526
(set! boundary-view-key-file file)]
27+
[("--report-space-efficient")
28+
"Distinguish space-efficient contracts from non"
29+
(set! report-space-efficient? #t)]
2630
#:args (filename)
2731
filename))
2832

@@ -34,6 +38,7 @@
3438
#:module-graph-view-file module-graph-view-file
3539
#:boundary-view-file boundary-view-file
3640
#:boundary-view-key-file boundary-view-key-file
41+
#:report-space-efficient? report-space-efficient?
3742
(dynamic-require (module-to-profile file) #f))
3843

3944
(module test racket/base) ; don't run for testing

scribblings/contract-profile.scrbl

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ portions of programs, and for controlling the output.
3737
@defform[(contract-profile option ... body ...)
3838
#:grammar [(option (code:line #:module-graph-view-file module-graph-view-file)
3939
(code:line #:boundary-view-file boundary-view-file)
40-
(code:line #:boundary-view-key-file boundary-view-key-file))]]{
40+
(code:line #:boundary-view-key-file boundary-view-key-file)
41+
(code:line #:report-space-efficient? report-space-efficient?))]]{
4142

4243
Produces a report of the performance costs related to contract checking in
4344
@racket[body] on standard output.
@@ -47,6 +48,10 @@ spent checking contracts and breaks that time down by contract, and then breaks
4748
down the cost of each contract between the different contracted values that use
4849
it.
4950

51+
If @racket[report-space-efficient?] is non-false, space-efficient contracts
52+
are marked specially in the report. When using @exec{raco contract-profile},
53+
this is controlled using the @exec{--report-space-efficient} flag.
54+
5055
Additional visualizations are available on-demand, controlled by keyword
5156
arguments which specify their destination files. An argument of @racket[#f]
5257
(the default) disables that visualization.
@@ -105,7 +110,9 @@ arguments which specify their destination files. An argument of @racket[#f]
105110
[thunk (-> any)]
106111
[#:module-graph-view-file module-graph-view-file (or/c path-string #f) #f]
107112
[#:boundary-view-file boundary-view-file (or/c path-string #f) #f]
108-
[#:boundary-view-key-file boundary-view-key-file (or/c path-string #f) #f]) any]{
113+
[#:boundary-view-key-file boundary-view-key-file (or/c path-string #f) #f]
114+
[#:report-space-efficient? report-space-efficient? any/c #f])
115+
any]{
109116
Like @racket[contract-profile], but as a function which takes a thunk to
110117
profile as argument.
111118
}

0 commit comments

Comments
 (0)