optimizer: reduce (length '()) ==> 0

This pattern is common in the internal code of the keyword procedures
This commit is contained in:
Gustavo Massaccesi 2018-06-20 14:27:15 -03:00
parent 598a5baf6a
commit 6d775e5c5c
2 changed files with 10 additions and 0 deletions

View File

@ -152,6 +152,10 @@
'(if (pair? null) 89 88)) '(if (pair? null) 89 88))
(test-comp 89 (test-comp 89
'(if (list? null) 89 88)) '(if (list? null) 89 88))
(test-comp 0
'(length '()))
(test-comp 3
'(length '(1 2 3)))
(test-comp '(lambda (x) (if x 2 1)) (test-comp '(lambda (x) (if x 2 1))
'(lambda (x) (if (not x) 1 2))) '(lambda (x) (if (not x) 1 2)))

View File

@ -4520,6 +4520,12 @@ static Scheme_Object *finish_optimize_application2(Scheme_App2_Rec *app, Optimiz
} }
} }
if (IS_NAMED_PRIM(rator, "length")
&& SCHEME_LISTP(rand)) {
alt = scheme_make_integer(scheme_list_length(rand));
return replace_tail_inside(alt, inside, app->rand);
}
alt = try_reduce_predicate(rator, rand, info); alt = try_reduce_predicate(rator, rand, info);
if (alt) if (alt)
return replace_tail_inside(alt, inside, app->rand); return replace_tail_inside(alt, inside, app->rand);