From 19eac53f0e6271e64981222732237448bd7c478d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ezgi=20=C3=87i=C3=A7ek?= Date: Thu, 6 Jun 2019 05:46:52 -0700 Subject: [PATCH] [cost] Add models for Collections.max/min Reviewed By: ngorogiannis Differential Revision: D15673503 fbshipit-source-id: 5450ec63f --- infer/src/checkers/costModels.ml | 6 ++++++ .../tests/codetoanalyze/java/performance/ArrayListTest.java | 4 ++++ infer/tests/codetoanalyze/java/performance/issues.exp | 1 + 3 files changed, 11 insertions(+) diff --git a/infer/src/checkers/costModels.ml b/infer/src/checkers/costModels.ml index d0cd45d94..eca24e28a 100644 --- a/infer/src/checkers/costModels.ml +++ b/infer/src/checkers/costModels.ml @@ -121,6 +121,12 @@ module Call = struct ; +PatternMatch.implements_collections &:: "reverse" <>$ capt_exp $+...$--> BoundsOfCollection.linear_length ~of_function:"Collections.reverse" + ; +PatternMatch.implements_collections + &:: "max" <>$ capt_exp + $+...$--> BoundsOfCollection.linear_length ~of_function:"Collections.max" + ; +PatternMatch.implements_collections + &:: "min" <>$ capt_exp + $+...$--> BoundsOfCollection.linear_length ~of_function:"Collections.min" ; +PatternMatch.implements_collections &:: "shuffle" <>$ capt_exp $+...$--> BoundsOfCollection.linear_length ~of_function:"Collections.shuffle" diff --git a/infer/tests/codetoanalyze/java/performance/ArrayListTest.java b/infer/tests/codetoanalyze/java/performance/ArrayListTest.java index 775ead2f1..f641da622 100644 --- a/infer/tests/codetoanalyze/java/performance/ArrayListTest.java +++ b/infer/tests/codetoanalyze/java/performance/ArrayListTest.java @@ -255,6 +255,10 @@ public class ArrayListTest { void sort_comparator_nlogn(ArrayList people) { java.util.Collections.sort(people, new LexicographicComparator()); } + + Person max_linear(ArrayList people) { + return java.util.Collections.max(people, new LexicographicComparator()); + } } class LexicographicComparator implements java.util.Comparator { diff --git a/infer/tests/codetoanalyze/java/performance/issues.exp b/infer/tests/codetoanalyze/java/performance/issues.exp index b6d9372be..38648f6a2 100644 --- a/infer/tests/codetoanalyze/java/performance/issues.exp +++ b/infer/tests/codetoanalyze/java/performance/issues.exp @@ -37,6 +37,7 @@ codetoanalyze/java/performance/ArrayListTest.java, ArrayListTest.iterate_over_ar codetoanalyze/java/performance/ArrayListTest.java, ArrayListTest.iterate_over_local_arraylist(java.util.ArrayList):void, 2, EXPENSIVE_EXECUTION_CALL, no_bucket, ERROR, [with estimated cost 7 + 5 ⋅ list.length, degree = 1,{list.length},Loop at line 19] codetoanalyze/java/performance/ArrayListTest.java, ArrayListTest.iterate_while_has_next(java.util.ArrayList):void, 3, EXPENSIVE_EXECUTION_CALL, no_bucket, ERROR, [with estimated cost 4 + 10 ⋅ (list.length - 1) + 3 ⋅ list.length, degree = 1,{list.length},Loop at line 176,{list.length - 1},Loop at line 176] codetoanalyze/java/performance/ArrayListTest.java, ArrayListTest.iterate_with_iterator(java.util.ArrayList):void, 1, EXPENSIVE_EXECUTION_CALL, no_bucket, ERROR, [with estimated cost 4 + 8 ⋅ (list.length - 1) + 3 ⋅ list.length, degree = 1,{list.length},Loop at line 170,{list.length - 1},Loop at line 170] +codetoanalyze/java/performance/ArrayListTest.java, ArrayListTest.max_linear(java.util.ArrayList):Person, 1, EXPENSIVE_EXECUTION_CALL, no_bucket, ERROR, [with estimated cost 8 + people.length, degree = 1,{people.length},Modeled call to Collections.max] codetoanalyze/java/performance/ArrayListTest.java, ArrayListTest.remove_string_from_list(java.lang.String):boolean, 2, EXPENSIVE_EXECUTION_CALL, no_bucket, ERROR, [with estimated cost 5 + 12 ⋅ (this.list.length - 1) + 3 ⋅ this.list.length, degree = 1,{this.list.length},Loop at line 216,{this.list.length - 1},Loop at line 216] codetoanalyze/java/performance/ArrayListTest.java, ArrayListTest.sortArrayList(java.util.ArrayList):void, 1, EXPENSIVE_EXECUTION_CALL, no_bucket, ERROR, [with estimated cost 2 + list.length × log(list.length), degree = 1 + 1⋅log,{list.length},Modeled call to Collections.sort,{list.length},Modeled call to Collections.sort] codetoanalyze/java/performance/ArrayListTest.java, ArrayListTest.sort_comparator_nlogn(java.util.ArrayList):void, 1, EXPENSIVE_EXECUTION_CALL, no_bucket, ERROR, [with estimated cost 8 + people.length × log(people.length), degree = 1 + 1⋅log,{people.length},Modeled call to Collections.sort,{people.length},Modeled call to Collections.sort]