You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@buildstream.apache.org by tv...@apache.org on 2021/02/04 07:39:33 UTC

[buildstream] 14/16: _variables.pyx: Try to improve fast path performance

This is an automated email from the ASF dual-hosted git repository.

tvb pushed a commit to branch tristan/variables-refactor
in repository https://gitbox.apache.org/repos/asf/buildstream.git

commit 1b1a9f8d8a611f900c3578a413b2f6d175c8ed2c
Author: Tristan van Berkom <tr...@codethink.co.uk>
AuthorDate: Sat Jul 18 16:15:56 2020 +0900

    _variables.pyx: Try to improve fast path performance
    
    Try using enumerate() for look instead of repeatedly indexing
    the list throughout a while loop.
---
 src/buildstream/_variables.pyx | 17 +++++++----------
 1 file changed, 7 insertions(+), 10 deletions(-)

diff --git a/src/buildstream/_variables.pyx b/src/buildstream/_variables.pyx
index c4f86b6..53a5696 100644
--- a/src/buildstream/_variables.pyx
+++ b/src/buildstream/_variables.pyx
@@ -374,18 +374,15 @@ cdef class Variables:
         if counter > 1000:
             raise RecursionError()
 
-        cdef Py_ssize_t idx = 0
-        cdef Py_ssize_t value_len = len(value)
-        cdef str sub
+        cdef Py_ssize_t idx
+        cdef object val
         cdef list acc = []
 
-        while idx < value_len:
-            acc.append(value[idx])
-            idx += 1
-
-            if idx < value_len:
-                acc.append(self._fast_expand_var(<str> value[idx], counter + 1))
-            idx += 1
+        for idx, val in enumerate(value):
+            if (idx % 2) == 0:
+                acc.append(val)
+            else:
+                acc.append(self._fast_expand_var(<str> val, counter + 1))
 
         return "".join(acc)