From 5ff9977572e7f82572432388324c2d62e3ad78b8 Mon Sep 17 00:00:00 2001
From: winlin <winlin@vip.126.com>
Date: Wed, 24 Feb 2021 20:56:17 +0800
Subject: [PATCH] Perf: Improve fast find for pithy print

---
 trunk/src/app/srs_app_pithy_print.cpp | 11 +++++++++--
 trunk/src/app/srs_app_pithy_print.hpp |  1 +
 2 files changed, 10 insertions(+), 2 deletions(-)

diff --git a/trunk/src/app/srs_app_pithy_print.cpp b/trunk/src/app/srs_app_pithy_print.cpp
index f408d08c5..ebf70def8 100644
--- a/trunk/src/app/srs_app_pithy_print.cpp
+++ b/trunk/src/app/srs_app_pithy_print.cpp
@@ -171,6 +171,7 @@ static SrsStageManager* _srs_stages = new SrsStageManager();
 SrsPithyPrint::SrsPithyPrint(int _stage_id)
 {
     stage_id = _stage_id;
+    cache_ = NULL;
     client_id = enter_stage();
     previous_tick = srs_get_system_time();
     _age = 0;
@@ -308,7 +309,10 @@ void SrsPithyPrint::leave_stage()
 
 void SrsPithyPrint::elapse()
 {
-    SrsStageInfo* stage = _srs_stages->fetch_or_create(stage_id);
+    SrsStageInfo* stage = cache_;
+    if (!stage) {
+        stage = cache_ = _srs_stages->fetch_or_create(stage_id);
+    }
     srs_assert(stage != NULL);
     
     srs_utime_t diff = srs_get_system_time() - previous_tick;
@@ -321,7 +325,10 @@ void SrsPithyPrint::elapse()
 
 bool SrsPithyPrint::can_print()
 {
-    SrsStageInfo* stage = _srs_stages->fetch_or_create(stage_id);
+    SrsStageInfo* stage = cache_;
+    if (!stage) {
+        stage = cache_ = _srs_stages->fetch_or_create(stage_id);
+    }
     srs_assert(stage != NULL);
     
     return stage->can_print();
diff --git a/trunk/src/app/srs_app_pithy_print.hpp b/trunk/src/app/srs_app_pithy_print.hpp
index 0fa3b6da2..bc966db98 100644
--- a/trunk/src/app/srs_app_pithy_print.hpp
+++ b/trunk/src/app/srs_app_pithy_print.hpp
@@ -111,6 +111,7 @@ class SrsPithyPrint
 {
 private:
     int client_id;
+    SrsStageInfo* cache_;
     int stage_id;
     srs_utime_t _age;
     srs_utime_t previous_tick;