git » git-arr » commit 107b149

git: Make smstr.html and smstr.url cached properties

author Alberto Bertogli
2025-05-18 23:12:05 UTC
committer Alberto Bertogli
2025-05-18 23:16:00 UTC
parent 535aa3361f5f3cafcfc2ed55a57a527f38499df3

git: Make smstr.html and smstr.url cached properties

We use smstr a lot when parsing git output, but a lot of times we don't
need the HTML or URL conversions. However, today we always compute them.

This patch makes smstr.html and smstr.url cached properties, so they
will only be computed when needed.

This results in a noticeable speedup when regenerating repositories.

git.py +6 -3

diff --git a/git.py b/git.py
index 0314f07..a2904c3 100644
--- a/git.py
+++ b/git.py
@@ -154,8 +154,6 @@ class smstr:
 
     def __init__(self, s: str):
         self.raw = s
-        self.url = urllib.request.pathname2url(s)
-        self.html = self._to_html()
 
     # Note we don't define __repr__() or __str__() to prevent accidental
     # misuse. It does mean that some uses become more annoying, so it's a
@@ -175,7 +173,12 @@ class smstr:
             other = other.raw
         return smstr(self.raw + other)
 
-    def _to_html(self):
+    @functools.cached_property
+    def url(self):
+        return urllib.request.pathname2url(self.raw)
+
+    @functools.cached_property
+    def html(self):
         """Returns an html representation of the unicode string."""
         html = ""
         for c in escape(self.raw):