Optimize the log entry search algorithm to save boot performance.
1. Search from the first entry can be changed to Search from the end entry, because most End just follows its Start.
2. Match Start and End entry, the first comparison can be changed from Handle to EndTimeStamp, because only zero EndTimeStamp is required to be matched.

Signed-off-by: Star Zeng <star.zeng@intel.com>
Reviewed-by: Liming Gao <liming.gao@intel.com>

git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@13367 6f19259b-4bc3-4df7-8a09-765794883524
diff --git a/MdeModulePkg/Library/DxeCorePerformanceLib/DxeCorePerformanceLib.c b/MdeModulePkg/Library/DxeCorePerformanceLib/DxeCorePerformanceLib.c
index 696aa66..90053e7 100644
--- a/MdeModulePkg/Library/DxeCorePerformanceLib/DxeCorePerformanceLib.c
+++ b/MdeModulePkg/Library/DxeCorePerformanceLib/DxeCorePerformanceLib.c
@@ -88,6 +88,7 @@
   )

 {

   UINT32                    Index;

+  UINT32                    Index2;

   UINT32                    NumberOfEntries;

   GAUGE_DATA_ENTRY_EX       *GaugeEntryExArray;

 

@@ -101,13 +102,16 @@
   NumberOfEntries = mGaugeData->NumberOfEntries;

   GaugeEntryExArray = (GAUGE_DATA_ENTRY_EX *) (mGaugeData + 1);

 

+  Index2 = 0;

+

   for (Index = 0; Index < NumberOfEntries; Index++) {

-    if ((GaugeEntryExArray[Index].Handle == (EFI_PHYSICAL_ADDRESS) (UINTN) Handle) &&

-         AsciiStrnCmp (GaugeEntryExArray[Index].Token, Token, DXE_PERFORMANCE_STRING_LENGTH) == 0 &&

-         AsciiStrnCmp (GaugeEntryExArray[Index].Module, Module, DXE_PERFORMANCE_STRING_LENGTH) == 0 &&

-         (GaugeEntryExArray[Index].Identifier == Identifier) &&

-         GaugeEntryExArray[Index].EndTimeStamp == 0

-       ) {

+    Index2 = NumberOfEntries - 1 - Index;

+    if (GaugeEntryExArray[Index2].EndTimeStamp == 0 &&

+        (GaugeEntryExArray[Index2].Handle == (EFI_PHYSICAL_ADDRESS) (UINTN) Handle) &&

+        AsciiStrnCmp (GaugeEntryExArray[Index2].Token, Token, DXE_PERFORMANCE_STRING_LENGTH) == 0 &&

+        AsciiStrnCmp (GaugeEntryExArray[Index2].Module, Module, DXE_PERFORMANCE_STRING_LENGTH) == 0 &&

+        (GaugeEntryExArray[Index2].Identifier == Identifier)) {

+      Index = Index2;

       break;

     }

   }

diff --git a/MdeModulePkg/Library/PeiPerformanceLib/PeiPerformanceLib.c b/MdeModulePkg/Library/PeiPerformanceLib/PeiPerformanceLib.c
index 7a2d3da..60451ee 100644
--- a/MdeModulePkg/Library/PeiPerformanceLib/PeiPerformanceLib.c
+++ b/MdeModulePkg/Library/PeiPerformanceLib/PeiPerformanceLib.c
@@ -113,6 +113,7 @@
   )

 {

   UINT32                    Index;

+  UINT32                    Index2;

   UINT32                    NumberOfEntries;

   PEI_PERFORMANCE_LOG_ENTRY *LogEntryArray;

 

@@ -126,13 +127,16 @@
   NumberOfEntries = PeiPerformanceLog->NumberOfEntries;

   LogEntryArray   = (PEI_PERFORMANCE_LOG_ENTRY *) (PeiPerformanceLog + 1);

 

+  Index2 = 0;

+

   for (Index = 0; Index < NumberOfEntries; Index++) {

-    if ((LogEntryArray[Index].Handle == (EFI_PHYSICAL_ADDRESS) (UINTN) Handle) &&

-         AsciiStrnCmp (LogEntryArray[Index].Token, Token, PEI_PERFORMANCE_STRING_LENGTH) == 0 &&

-         AsciiStrnCmp (LogEntryArray[Index].Module, Module, PEI_PERFORMANCE_STRING_LENGTH) == 0 &&

-         (PeiPerformanceIdArray[Index] == Identifier) &&

-         LogEntryArray[Index].EndTimeStamp == 0

-       ) {

+    Index2 = NumberOfEntries - 1 - Index;

+    if (LogEntryArray[Index2].EndTimeStamp == 0 &&

+        (LogEntryArray[Index2].Handle == (EFI_PHYSICAL_ADDRESS) (UINTN) Handle) &&

+        AsciiStrnCmp (LogEntryArray[Index2].Token, Token, PEI_PERFORMANCE_STRING_LENGTH) == 0 &&

+        AsciiStrnCmp (LogEntryArray[Index2].Module, Module, PEI_PERFORMANCE_STRING_LENGTH) == 0 &&

+        (PeiPerformanceIdArray[Index2] == Identifier)) {

+      Index = Index2;

       break;

     }

   }

diff --git a/MdeModulePkg/Library/SmmCorePerformanceLib/SmmCorePerformanceLib.c b/MdeModulePkg/Library/SmmCorePerformanceLib/SmmCorePerformanceLib.c
index ca953df..214d044 100644
--- a/MdeModulePkg/Library/SmmCorePerformanceLib/SmmCorePerformanceLib.c
+++ b/MdeModulePkg/Library/SmmCorePerformanceLib/SmmCorePerformanceLib.c
@@ -93,6 +93,7 @@
   )

 {

   UINT32                    Index;

+  UINT32                    Index2;

   UINT32                    NumberOfEntries;

   GAUGE_DATA_ENTRY_EX       *GaugeEntryExArray;

 

@@ -106,12 +107,16 @@
   NumberOfEntries = mGaugeData->NumberOfEntries;

   GaugeEntryExArray = (GAUGE_DATA_ENTRY_EX *) (mGaugeData + 1);

 

+  Index2 = 0;

+

   for (Index = 0; Index < NumberOfEntries; Index++) {

-    if ((GaugeEntryExArray[Index].Handle == (EFI_PHYSICAL_ADDRESS) (UINTN) Handle) &&

-         AsciiStrnCmp (GaugeEntryExArray[Index].Token, Token, SMM_PERFORMANCE_STRING_LENGTH) == 0 &&

-         AsciiStrnCmp (GaugeEntryExArray[Index].Module, Module, SMM_PERFORMANCE_STRING_LENGTH) == 0 &&

-         (GaugeEntryExArray[Index].Identifier == Identifier) &&

-         GaugeEntryExArray[Index].EndTimeStamp == 0) {

+    Index2 = NumberOfEntries - 1 - Index;

+    if (GaugeEntryExArray[Index2].EndTimeStamp == 0 &&

+        (GaugeEntryExArray[Index2].Handle == (EFI_PHYSICAL_ADDRESS) (UINTN) Handle) &&

+        AsciiStrnCmp (GaugeEntryExArray[Index2].Token, Token, SMM_PERFORMANCE_STRING_LENGTH) == 0 &&

+        AsciiStrnCmp (GaugeEntryExArray[Index2].Module, Module, SMM_PERFORMANCE_STRING_LENGTH) == 0 &&

+        (GaugeEntryExArray[Index2].Identifier == Identifier)) {

+      Index = Index2;

       break;

     }

   }