Merge branch 'dump_rta'
authorJean Privat <jean@pryen.org>
Thu, 13 Mar 2014 12:37:28 +0000 (08:37 -0400)
committerJean Privat <jean@pryen.org>
Thu, 13 Mar 2014 12:37:28 +0000 (08:37 -0400)
lib/standard/string.nit
src/metrics/rta_metrics.nit
src/model_viz.nit
src/rapid_type_analysis.nit

index e1505df..e92fea8 100644 (file)
@@ -1315,3 +1315,47 @@ redef class Sys
        private fun native_argv(i: Int): NativeString is intern
 end
 
+# Comparator that efficienlty use `to_s` to compare things
+#
+# The comparaison call `to_s` on object and use the result to order things.
+#
+#     var a = [1, 2, 3, 10, 20]
+#     (new CachedAlphaComparator).sort(a)
+#     assert a == [1, 10, 2, 20, 3]
+#
+# Internally the result of `to_s` is cached in a HashMap to counter
+# uneficient implementation of `to_s`.
+#
+# Note: it caching is not usefull, see `alpha_comparator`
+class CachedAlphaComparator
+       super AbstractSorter[Object]
+
+       private var cache = new HashMap[Object, String]
+
+       private fun do_to_s(a: Object): String do
+               if cache.has_key(a) then return cache[a]
+               var res = a.to_s
+               cache[a] = res
+               return res
+       end
+
+       redef fun compare(a, b) do
+               return do_to_s(a) <=> do_to_s(b)
+       end
+end
+
+# see `alpha_comparator`
+private class AlphaComparator
+       super AbstractSorter[Object]
+       redef fun compare(a, b) do return a.to_s <=> b.to_s
+end
+
+# Stateless comparator that naively use `to_s` to compare things.
+#
+# Note: the result of `to_s` is not cached, thus can be invoked a lot
+# on a single instace. See `CachedAlphaComparator` as an alternative.
+#
+#     var a = [1, 2, 3, 10, 20]
+#     alpha_comparator.sort(a)
+#     assert a == [1, 10, 2, 20, 3]
+fun alpha_comparator: AbstractSorter[Object] do return once new AlphaComparator
index 7feb836..d274207 100644 (file)
@@ -79,6 +79,15 @@ private class RTAMetricsPhase
                gmetrics.collect(mtypes)
                gmetrics.to_console(1, not toolcontext.opt_nocolors.value)
                if csv then gmetrics.to_csv.save("{out}/complexity.csv")
+
+               # dump type and method infos
+               if csv then
+                       analysis.live_types_to_csv.save("{out}/rta_types.csv")
+
+                       var s = new OFStream.open("{out}/rta_methods.dat")
+                       analysis.live_methods_to_tree.pretty(s)
+                       s.close
+               end
        end
 end
 
index 6b6db4c..ba851e8 100644 (file)
@@ -39,8 +39,6 @@ class MProjectTree
                end
        end
 
-       var alpha_comparator = new AlphaComparator
-
        var linex_comparator: nullable LinexComparator = null
 
        # Sort modules and groups with their names
@@ -61,12 +59,6 @@ class MProjectTree
        end
 end
 
-# Just compare objects by using the `to_s` method
-private class AlphaComparator
-       super AbstractSorter[Object]
-       redef fun compare(a,b) do return a.to_s <=> b.to_s
-end
-
 # Compare modules and groups using the
 # FIXME do not use Object, but a better common interface of MModule and MGroup
 private class LinexComparator
index 50e4563..ff72fac 100644 (file)
@@ -28,6 +28,9 @@ import modelbuilder
 import typing
 import auto_super_init
 
+import csv # for live_types_to_csv
+import ordered_tree # for live_methods_to_tree
+
 redef class ModelBuilder
        fun do_rapid_type_analysis(mainmodule: MModule): RapidTypeAnalysis
        do
@@ -75,6 +78,57 @@ class RapidTypeAnalysis
        # Live call-to-super.
        var live_super_sends = new HashSet[MMethodDef]
 
+       # Return a ready-to-save CSV document objet that agregates informations about live types.
+       # Each discovered type is listed in a line, with its status: resolution, liveness, cast-liveness.
+       # Note: types are listed in an alphanumeric order to improve human reading.
+       fun live_types_to_csv: CSVDocument
+       do
+               # Gather all kind of type
+               var typeset = new HashSet[MType]
+               typeset.add_all(live_types)
+               typeset.add_all(live_open_types)
+               typeset.add_all(live_cast_types)
+               typeset.add_all(live_open_cast_types)
+               var types = typeset.to_a
+               (new CachedAlphaComparator).sort(types)
+               var res = new CSVDocument
+               res.header = ["Type", "Resolution", "Liveness", "Cast-liveness"]
+               for t in types do
+                       var reso
+                       if t.need_anchor then reso = "OPEN " else reso = "CLOSED"
+                       var live
+                       if t isa MClassType and (live_types.has(t) or live_open_types.has(t)) then live = "LIVE" else live = "DEAD"
+                       var cast
+                       if live_cast_types.has(t) or live_open_cast_types.has(t) then cast = "CAST LIVE" else cast = "CAST DEAD"
+                       res.add_line(t, reso, live, cast)
+               end
+               return res
+       end
+
+       # Return a ready-to-save OrderedTree object that agregates infomration about live methods.
+       # Note: methods are listed in an alphanumeric order to improve human reading.
+       fun live_methods_to_tree: OrderedTree[Object]
+       do
+               var tree = new OrderedTree[Object]
+               for x in live_methods do
+                       var xn = x.full_name
+                       tree.add(null, xn)
+                       for z in x.mpropdefs do
+                               var zn = z.to_s
+                               if live_methoddefs.has(z) then
+                                       tree.add(xn, zn)
+                                       if live_super_sends.has(z) then
+                                               tree.add(zn, zn + "(super)")
+                                       end
+                               else if live_super_sends.has(z) then
+                                       tree.add(xn, zn + "(super)")
+                               end
+                       end
+               end
+               tree.sort_with(alpha_comparator)
+               return tree
+       end
+
        # Methods that are are still candidate to the try_send
        private var totry_methods = new HashSet[MMethod]