From b320714edd9a49c15a17be71cb00aab8d1f7e14e Mon Sep 17 00:00:00 2001 From: Sam Blackshear Date: Thu, 17 Nov 2016 13:41:01 -0800 Subject: [PATCH] [quandary] optimizing Quandary compare functions Reviewed By: jeremydubreil Differential Revision: D4179159 fbshipit-source-id: 2bae286 --- infer/src/IR/CallSite.ml | 11 +++++++---- infer/src/checkers/Trace.ml | 15 +++++++++++--- infer/src/checkers/accessPath.ml | 34 +++++++++++++++++++++----------- infer/src/quandary/CppTrace.ml | 31 ++++++++++++++++++++++------- infer/src/quandary/JavaTrace.ml | 20 +++++++++++++++---- 5 files changed, 82 insertions(+), 29 deletions(-) diff --git a/infer/src/IR/CallSite.ml b/infer/src/IR/CallSite.ml index 0d19144ce..3893db8e2 100644 --- a/infer/src/IR/CallSite.ml +++ b/infer/src/IR/CallSite.ml @@ -16,10 +16,13 @@ type t = } let compare t1 t2 = - let n = Procname.compare t1.pname t2.pname in - if n <> 0 - then n - else Location.compare t1.loc t2.loc + if t1 == t2 + then 0 + else + let n = Procname.compare t1.pname t2.pname in + if n <> 0 + then n + else Location.compare t1.loc t2.loc let equal t1 t2 = compare t1 t2 = 0 diff --git a/infer/src/checkers/Trace.ml b/infer/src/checkers/Trace.ml index 9c6416159..2bd89e5af 100644 --- a/infer/src/checkers/Trace.ml +++ b/infer/src/checkers/Trace.ml @@ -138,9 +138,18 @@ module Make (Spec : Spec) = struct type path = Passthroughs.t * (Source.t * Passthroughs.t) list * (Sink.t * Passthroughs.t) list let compare t1 t2 = - Sources.compare t1.sources t2.sources - |> next Sinks.compare t1.sinks t2.sinks - |> next Passthroughs.compare t1.passthroughs t2.passthroughs + if t1 == t2 + then + 0 + else + let n = Sources.compare t1.sources t2.sources in + if n <> 0 + then n + else + let n = Sinks.compare t1.sinks t2.sinks in + if n <> 0 + then n + else Passthroughs.compare t1.passthroughs t2.passthroughs let equal t1 t2 = compare t1 t2 = 0 diff --git a/infer/src/checkers/accessPath.ml b/infer/src/checkers/accessPath.ml index f3542a0ff..261ce58a9 100644 --- a/infer/src/checkers/accessPath.ml +++ b/infer/src/checkers/accessPath.ml @@ -27,20 +27,25 @@ let base_compare ((var1, typ1) as base1) ((var2, typ2) as base2) = if base1 == base2 then 0 else - Var.compare var1 var2 - |> next Typ.array_sensitive_compare typ1 typ2 + let n = Var.compare var1 var2 in + if n <> 0 + then n + else Typ.array_sensitive_compare typ1 typ2 let base_equal base1 base2 = base_compare base1 base2 = 0 let access_compare access1 access2 = if access1 == access2 - then 0 + then + 0 else match access1, access2 with | FieldAccess (f1, typ1), FieldAccess (f2, typ2) -> - Ident.fieldname_compare f1 f2 - |> next Typ.compare typ1 typ2 + let n = Ident.fieldname_compare f1 f2 in + if n <> 0 + then n + else Typ.compare typ1 typ2 | ArrayAccess typ1, ArrayAccess typ2 -> Typ.compare typ1 typ2 | FieldAccess _, _ -> 1 @@ -53,16 +58,23 @@ let raw_compare ((base1, accesses1) as ap1) ((base2, accesses2) as ap2) = if ap1 == ap2 then 0 else - base_compare base1 base2 - |> next (IList.compare access_compare) accesses1 accesses2 + let n = base_compare base1 base2 in + if n <> 0 + then n + else (IList.compare access_compare) accesses1 accesses2 let raw_equal ap1 ap2 = raw_compare ap1 ap2 = 0 -let compare ap1 ap2 = match ap1, ap2 with - | Exact ap1, Exact ap2 | Abstracted ap1, Abstracted ap2 -> raw_compare ap1 ap2 - | Exact _, Abstracted _ -> 1 - | Abstracted _, Exact _ -> (-1) +let compare ap1 ap2 = + if ap1 == ap2 + then + 0 + else + match ap1, ap2 with + | Exact ap1, Exact ap2 | Abstracted ap1, Abstracted ap2 -> raw_compare ap1 ap2 + | Exact _, Abstracted _ -> 1 + | Abstracted _, Exact _ -> (-1) let equal ap1 ap2 = compare ap1 ap2 = 0 diff --git a/infer/src/quandary/CppTrace.ml b/infer/src/quandary/CppTrace.ml index dee0e8ea3..eee0908fc 100644 --- a/infer/src/quandary/CppTrace.ml +++ b/infer/src/quandary/CppTrace.ml @@ -20,9 +20,14 @@ module CppSource = struct | EnvironmentVariable (** source that was read from an environment variable *) | Other (** for testing or uncategorized sources *) - let compare sk1 sk2 = match sk1, sk2 with - | Footprint ap1, Footprint ap2 -> AccessPath.compare ap1 ap2 - | _ -> tags_compare sk1 sk2 + let compare sk1 sk2 = + if sk1 == sk2 + then + 0 + else + match sk1, sk2 with + | Footprint ap1, Footprint ap2 -> AccessPath.compare ap1 ap2 + | _ -> tags_compare sk1 sk2 let equal sk1 sk2 = compare sk1 sk2 = 0 @@ -83,8 +88,14 @@ module CppSource = struct { t with site = callee_site; } let compare src1 src2 = - Kind.compare src1.kind src2.kind - |> next CallSite.compare src1.site src2.site + if src1 == src2 + then + 0 + else + let n = Kind.compare src1.kind src2.kind in + if n <> 0 + then n + else CallSite.compare src1.site src2.site let equal t1 t2 = compare t1 t2 = 0 @@ -163,8 +174,14 @@ module CppSink = struct { t with site = callee_site; } let compare snk1 snk2 = - Kind.compare snk1.kind snk2.kind - |> next CallSite.compare snk1.site snk2.site + if snk1 == snk2 + then + 0 + else + let n = Kind.compare snk1.kind snk2.kind in + if n <> 0 + then n + else CallSite.compare snk1.site snk2.site let equal t1 t2 = compare t1 t2 = 0 diff --git a/infer/src/quandary/JavaTrace.ml b/infer/src/quandary/JavaTrace.ml index ea0552db3..f23e700e5 100644 --- a/infer/src/quandary/JavaTrace.ml +++ b/infer/src/quandary/JavaTrace.ml @@ -89,8 +89,14 @@ module JavaSource = struct { t with site = callee_site; } let compare src1 src2 = - Kind.compare src1.kind src2.kind - |> next CallSite.compare src1.site src2.site + if src1 == src2 + then + 0 + else + let n = Kind.compare src1.kind src2.kind in + if n <> 0 + then n + else CallSite.compare src1.site src2.site let pp fmt s = F.fprintf fmt "%a(%a)" Kind.pp s.kind CallSite.pp s.site @@ -185,8 +191,14 @@ module JavaSink = struct { t with site = callee_site; } let compare snk1 snk2 = - Kind.compare snk1.kind snk2.kind - |> next CallSite.compare snk1.site snk2.site + if snk1 == snk2 + then + 0 + else + let n = Kind.compare snk1.kind snk2.kind in + if n <> 0 + then n + else CallSite.compare snk1.site snk2.site let pp fmt s = F.fprintf fmt "%a(%a)" Kind.pp s.kind CallSite.pp s.site