Skip to content

Commit 6059538

Browse files
committed
Add a --profile mode
1 parent 005c66e commit 6059538

File tree

3 files changed

+87
-57
lines changed

3 files changed

+87
-57
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ report/*
88
rival-compiled
99
*~
1010
test.rival
11+
.worktrees/

main.rkt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,11 @@
138138
(module+ main
139139
(require "repl.rkt"
140140
racket/cmdline)
141+
(define repl-mode repl-main)
141142
(command-line #:program "racket -l rival"
143+
#:once-each [("--profile") "Print timing results"
144+
(set! repl-mode repl-profile)]
142145
#:args ([file #f])
143146
(if file
144-
(call-with-input-file file rival-repl)
145-
(rival-repl (current-input-port)))))
147+
(call-with-input-file file repl-mode)
148+
(repl-mode (current-input-port)))))

repl.rkt

Lines changed: 81 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
bf-precision
77
bigfloat->string
88
bigfloat?
9-
bf))
9+
bf)
10+
profile)
1011
(require "eval/main.rkt"
1112
"eval/machine.rkt"
1213
"utils.rkt")
13-
(provide rival-repl)
14+
(provide repl-main repl-profile)
1415

1516
(define (create-discs args bodies repl)
1617
(for/list ([body (in-list bodies)])
@@ -181,64 +182,89 @@
181182
(~r (* (execution-time exec) 1000) #:precision '(= 1)))))
182183
value]))))
183184

184-
(define (rival-repl p)
185+
(define (repl-run repl cmd #:print? [print? #t])
186+
(with-handlers ([exn:fail:user? (lambda (e) (eprintf "ERROR ~a\n" (exn-message e)))])
187+
(match cmd
188+
[`(set precision fp64)
189+
(set-repl-precision! repl 'fp64)]
190+
[`(set precision ,(? integer? n))
191+
(when (< n 4)
192+
(raise-user-error 'set "Precision must be an integer greater than 3"))
193+
(set-repl-precision! repl (list 'bf n))]
194+
[`(define (,(? symbol? name) ,(? symbol? args) ...)
195+
,bodies ...)
196+
(repl-save-machine! repl name args bodies)]
197+
[`(eval ,name ,(? (disjoin real? boolean?) vals) ...)
198+
(define machine (repl-get-machine repl name))
199+
(check-args! name machine vals)
200+
(define out (repl-apply repl machine vals))
201+
(when print?
202+
(if (string? out)
203+
(displayln out)
204+
(for ([val (in-vector out)])
205+
(displayln (bigfloat->string val)))))]
206+
[`(explain ,name ,(? (disjoin real? boolean?) vals) ...)
207+
(define machine (repl-get-machine repl name))
208+
(check-args! name machine vals)
209+
210+
;; Make sure the cache is warm
211+
(repl-apply repl machine vals)
212+
;; Make sure the profile is clear
213+
(rival-profile machine 'executions)
214+
215+
;; Time the actual execution
216+
(define start (current-inexact-milliseconds))
217+
(repl-apply repl machine vals)
218+
(define end (current-inexact-milliseconds))
219+
220+
(when print?
221+
(write-explain machine)
222+
(printf "\nTotal: ~aµs\n" (~r (* (- end start) 1000) #:precision '(= 1))))]
223+
[(or '(help) 'help)
224+
(displayln "This is the Rival REPL, a demo of the Rival real evaluator.")
225+
(newline)
226+
(displayln "Commands:")
227+
(displayln " (set precision <n>) Set working precision to n")
228+
(displayln " (define (<name> <args> ...) <body> ...) Define a named function")
229+
(displayln " (eval <name> <vals> ...) Evaluate a named function")
230+
(displayln
231+
" (explain <name> <vals> ...) Show profile for evaluating a named function")
232+
(newline)
233+
(displayln "A closed expression can always be used in place of a named function.")]
234+
[_ (printf "Unknown command ~a; use help for command list\n" cmd)])))
235+
236+
(define (repl-main p)
185237
(let/ec k
186238
(parameterize ([read-decimal-as-inexact #f]
187239
[*rival-name-constants* #t])
188240
(define repl (make-repl))
189241
(when (terminal-port? p)
190242
(display "> "))
191243
(for ([cmd (in-port read p)])
192-
(with-handlers ([exn:fail:user? (lambda (e) (eprintf "ERROR ~a\n" (exn-message e)))])
193-
(match cmd
194-
[`(set precision fp64)
195-
(set-repl-precision! repl 'fp64)]
196-
[`(set precision ,(? integer? n))
197-
(when (< n 4)
198-
(raise-user-error 'set "Precision must be an integer greater than 3"))
199-
(set-repl-precision! repl (list 'bf n))]
200-
[`(define (,(? symbol? name) ,(? symbol? args) ...)
201-
,bodies ...)
202-
(repl-save-machine! repl name args bodies)]
203-
[`(eval ,name ,(? (disjoin real? boolean?) vals) ...)
204-
(define machine (repl-get-machine repl name))
205-
(check-args! name machine vals)
206-
(define out (repl-apply repl machine vals))
207-
(if (string? out)
208-
(displayln out)
209-
(for ([val (in-vector out)])
210-
(displayln (bigfloat->string val))))]
211-
[`(explain ,name ,(? (disjoin real? boolean?) vals) ...)
212-
(define machine (repl-get-machine repl name))
213-
(check-args! name machine vals)
214-
215-
;; Make sure the cache is warm
216-
(repl-apply repl machine vals)
217-
;; Make sure the profile is clear
218-
(rival-profile machine 'executions)
219-
220-
;; Time the actual execution
221-
(define start (current-inexact-milliseconds))
222-
(repl-apply repl machine vals)
223-
(define end (current-inexact-milliseconds))
224-
225-
(write-explain machine)
226-
227-
(printf "\nTotal: ~aµs\n" (~r (* (- end start) 1000) #:precision '(= 1)))]
228-
[(or '(help) 'help)
229-
(displayln "This is the Rival REPL, a demo of the Rival real evaluator.")
230-
(newline)
231-
(displayln "Commands:")
232-
(displayln " (set precision <n>) Set working precision to n")
233-
(displayln " (define (<name> <args> ...) <body> ...) Define a named function")
234-
(displayln " (eval <name> <vals> ...) Evaluate a named function")
235-
(displayln
236-
" (explain <name> <vals> ...) Show profile for evaluating a named function")
237-
(newline)
238-
(displayln "A closed expression can always be used in place of a named function.")]
239-
[(or '(exit) 'exit) (k)]
240-
[_ (printf "Unknown command ~a; use help for command list\n" cmd)]))
244+
(if (set-member? '((exit) exit) cmd)
245+
(k)
246+
(repl-run repl cmd))
241247
(when (terminal-port? p)
242-
(display "> "))))
243-
(when (terminal-port? p)
244-
(displayln "exit"))))
248+
(display "> ")))))
249+
(when (terminal-port? p)
250+
(displayln "exit")))
251+
252+
(define (repl-profile p)
253+
(define cmds
254+
(parameterize ([read-decimal-as-inexact #f])
255+
(for/list ([cmd (in-port read p)])
256+
cmd)))
257+
(define t0 (current-inexact-milliseconds))
258+
(define m0 (current-memory-use 'cumulative))
259+
(define repl (make-repl))
260+
(match-define (cons t1 m1)
261+
(profile
262+
(begin
263+
(for ([cmd (in-list cmds)])
264+
(repl-run repl cmd #:print? #f))
265+
(cons (current-inexact-milliseconds)
266+
(current-memory-use 'cumulative)))
267+
#:delay 0.001))
268+
(eprintf "Ran in ~ams, allocated ~aMB\n"
269+
(~r (- t1 t0) #:precision '(= 3))
270+
(~r (/ (- m1 m0) 1000000) #:precision '(= 3))))

0 commit comments

Comments
 (0)