From 254dac462593c0ea7a3d7bc7734688ab1356a90b Mon Sep 17 00:00:00 2001 From: Matthew Flatt Date: Thu, 3 Mar 2016 08:00:32 -0700 Subject: [PATCH] optimizer: drop redundant `with-continuation-mark`s Simplify `(wcm (wcm ))` to `(begin (wcm ))` for a simple enough . A variable simple enough, so this is useful for improving errortrace output. --- .../tests/racket/optimize.rktl | 43 +++++++++++++++++++ racket/src/racket/src/optimize.c | 15 +++++++ 2 files changed, 58 insertions(+) diff --git a/pkgs/racket-test-core/tests/racket/optimize.rktl b/pkgs/racket-test-core/tests/racket/optimize.rktl index 8fd5e3a131..8c60aa1ec6 100644 --- a/pkgs/racket-test-core/tests/racket/optimize.rktl +++ b/pkgs/racket-test-core/tests/racket/optimize.rktl @@ -3612,6 +3612,49 @@ `(lambda () (with-continuation-mark 'x (values 1 2) (list 1))) #f) +(test-comp `(lambda (x) + (with-continuation-mark + x 1 + (with-continuation-mark + x 2 + (x)))) + `(lambda (x) + (with-continuation-mark + x 2 + (x)))) +(test-comp `(lambda (x) + (with-continuation-mark + x (display x) + (with-continuation-mark + x 2 + (x)))) + `(lambda (x) + (display x) + (with-continuation-mark + x 2 + (x)))) +(test-comp `(lambda (x) + (with-continuation-mark + x 1 + (with-continuation-mark + x (current-continuation-marks) + (x)))) + `(lambda (x) + (with-continuation-mark + x (current-continuation-marks) + (x))) + #f) +(test-comp '(lambda (v) + (let ([x (with-continuation-mark + 'x 10 + (+ v v))]) + x)) + '(lambda (v) + (begin0 + (with-continuation-mark + 'x 10 + (+ v v)) + #f))) (test-comp `(lambda (x y f) (set! x 5) diff --git a/racket/src/racket/src/optimize.c b/racket/src/racket/src/optimize.c index 84d1af36ee..5d3e26dd51 100644 --- a/racket/src/racket/src/optimize.c +++ b/racket/src/racket/src/optimize.c @@ -4721,6 +4721,8 @@ static Scheme_Object *optimize_wcm(Scheme_Object *o, Optimize_Info *info, int co optimize_info_seq_done(info, &info_seq); + /* If the body cannot inspect the continution, and if the key is not + a chaperone, no need to add the mark: */ if (omittable_key(k, info) && scheme_omittable_expr(b, -1, 20, 0, info, info)) return make_discarding_first_sequence(v, b, info); @@ -4734,6 +4736,19 @@ static Scheme_Object *optimize_wcm(Scheme_Object *o, Optimize_Info *info, int co info->size += 1; + /* Simplify (with-continuation-mark + (with-continuation-mark + )) + to (begin + + (with-continuation-mark + )) + as long as doesn't inspect the continuation. */ + if (SAME_TYPE(SCHEME_TYPE(wcm->body), scheme_with_cont_mark_type) + && equivalent_exprs(wcm->key, ((Scheme_With_Continuation_Mark *)wcm->body)->key, NULL, NULL, 0) + && scheme_omittable_expr(((Scheme_With_Continuation_Mark *)wcm->body)->val, 1, 20, 0, info, info)) + return make_discarding_first_sequence(wcm->val, wcm->body, info); + return (Scheme_Object *)wcm; }