From f31a8c8352472db155d2f4e7acaca641a77643b4 Mon Sep 17 00:00:00 2001
From: Thomas Steur <thomas.steur@googlemail.com>
Date: Fri, 27 Jun 2014 06:54:03 +0200
Subject: [PATCH] a view more performance improvements, especially to reduce
 Piwik::translate() calls

---
 core/Cache/StaticCache.php      |  6 +---
 core/Metrics.php                | 22 +++++++++++++-
 core/Plugin/Report.php          | 52 ++++++++++++++-------------------
 plugins/API/ProcessedReport.php |  3 --
 4 files changed, 44 insertions(+), 39 deletions(-)

diff --git a/core/Cache/StaticCache.php b/core/Cache/StaticCache.php
index a5684199f4..90b5b619cd 100644
--- a/core/Cache/StaticCache.php
+++ b/core/Cache/StaticCache.php
@@ -35,11 +35,7 @@ class StaticCache
 
     public function get()
     {
-        if (self::has()) {
-            return self::$staticCache[$this->cacheKey];
-        }
-
-        return null;
+        return self::$staticCache[$this->cacheKey];
     }
 
     public function has()
diff --git a/core/Metrics.php b/core/Metrics.php
index 459708b152..0d9bc2c16e 100644
--- a/core/Metrics.php
+++ b/core/Metrics.php
@@ -9,6 +9,7 @@
 namespace Piwik;
 
 
+use Piwik\Cache\LanguageAwareStaticCache;
 use Piwik\Cache\PluginAwareStaticCache;
 
 require_once PIWIK_INCLUDE_PATH . "/core/Piwik.php";
@@ -279,17 +280,32 @@ class Metrics
 
     static public function getDefaultMetrics()
     {
+        $cache = new LanguageAwareStaticCache('DefaultMetrics');
+
+        if ($cache->has()) {
+            return $cache->get();
+        }
+
         $translations = array(
             'nb_visits'        => 'General_ColumnNbVisits',
             'nb_uniq_visitors' => 'General_ColumnNbUniqVisitors',
             'nb_actions'       => 'General_ColumnNbActions',
         );
         $translations = array_map(array('\\Piwik\\Piwik','translate'), $translations);
+
+        $cache->set($translations);
+
         return $translations;
     }
 
     static public function getDefaultProcessedMetrics()
     {
+        $cache = new LanguageAwareStaticCache('DefaultProcessedMetrics');
+
+        if ($cache->has()) {
+            return $cache->get();
+        }
+
         $translations = array(
             // Processed in AddColumnsProcessedMetrics
             'nb_actions_per_visit' => 'General_ColumnActionsPerVisit',
@@ -297,7 +313,11 @@ class Metrics
             'bounce_rate'          => 'General_ColumnBounceRate',
             'conversion_rate'      => 'General_ColumnConversionRate',
         );
-        return array_map(array('\\Piwik\\Piwik','translate'), $translations);
+        $translations = array_map(array('\\Piwik\\Piwik','translate'), $translations);
+
+        $cache->set($translations);
+
+        return $translations;
     }
 
     static public function getReadableColumnName($columnIdRaw)
diff --git a/core/Plugin/Report.php b/core/Plugin/Report.php
index 61b7436873..b2a42317db 100644
--- a/core/Plugin/Report.php
+++ b/core/Plugin/Report.php
@@ -32,12 +32,26 @@ class Report
     protected $widgetTitle;
     protected $widgetParams = array();
     protected $menuTitle;
-    protected $processedMetrics = array();
+    protected $processedMetrics = null;
     protected $hasGoalMetrics = false;
     protected $metrics = array();
     protected $constantRowsCount = null;
     protected $isSubtableReport = null;
     protected $parameters = null;
+    
+    private static $orderOfReports = array(
+        'General_MultiSitesSummary',
+        'VisitsSummary_VisitsSummary',
+        'Goals_Ecommerce',
+        'General_Actions',
+        'Events_Events',
+        'Actions_SubmenuSitesearch',
+        'Referrers_Referrers',
+        'Goals_Goals',
+        'General_Visitors',
+        'DevicesDetection_DevicesDetection',
+        'UserSettings_VisitorSettings',
+    );
 
     /**
      * @var \Piwik\Plugin\VisitDimension|\Piwik\Plugin\ActionDimension
@@ -58,7 +72,7 @@ class Report
         $this->module = $parts[2];
         $this->action = lcfirst($parts[4]);
         $this->processedMetrics = Metrics::getDefaultProcessedMetrics();
-        $this->metrics          = array_keys(Metrics::getDefaultMetrics());
+        $this->metrics = array_keys(Metrics::getDefaultMetrics());
 
         $this->init();
     }
@@ -196,7 +210,8 @@ class Report
 
         $report['metrics']              = $this->getMetrics();
         $report['metricsDocumentation'] = $this->getMetricsDocumentation();
-        $report['processedMetrics']     = $this->processedMetrics;
+
+        $report['processedMetrics'] = $this->processedMetrics;
 
         if (!empty($this->actionToLoadSubTables)) {
             $report['actionToLoadSubTables'] = $this->actionToLoadSubTables;
@@ -297,37 +312,14 @@ class Report
      * API metadata are sorted by category/name,
      * with a little tweak to replicate the standard Piwik category ordering
      *
-     * @param array|Report $a
-     * @param array|Report $b
+     * @param Report $a
+     * @param Report $b
      * @return int
      */
     public static function sort($a, $b)
     {
-        static $order = null;
-        if (is_null($order)) {
-            $order = array(
-                Piwik::translate('General_MultiSitesSummary'),
-                Piwik::translate('VisitsSummary_VisitsSummary'),
-                Piwik::translate('Goals_Ecommerce'),
-                Piwik::translate('General_Actions'),
-                Piwik::translate('Events_Events'),
-                Piwik::translate('Actions_SubmenuSitesearch'),
-                Piwik::translate('Referrers_Referrers'),
-                Piwik::translate('Goals_Goals'),
-                Piwik::translate('General_Visitors'),
-                Piwik::translate('DevicesDetection_DevicesDetection'),
-                Piwik::translate('UserSettings_VisitorSettings'),
-            );
-        }
-
-        $catA = is_object($a) ? $a->category : $a['category'];
-        $catB = is_object($b) ? $b->category : $b['category'];
-
-        $orderA = is_object($a) ? $a->order : @$a['order'];
-        $orderB = is_object($b) ? $b->order : @$b['order'];
-
-        return ($category = strcmp(array_search($catA, $order), array_search($catB, $order))) == 0
-            ? ($orderA < $orderB ? -1 : 1)
+        return ($category = strcmp(array_search($a->category, self::$orderOfReports), array_search($b->category, self::$orderOfReports))) == 0
+            ? ($a->order < $b->order ? -1 : 1)
             : $category;
     }
 
diff --git a/plugins/API/ProcessedReport.php b/plugins/API/ProcessedReport.php
index 0a5e4a81c3..05a1a960ee 100644
--- a/plugins/API/ProcessedReport.php
+++ b/plugins/API/ProcessedReport.php
@@ -245,9 +245,6 @@ class ProcessedReport
          */
         Piwik::postEvent('API.getReportMetadata.end', array(&$availableReports, $parameters));
 
-        // Sort results to ensure consistent order
-        usort($availableReports, array('\Piwik\Plugin\Report', 'sort'));
-
         // Add the magic API.get report metadata aggregating all plugins API.get API calls automatically
         $this->addApiGetMetdata($availableReports);
 
-- 
GitLab