From a8026824ddc0bab1aee500df64d9f82751a88d1f Mon Sep 17 00:00:00 2001 From: Matthew Flatt Date: Mon, 16 Feb 2015 10:01:16 -0700 Subject: [PATCH] adjust optimizer to improve intra-module inlining Instead of delaying the registration of some constants until a group of expressions is re-optimized, add constant information as it is discovered, which can expose some additional optimizations. The old grouping was probably aimed at avoiding excessive code growth, but I think that other and better controls are now in place. The overall size of ".zo" files in an installation did not grow significantly with this change. Closes PR 14978 --- .../tests/racket/optimize.rktl | 15 ++++++ racket/src/racket/src/optimize.c | 49 ++++++++----------- 2 files changed, 35 insertions(+), 29 deletions(-) diff --git a/pkgs/racket-test-core/tests/racket/optimize.rktl b/pkgs/racket-test-core/tests/racket/optimize.rktl index 1265d79e03..7de6bcfc5b 100644 --- a/pkgs/racket-test-core/tests/racket/optimize.rktl +++ b/pkgs/racket-test-core/tests/racket/optimize.rktl @@ -1494,6 +1494,21 @@ (set! x x)))) #f) +(test-comp '(module m racket/base + (define (true) #t) + (define no (if (true) (lambda (x) (cons x 'no)) display)) + (newline) ; <--- just to break the simultaneous group + (define yes (if (true) (lambda (x) (cons x 'yes)) display)) + (no 5) + (yes 5)) + '(module m racket/base + (define (true) #t) + (define no (lambda (x) (cons x 'no))) + (newline) + (define yes (lambda (x) (cons x 'yes))) + (cons 5 'no) + (cons 5 'yes))) + (test-comp '(let ([x 1][y 2]) x) '1) (test-comp '(let ([x 1][y 2]) (+ y x)) diff --git a/racket/src/racket/src/optimize.c b/racket/src/racket/src/optimize.c index a35f44bcc8..787f8156e3 100644 --- a/racket/src/racket/src/optimize.c +++ b/racket/src/racket/src/optimize.c @@ -6874,19 +6874,17 @@ module_optimize(Scheme_Object *data, Optimize_Info *info, int context) if (e2) { int pos; pos = tl->position; - if (sstruct) { - /* Add directly to `info->top_level_consts' for use - by sub-struct declarations in the same set */ - if (!info->top_level_consts) { - Scheme_Hash_Table *tlc; - tlc = scheme_make_hash_table(SCHEME_hash_ptr); - info->top_level_consts = tlc; - } - scheme_hash_set(info->top_level_consts, scheme_make_integer(pos), e2); + + consts = info->top_level_consts; + if (!consts) { + consts = scheme_make_hash_table(SCHEME_hash_ptr); + info->top_level_consts = consts; + } + scheme_hash_set(consts, scheme_make_integer(pos), e2); + + if (sstruct || (SCHEME_TYPE(e2) > _scheme_compiled_values_types_)) { + /* No use re-optimizing */ } else { - if (!consts) - consts = scheme_make_hash_table(SCHEME_hash_ptr); - scheme_hash_set(consts, scheme_make_integer(pos), e2); if (!re_consts) re_consts = scheme_make_hash_table(SCHEME_hash_ptr); scheme_hash_set(re_consts, scheme_make_integer(i_m), @@ -6896,9 +6894,12 @@ module_optimize(Scheme_Object *data, Optimize_Info *info, int context) /* At least mark it as fixed */ if (!fixed_table) { fixed_table = scheme_make_hash_table(SCHEME_hash_ptr); - if (!consts) + if (!info->top_level_consts) { consts = scheme_make_hash_table(SCHEME_hash_ptr); - scheme_hash_set(consts, scheme_false, (Scheme_Object *)fixed_table); + info->top_level_consts = consts; + consts = NULL; + } + scheme_hash_set(info->top_level_consts, scheme_false, (Scheme_Object *)fixed_table); } scheme_hash_set(fixed_table, scheme_make_integer(tl->position), scheme_true); } @@ -6936,19 +6937,6 @@ module_optimize(Scheme_Object *data, Optimize_Info *info, int context) if (consts) { int flags; - if (!info->top_level_consts) { - info->top_level_consts = consts; - } else { - int i; - for (i = 0; i < consts->size; i++) { - if (consts->vals[i]) { - scheme_hash_set(info->top_level_consts, - consts->keys[i], - consts->vals[i]); - } - } - } - /* Same as in letrec: assume CLOS_SINGLE_RESULT and CLOS_PRESERVES_MARKS for all, but then assume not for all if any turn out not (i.e., approximate fix point). */ @@ -7049,9 +7037,12 @@ module_optimize(Scheme_Object *data, Optimize_Info *info, int context) if (next_pos_ready > -1) { if (!fixed_table) { fixed_table = scheme_make_hash_table(SCHEME_hash_ptr); - if (!consts) + if (!info->top_level_consts) { consts = scheme_make_hash_table(SCHEME_hash_ptr); - scheme_hash_set(consts, scheme_false, (Scheme_Object *)fixed_table); + info->top_level_consts = consts; + consts = NULL; + } + scheme_hash_set(info->top_level_consts, scheme_false, (Scheme_Object *)fixed_table); } scheme_hash_set(fixed_table, scheme_make_integer(next_pos_ready), scheme_true); next_pos_ready = -1;