Skip to content

Commit 5d22376

Browse files
Replace List.length comparisons with List.is_empty for O(1) checks
List.length traverses the entire list (O(n)) just to compare against zero. List.is_empty is a constant-time check and is already the established idiom elsewhere in the codebase. Changed in: - Warnings.ml: List.length warnings > 0 → not (List.is_empty warnings) - Optimize.ml: List.length l' = 0 → List.is_empty l' (3 occurrences)
1 parent 0121e19 commit 5d22376

2 files changed

Lines changed: 4 additions & 4 deletions

File tree

src/analysis_and_optimization/Optimize.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -799,10 +799,10 @@ let dead_code_elimination (mir : Program.Typed.t) =
799799
else For {loopvar; lower; upper; body}
800800
| Profile (name, l) ->
801801
let l' = List.filter ~f:(fun x -> x.pattern <> Skip) l in
802-
if List.length l' = 0 then Skip else Profile (name, l')
802+
if List.is_empty l' then Skip else Profile (name, l')
803803
| Block l ->
804804
let l' = List.filter ~f:(fun x -> x.pattern <> Skip) l in
805-
if List.length l' = 0 then Skip else Block l'
805+
if List.is_empty l' then Skip else Block l'
806806
| SList l ->
807807
let l' = List.filter ~f:(fun x -> x.pattern <> Skip) l in
808808
SList l' in
@@ -1023,7 +1023,7 @@ let lazy_code_motion ?(preserve_stability = false) (mir : Program.Typed.t) =
10231023
(Map.mapi expression_map ~f:(fun ~key ~data ->
10241024
{key with pattern= Var data})) in
10251025
let f = expr_subst_stmt_except_initial_assign expr_map in
1026-
if List.length assignments_to_add_to_s = 0 then
1026+
if List.is_empty assignments_to_add_to_s then
10271027
(f Stmt.{pattern= stmt; meta= Location_span.empty}).pattern
10281028
else
10291029
SList

src/frontend/Warnings.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ let pp ?printed_filename ppf (span, message) =
1313
Fmt.pf ppf "@[<v4>%a%a%a@]@]" purple "Warning" maybe_loc span Fmt.text message
1414

1515
let pp_warnings ?printed_filename ppf warnings =
16-
if List.length warnings > 0 then
16+
if not (List.is_empty warnings) then
1717
Fmt.(pf ppf "@[<v>%a@]@\n" (list ~sep:cut (pp ?printed_filename)) warnings)

0 commit comments

Comments
 (0)