Skip to content

Commit

Permalink
improve performance of lift-toplevel in lowering (#27706)
Browse files Browse the repository at this point in the history
This accounted for a surprising fraction of front-end time.
  • Loading branch information
JeffBezanson authored Jun 21, 2018
1 parent c1eb3e8 commit 748f008
Showing 1 changed file with 23 additions and 19 deletions.
42 changes: 23 additions & 19 deletions src/julia-syntax.scm
Original file line number Diff line number Diff line change
Expand Up @@ -2918,18 +2918,21 @@ f(x) = yt(x)
`(call (core svec) (call (core svec) ,@newtypes)
(call (core svec) ,@(append (cddr (cadddr te)) type-sp)))))

;; collect all toplevel-butlast expressions inside `e`, and return
;; collect all toplevel-butfirst expressions inside `e`, and return
;; (ex . stmts), where `ex` is the expression to evaluated and
;; `stmts` is a list of statements to move to the top level.
;; TODO: this implementation seems quite inefficient.
(define (lift-toplevel e)
(if (atom? e) (cons e '())
(let* ((rec (map lift-toplevel e))
(e2 (map car rec))
(tl (apply append (map cdr rec))))
(if (eq? (car e) 'toplevel-butlast)
(cons (last e2) (append tl (butlast (cdr e2))))
(cons e2 tl)))))
(let ((top '()))
(define (lift- e)
(if (or (atom? e) (quoted? e))
e
(let ((e (cons (car e) (map lift- (cdr e)))))
(if (eq? (car e) 'toplevel-butfirst)
(begin (set! top (cons (cddr e) top))
(cadr e))
e))))
(let ((e2 (lift- e)))
(cons e2 (apply append (reverse top))))))

(define (first-non-meta blk)
(let loop ((xs (cdr blk)))
Expand Down Expand Up @@ -3200,11 +3203,12 @@ f(x) = yt(x)
(let* ((exprs (lift-toplevel (convert-lambda lam2 '|#anon| #t '())))
(top-stmts (cdr exprs))
(newlam (compact-and-renumber (linearize (car exprs)))))
`(toplevel-butlast
,@top-stmts
`(toplevel-butfirst
(block ,@sp-inits
(method ,name ,(cl-convert sig fname lam namemap toplevel interp)
,(julia-bq-macro newlam)))))))
,(julia-bq-macro newlam)))
,@top-stmts))))

;; local case - lift to a new type at top level
(let* ((exists (get namemap name #f))
(type-name (or exists
Expand Down Expand Up @@ -3302,16 +3306,16 @@ f(x) = yt(x)
type-name
`(call (core apply_type) ,type-name ,@P))
,@var-exprs))))
`(toplevel-butlast
`(toplevel-butfirst
,(if exists
'(null)
(convert-assignment name mk-closure fname lam interp))
,@(if exists
'()
(begin (and name (put! namemap name type-name))
typedef))
(begin (and name (put! namemap name type-name))
typedef))
,@sp-inits
,@mk-method
,(if exists
'(null)
(convert-assignment name mk-closure fname lam interp)))))))
,@mk-method)))))
((lambda) ;; happens inside (thunk ...) and generated function bodies
(for-each (lambda (vi) (vinfo:set-asgn! vi #t))
(list-tail (car (lam:vinfo e)) (length (lam:args e))))
Expand Down

2 comments on commit 748f008

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Executing the daily benchmark build, I will reply here when finished:

@nanosoldier runbenchmarks(ALL, isdaily = true)

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your benchmark job has completed - possible performance regressions were detected. A full report can be found here. cc @ararslan

Please sign in to comment.