From 4e375707392e4f9085e2d2342e41aee9d4df2b0a Mon Sep 17 00:00:00 2001 From: BobCF Date: Thu, 8 Nov 2018 18:16:25 +0800 Subject: BaseTools: Optimize string concatenation https://bugzilla.tianocore.org/show_bug.cgi?id=1288 This patch is one of build tool performance improvement series patches. This patch is going to use join function instead of string += string2 statement. Current code use string += string2 in a loop to combine a string. while creating a string list in a loop and using "".join(stringlist) after the loop will be much faster. Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: BobCF Cc: Liming Gao Cc: Jaben Carsey Reviewed-by: Liming Gao Reviewed-by: Jaben Carsey --- BaseTools/Source/Python/Common/Misc.py | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) (limited to 'BaseTools/Source/Python/Common/Misc.py') diff --git a/BaseTools/Source/Python/Common/Misc.py b/BaseTools/Source/Python/Common/Misc.py index 2354f392f2..84f3f16ee6 100644 --- a/BaseTools/Source/Python/Common/Misc.py +++ b/BaseTools/Source/Python/Common/Misc.py @@ -778,7 +778,7 @@ class TemplateString(object): ## Constructor def __init__(self, Template=None): - self.String = '' + self.String = [] self.IsBinary = False self._Template = Template self._TemplateSectionList = self._Parse(Template) @@ -788,7 +788,7 @@ class TemplateString(object): # @retval string The string replaced # def __str__(self): - return self.String + return "".join(self.String) ## Split the template string into fragments per the ${BEGIN} and ${END} flags # @@ -836,9 +836,12 @@ class TemplateString(object): def Append(self, AppendString, Dictionary=None): if Dictionary: SectionList = self._Parse(AppendString) - self.String += "".join(S.Instantiate(Dictionary) for S in SectionList) + self.String.append( "".join(S.Instantiate(Dictionary) for S in SectionList)) else: - self.String += AppendString + if isinstance(AppendString,list): + self.String.extend(AppendString) + else: + self.String.append(AppendString) ## Replace the string template with dictionary of placeholders # @@ -1744,10 +1747,7 @@ class PathClass(object): # @retval True The two PathClass are the same # def __eq__(self, Other): - if isinstance(Other, type(self)): - return self.Path == Other.Path - else: - return self.Path == str(Other) + return self.Path == str(Other) ## Override __cmp__ function # @@ -1757,10 +1757,7 @@ class PathClass(object): # @retval -1 The first PathClass is less than the second PathClass # @retval 1 The first PathClass is Bigger than the second PathClass def __cmp__(self, Other): - if isinstance(Other, type(self)): - OtherKey = Other.Path - else: - OtherKey = str(Other) + OtherKey = str(Other) SelfKey = self.Path if SelfKey == OtherKey: -- cgit v1.2.3