From 0a517db2389b1bac5e93657f17b81ccd957dcf3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ezgi=20=C3=87i=C3=A7ek?= Date: Tue, 2 Feb 2021 05:46:15 -0800 Subject: [PATCH] [cost] Model Collection's contains Reviewed By: skcho Differential Revision: D26076519 fbshipit-source-id: f8dce867f --- infer/src/cost/costModels.ml | 3 +++ .../tests/codetoanalyze/java/performance/CollectionTest.java | 5 +++++ infer/tests/codetoanalyze/java/performance/cost-issues.exp | 1 + 3 files changed, 9 insertions(+) diff --git a/infer/src/cost/costModels.ml b/infer/src/cost/costModels.ml index 8134445ce..0fab779b0 100644 --- a/infer/src/cost/costModels.ml +++ b/infer/src/cost/costModels.ml @@ -279,6 +279,9 @@ module Call = struct ; +PatternMatch.Java.implements_list &:: "contains" <>$ capt_exp $+...$--> BoundsOfCollection.linear_length ~of_function:"List.contains" + ; +PatternMatch.Java.implements_collection + &:: "contains" <>$ capt_exp + $+...$--> BoundsOfCollection.linear_length ~of_function:"Collection.contains" ; +PatternMatch.Java.implements_collections &:: "binarySearch" <>$ capt_exp $+...$--> BoundsOfCollection.logarithmic_length ~of_function:"Collections.binarySearch" diff --git a/infer/tests/codetoanalyze/java/performance/CollectionTest.java b/infer/tests/codetoanalyze/java/performance/CollectionTest.java index 1e32a7d15..9f8bfc061 100644 --- a/infer/tests/codetoanalyze/java/performance/CollectionTest.java +++ b/infer/tests/codetoanalyze/java/performance/CollectionTest.java @@ -8,6 +8,7 @@ import android.util.SparseArray; import com.google.common.collect.ImmutableSet; import java.util.Collection; +import java.util.HashMap; import java.util.concurrent.ConcurrentLinkedQueue; public class CollectionTest { @@ -102,4 +103,8 @@ public class CollectionTest { ImmutableSet set = ImmutableSet.of(1, 2, 3, 4, 5); for (int i = 0; i < set.size(); i++) {} } + + void containsNull_linear(HashMap keyMap) { + keyMap.values().contains(null); + } } diff --git a/infer/tests/codetoanalyze/java/performance/cost-issues.exp b/infer/tests/codetoanalyze/java/performance/cost-issues.exp index ea096e805..94bf50b90 100644 --- a/infer/tests/codetoanalyze/java/performance/cost-issues.exp +++ b/infer/tests/codetoanalyze/java/performance/cost-issues.exp @@ -99,6 +99,7 @@ codetoanalyze/java/performance/CollectionTest.java, CollectionTest$MyEnumType.(), 2, OnUIThread:false, [] +codetoanalyze/java/performance/CollectionTest.java, CollectionTest.containsNull_linear(java.util.HashMap):void, 5 + keyMap.length, OnUIThread:false, [{keyMap.length},Modeled call to Collection.contains] codetoanalyze/java/performance/CollectionTest.java, CollectionTest.ensure_call(CollectionTest$MyCollection):void, 9 + 5 ⋅ list.length, OnUIThread:false, [{list.length},Call to void CollectionTest.iterate_over_mycollection(CollectionTest$MyCollection),Loop] codetoanalyze/java/performance/CollectionTest.java, CollectionTest.immutable_set_of_constant():void, 9, OnUIThread:false, [] codetoanalyze/java/performance/CollectionTest.java, CollectionTest.immutable_set_of_multiple_constant():void, 64, OnUIThread:false, []