public
Created

Performance discrepancies with macro generalization

  • Download Gist
compare.rkt
Racket
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
#lang racket/load
 
(module tweaks racket
(require (for-syntax syntax/parse))
(provide <- for/union for*/union for*/set
appl fix do join extend get-cont lookup-env lookup P parse
compile widen push
join-stores s->c c->s
(struct-out ev^)
(struct-out co^)
(struct-out ap^)
(struct-out ap-op^)
(struct-out ans^)
 
(struct-out exp)
(struct-out var)
(struct-out num)
(struct-out bln)
(struct-out lam)
(struct-out app)
(struct-out rec)
(struct-out ife)
(struct-out 1op)
(struct-out 2op)
(struct-out clos)
(struct-out rlos)
 
(struct-out ar)
(struct-out fn)
(struct-out ifk)
(struct-out 1opk)
(struct-out 2opak)
(struct-out 2opfk)
(struct-out state)
(struct-out ev)
(struct-out co)
(struct-out ap)
(struct-out ap-op)
(struct-out ans))
 
(define <- (case-lambda))
(begin-for-syntax
(define-syntax-class guards
#:attributes ((guard-forms 1) (gv 1) (gfromv 1)) #:literals (<-)
(pattern ((~and (~seq (~or [i:id e:expr]
[(is:id ...) e0:expr]) ...)
(~seq start:expr ...))
(~optional (~seq [v:id <- (σ:expr fromv:expr)] ...)
#:defaults ([(v 1) #'()]
[(σ 1) #'()]
[(fromv 1) #'()])))
;; XXX: Switch these for laziness
;;#:with (guard-forms ...) #'(start ... [v (get-val σ fromv)] ...)
;;#:with (gv ...) #'() #:with (gfromv ...) #'()
#:with (guard-forms ...) #'(start ...)
#:with (gv ...) #'(v ...)
#:with (gfromv ...) #'(fromv ...)
)))
(define-syntax (for/get-vals stx)
(syntax-parse stx
[(_ form:id targets:expr gs:guards body1:expr body:expr ...)
(syntax/loc stx
(form targets (gs.guard-forms ...)
(let* ([gs.gv gs.gfromv] ...)
body1 body ...)))]))
 
(define-syntax-rule (for/union guards body1 body ...)
(for/get-vals for/fold ([res (set)]) guards (set-union res (let () body1 body ...))))
(define-syntax-rule (for*/union guards body1 body ...)
(for/get-vals for*/fold ([res (set)]) guards (set-union res (let () body1 body ...))))
 
(define-syntax-rule (for*/set guards body1 body ...)
(for/get-vals for*/fold ([res (set)]) guards (set-add res (let () body1 body ...))))
 
;; (X -> Set X) -> (Set X) -> (Set X)
(define ((appl f) s)
(for/union ([x (in-set s)]) (f x)))
 
;; (X -> Set X) (Set X) -> (Set X)
;; Calculate fixpoint of (appl f).
(define (fix f s)
(let loop ((accum (set)) (front s))
(if (set-empty? front)
accum
(let ((new-front ((appl f) front)))
(loop (set-union accum front)
(set-subtract new-front accum))))))
 
;; An Exp is one of:
;; (var Lab Exp)
;; (num Lab Number)
;; (bln Lab Boolean)
;; (lam Lab Sym Exp)
;; (app Lab Exp Exp)
;; (rec Sym Lam)
;; (if Lab Exp Exp Exp)
(struct exp (lab) #:transparent)
(struct var exp (name) #:transparent)
(struct num exp (val) #:transparent)
(struct bln exp (b) #:transparent)
(struct lam exp (var exp) #:transparent)
(struct app exp (rator rand) #:transparent)
(struct rec (name fun) #:transparent)
(struct ife exp (t c a) #:transparent)
(struct 1op exp (o a) #:transparent)
(struct 2op exp (o a b) #:transparent)
 
;; A Val is one of:
;; - Number
;; - Boolean
;; - (clos Lab Sym Exp Env)
;; - (rlos Lab Sym Sym Exp Env)
(struct clos (l x e ρ) #:transparent)
(struct rlos (l f x e ρ) #:transparent)
 
;; A Cont is one of:
;; - 'mt
;; - (ar Exp Env Cont)
;; - (fn Val Cont)
;; - (ifk Exp Exp Env Cont)
;; - (1opk Opr Cont)
;; - (2opak Opr Exp Env Cont)
;; - (2opfk Opr Val Cont)
(struct ar (e ρ k) #:transparent)
(struct fn (v k) #:transparent)
(struct ifk (c a ρ k) #:transparent)
(struct 1opk (o k) #:transparent)
(struct 2opak (o e ρ k) #:transparent)
(struct 2opfk (o v k) #:transparent)
 
;; State
(struct state (σ) #:transparent)
(struct ev state (e ρ k) #:transparent)
(struct co state (k v) #:transparent)
(struct ap state (f a k) #:transparent)
(struct ap-op state (o vs k) #:transparent)
(struct ans state (v) #:transparent)
 
(define (lookup ρ σ x)
(hash-ref σ (hash-ref ρ x)))
(define (lookup-env ρ x)
(hash-ref ρ x))
(define (get-cont σ l)
(hash-ref σ l))
(define (extend ρ x v)
(hash-set ρ x v))
(define (join σ a s)
(hash-set σ a
(set-union s (hash-ref σ a (set)))))
 
(define-syntax-rule (do guards e)
(for*/set guards e))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Widening State to State^
 
;; State^ = (cons (Set Conf) Store)
 
;; Conf
(struct ev^ (e ρ k) #:transparent)
(struct co^ (k v) #:transparent)
(struct ap^ (f a k) #:transparent)
(struct ap-op^ (o vs k) #:transparent)
(struct ans^ (v) #:transparent)
 
;; The following "functions" are called in hot loops,
;; so make them macros instead to inline them across the module boundary.
;; Conf Store -> State
(define-syntax-rule (c->s c σ)
(match c
[(ev^ e ρ k) (ev σ e ρ k)]
[(co^ k v) (co σ k v)]
[(ap^ f a k) (ap σ f a k)]
[(ap-op^ o vs k) (ap-op σ o vs k)]
[(ans^ v) (ans σ v)]))
 
;; State -> Conf
(define-syntax-rule (s->c s)
(match s
[(ev _ e ρ k) (ev^ e ρ k)]
[(co _ k v) (co^ k v)]
[(ap _ f a k) (ap^ f a k)]
[(ap-op _ o vs k) (ap-op^ o vs k)]
[(ans _ v) (ans^ v)]))
;; Set State -> Store
(define-syntax-rule (join-stores ss)
(letrec ([join-store (λ (σ1 σ2)
(for/fold ([σ σ1])
([k×v (in-hash-pairs σ2)])
(hash-set σ (car k×v)
(set-union (cdr k×v)
(hash-ref σ (car k×v) (set))))))])
(for/fold ([σ (hash)])
([s ss])
(join-store σ (state-σ s)))))
 
(define-syntax-rule (widen b)
(cond [(number? b) 'number]
[else (error "Unknown base value" b)]))
 
(define-syntax-rule (push s)
(match s
[(ev σ e ρ k)
(define a (exp-lab e))
(values (join σ a (set k))
a)]))
(define (parse sexp)
(match sexp
[`(let* () ,e) (parse e)]
[`(let* ((,x ,e) . ,r) ,b)
(app (gensym)
(lam (gensym) x (parse `(let* ,r ,b)))
(parse e))]
[`(lambda (,x) ,e)
(lam (gensym) x (parse e))]
[`(if ,e0 ,e1 ,e2)
(ife (gensym) (parse e0) (parse e1) (parse e2))]
[`(rec ,f ,e)
(rec f (parse e))]
[`(sub1 ,e)
(1op (gensym) 'sub1 (parse e))]
[`(add1 ,e)
(1op (gensym) 'add1 (parse e))]
[`(zero? ,e)
(1op (gensym) 'zero? (parse e))]
[`(* ,e0 ,e1)
(2op (gensym) '* (parse e0) (parse e1))]
[`(,e0 ,e1)
(app (gensym)
(parse e0)
(parse e1))]
[(? boolean? b) (bln (gensym) b)]
[(? number? n) (num (gensym) n)]
[(? symbol? s) (var (gensym) s)]))
 
(define (compile var-case e)
(let compile ([e e])
(match e
[(var l x) (var-case x)]
[(num l n) (λ (σ ρ k) (set (co σ k n)))]
[(bln l b) (λ (σ ρ k) (set (co σ k b)))]
[(lam l x e)
(define c (compile e))
(λ (σ ρ k) (set (co σ k (clos l x c ρ))))]
[(rec f (lam l x e))
(define c (compile e))
(λ (σ ρ k) (set (co σ k (rlos l f x c ρ))))]
[(app l e0 e1)
(define c0 (compile e0))
(define c1 (compile e1))
(λ (σ ρ k)
;; "ev" simulated for push's sake.
(define-values (σ* a) (push (ev σ (app l e0 e1) ρ k)))
(c0 σ* ρ (ar c1 ρ a)))]
[(ife l e0 e1 e2)
(define c0 (compile e0))
(define c1 (compile e1))
(define c2 (compile e2))
(λ (σ ρ k)
(define-values (σ* a) (push (ev σ (ife l e0 e1 e2) ρ k)))
(c0 σ* ρ (ifk c1 c2 ρ a)))]
[(1op l o e)
(define c (compile e))
(λ (σ ρ k)
(define-values (σ* a) (push (ev σ (1op l o e) ρ k)))
(c σ* ρ (1opk o a)))]
[(2op l o e0 e1)
(define c0 (compile e0))
(define c1 (compile e1))
(λ (σ ρ k)
(define-values (σ* a) (push (ev σ (2op l o e0 e1) ρ k)))
(c0 σ* ρ (2opak o c1 ρ a)))])))
 
(define P
;; Ian's example, curried, alpha renamed and
;; let* in place of define where possible.
'(let* ((plus (lambda (p1)
(lambda (p2)
(lambda (pf)
(lambda (x) ((p1 pf) ((p2 pf) x)))))))
(mult (lambda (m1)
(lambda (m2)
(lambda (mf) (m2 (m1 mf))))))
(pred (lambda (n)
(lambda (rf)
(lambda (rx)
(((n (lambda (g) (lambda (h) (h (g rf)))))
(lambda (ignored) rx))
(lambda (id) id))))))
(sub (lambda (s1)
(lambda (s2)
((s2 pred) s1))))
 
(church0 (lambda (f0) (lambda (x0) x0)))
(church1 (lambda (f1) (lambda (x1) (f1 x1))))
(church2 (lambda (f2) (lambda (x2) (f2 (f2 x2)))))
(church3 (lambda (f3) (lambda (x3) (f3 (f3 (f3 x3))))))
(church0? (lambda (z) ((z (lambda (zx) #f)) #t)))
(c->n (lambda (cn) ((cn (lambda (u) (add1 u))) 0)))
(church=? (rec c=?
(lambda (e1)
(lambda (e2)
(if (church0? e1)
(church0? e2)
(if (church0? e2)
#f
((c=? ((sub e1) church1)) ((sub e2) church1)))))))))
;; multiplication distributes over addition
((church=? ((mult church2) ((plus church1) church3)))
((plus ((mult church2) church1)) ((mult church2) church3))))))
 
(module fast racket
(require 'tweaks)
 
;; Expr -> (Store Env Cont -> State)
(define (fcompile e)
(compile (λ (x)
(λ (σ ρ k)
(do ([v (lookup ρ σ x)])
(co σ k v))))
e))
 
;; "Bytecode" interpreter
;; State -> State
(define (step-compiled state)
(match state
[(co σ k v)
(match k
['mt (set (ans σ v))]
[(ar c ρ l) (c σ ρ (fn v l))]
[(fn f l) (do ([k (get-cont σ l)])
(ap σ f v k))]
[(ifk c a ρ l)
(for/union ([k (get-cont σ l)])
((if v c a) σ ρ k))]
[(1opk o l)
(do ([k (get-cont σ l)])
(ap-op σ o (list v) k))]
[(2opak o c ρ l)
(c σ ρ (2opfk o v l))]
[(2opfk o u l)
(do ([k (get-cont σ l)])
(ap-op σ o (list v u) k))])]
 
[(ap σ fun a k)
(match fun
[(clos l x c ρ)
(define-values (ρ* σ*) (bind state))
(c σ* ρ* k)]
[(rlos l f x c ρ)
(define-values (ρ* σ*) (bind state))
(c σ* ρ* k)]
[_ (set state)])]
 
[(ap-op σ o vs k)
(match* (o vs)
[('zero? (list (? number? n))) (set (co σ k (zero? n)))]
[('sub1 (list (? number? n))) (set (co σ k (widen (sub1 n))))]
[('add1 (list (? number? n))) (set (co σ k (widen (add1 n))))]
[('zero? (list 'number))
(set (co σ k #t)
(co σ k #f))]
[('sub1 (list 'number)) (set (co σ k 'number))]
[('* (list (? number? n) (? number? m)))
(set (co σ k (widen (* m n))))]
[('* (list (? number? n) 'number))
(set (co σ k 'number))]
[('* (list 'number 'number))
(set (co σ k 'number))]
[(_ _) (set state)])]
 
[_ (set state)]))
 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 0CFA-style Abstract semantics
 
(define (bind s)
(match s
[(ap σ (clos l x e ρ) v k)
(values (extend ρ x x)
(extend σ x (set v)))]
[(ap σ (rlos l f x e ρ) v k)
(values (extend (extend ρ x x) f f)
(join (join σ x (set v)) f (set (rlos l f x e ρ))))]))
 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
;; Exp -> Set Val
;; 0CFA without store widening
(define (aval e)
(for/set ([s (fix step-compiled (inj e))]
#:when (ans? s))
(ans-v s)))
 
;; Exp -> Set Vlal
;; 0CFA with store widening
(define (aval^ e)
(for/fold ([vs (set)])
([s (fix wide-step (inj-wide e))])
(set-union vs
(match s
[(cons cs σ)
(for/set ([c cs]
#:when (ans^? c))
(ans^-v c))]))))
 
;; Exp -> Set State
(define (inj e)
((fcompile e) (hash) (hash) 'mt))
 
;; Exp -> Set State^
(define (inj-wide e)
(for/first ([s (inj e)])
(set (cons (set (s->c s)) (state-σ s)))))
 
;; State^ -> { State^ }
(define (wide-step state)
(match state
[(cons cs σ)
(define ss (for/set ([c cs]) (c->s c σ)))
(define ss* ((appl step-compiled) ss))
(set (cons (for/set ([s ss*]) (s->c s))
(join-stores ss*)))]))
 
(time (aval^ (parse P))))
 
(module slow racket
(require 'tweaks)
;; 0CFA in the AAM style on some hairy Church numeral churning
 
(struct addr (a) #:transparent)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Lazy/non-lazy
;; XXX: Switch these for laziness
#;(define-syntax-rule (deref ρ σ k x) (set (co σ k (addr (lookup-env ρ x)))))
 
(define-syntax-rule (deref ρ σ k x)
(do ([v (lookup ρ σ x)])
(co σ k v)))
 
;; Store (Addr + Val) -> Set Val
;; XXX: Switch these for laziness
#;(define-syntax-rule (get-val σ v) (match v [(addr loc) (hash-ref σ loc (λ () (error "~a ~a" loc σ)))] [_ (set v)]))
(define-syntax-rule (get-val σ v) (set v))
 
(define (scompile e)
(compile (λ (x)
(λ (σ ρ k) (deref ρ σ k x)))
e))
 
;; State -> Set State
(define (step-compiled state)
(match state
[(co σ k v)
(match k
['mt (do ([v <- (σ v)])
(ans σ v))]
[(ar c ρ l) (c σ ρ (fn v l))]
[(fn f l)
(do ([k (get-cont σ l)]
[f <- (σ f)])
(ap σ f v k))]
[(ifk c a ρ l)
(for*/union ([k (get-cont σ l)]
[v <- (σ v)])
((if v c a) σ ρ k))]
[(1opk o l)
(do ([k (get-cont σ l)]
[v <- (σ v)])
(ap-op σ o (list v) k))]
[(2opak o c ρ l)
(c σ ρ (2opfk o v l))]
[(2opfk o u l)
(do ([k (get-cont σ l)]
[v <- (σ v)]
[u <- (σ u)])
(ap-op σ o (list v u) k))])]
 
[(ap σ fun a k)
(match fun
[(clos l x c ρ)
(define-values (ρ* σ*) (bind state))
(c σ* ρ* k)]
[(rlos l f x c ρ)
(define-values (ρ* σ*) (bind state))
(c σ* ρ* k)]
;; stuck
[_ (set state)])]
 
[(ap-op σ o vs k)
(match* (o vs)
[('zero? (list (? number? n))) (set (co σ k (zero? n)))]
[('sub1 (list (? number? n))) (set (co σ k (widen (sub1 n))))]
[('add1 (list (? number? n))) (set (co σ k (widen (add1 n))))]
[('zero? (list 'number))
(set (co σ k #t)
(co σ k #f))]
[('sub1 (list 'number)) (set (co σ k 'number))]
[('* (list (? number? n) (? number? m)))
(set (co σ k (widen (* m n))))]
[('* (list (? number? n) 'number))
(set (co σ k 'number))]
[('* (list 'number 'number))
(set (co σ k 'number))]
[(_ _) (set state)])]
;; stuck or an answer
[_ (set state)]))
 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 0CFA-style Abstract semantics
 
(define (bind s)
(match s
[(ap σ (clos l x e ρ) v k)
(values (extend ρ x x)
(join σ x (get-val σ v)))]
[(ap σ (rlos l f x e ρ) v k)
(values (extend (extend ρ x x) f f)
(join (join σ x (get-val σ v)) f (set (rlos l f x e ρ))))]))
 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
;; Exp -> Set Vlal
;; 0CFA with store widening
(define (aval^-compiled e)
(for/union ([s (fix wide-step-compiled (inj-wide-compiled e))])
(match s
[(cons cs σ)
(for/set ([c cs]
#:when (ans^? c))
(ans^-v c))])))
 
;; Exp -> Set State
(define (inj-compiled e)
((scompile e) (hash) (hash) 'mt))
 
;; Exp -> Set State^
(define (inj-wide-compiled e)
(for/first ([s (inj-compiled e)])
(set (cons (set (s->c s))
(state-σ s)))))
 
;; Exp -> Set State^
(define (inj-wide e)
(set (cons (set (ev^ e (hash) 'mt)) (hash))))
 
;; State^ -> { State^ }
(define (wide-step-compiled state)
(match state
[(cons cs σ)
(define ss (for/set ([c cs]) (c->s c σ)))
(define ss* ((appl step-compiled) ss))
(set (cons (for/set ([s ss*]) (s->c s))
(join-stores ss*)))]))
 
(time (aval^-compiled (parse P))))
(require 'fast 'slow)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.