From 9b44ba3ef2c3075706af2bfd744d51e14fd6e52b Mon Sep 17 00:00:00 2001
From: Philip Sargent <philip.sargent@gmail.com>
Date: Sun, 19 Dec 2021 14:24:20 +0000
Subject: [PATCH] precompile regexes

---
 parsers/survex.py | 20 ++++++++++++++++++--
 1 file changed, 18 insertions(+), 2 deletions(-)

diff --git a/parsers/survex.py b/parsers/survex.py
index 9662b54..87c43d0 100644
--- a/parsers/survex.py
+++ b/parsers/survex.py
@@ -130,6 +130,10 @@ class LoadingSurvex():
     rx_star    = re.compile(r'(?i)\s*\*[\s,]*(\w+)\s*(.*?)\s*(?:;.*)?$')
     rx_starref = re.compile(r'(?i)^\s*\*ref[\s.:]*((?:19[6789]\d)|(?:20[0123]\d))\s*#?\s*(X)?\s*(.*?\d+.*?)$')
     rx_argsref = re.compile(r'(?i)^[\s.:]*((?:19[6789]\d)|(?:20[0123]\d))\s*#?\s*(X)?\s*(.*?\d+.*?)$')
+    
+    rx_ref2 = re.compile(r'(?i)\s*ref[.;]?')
+    rx_ref3 = re.compile(r'(?i)\s*wallet[.;]?')
+
 
     # This interprets the survex "*data normal" command which sets out the order of the fields in the data, e.g.
     # *DATA normal from to length gradient bearing ignore ignore ignore ignore
@@ -760,13 +764,18 @@ class LoadingSurvex():
 
     def LoadSurvexComment(self, survexblock, comment):
         # ignore all comments except ;ref, ; wallet and ;QM and ;*include (for collated survex file)
+        #    rx_ref2 = re.compile(r'(?i)\s*ref[.;]?')
+        #    rx_ref3 = re.compile(r'(?i)\s*wallet[.;]?')
+
         refline = self.rx_commref.match(comment)
         if refline:
-            comment = re.sub('(?i)\s*ref[.;]?',"",comment.strip())
+            #comment = re.sub('(?i)\s*ref[.;]?',"",comment.strip())
+            comment = self.rx_ref2.sub("",comment.strip())
             self.LoadSurvexRef(survexblock, comment)
         walletline = self.rx_wallet.match(comment)
         if walletline:
-            comment = re.sub('(?i)\s*wallet[.;]?',"",comment.strip())
+            #comment = re.sub('(?i)\s*wallet[.;]?',"",comment.strip())
+            comment = self.rx_ref3.sub("",comment.strip())
             self.LoadSurvexRef(survexblock, comment)
         implicitline = self.rx_implicit.match(comment)
         if implicitline:
@@ -1287,9 +1296,16 @@ def FindAndLoadSurvex(survexblockroot):
     # line is held in memory at a time:
     with open(collatefilename, "r") as fcollate:
             svxlines = fcollate.read().splitlines()
+    #pr2 = cProfile.Profile()
+    #pr2.enable()
     #----------------------------------------------------------------
     svx_load.LinearLoad(survexblockroot,survexfileroot.path, svxlines)
     #----------------------------------------------------------------
+    #pr2.disable()
+    # with open('LinearLoad.prof', 'w') as f:
+        # ps = pstats.Stats(pr2, stream=f)
+        # ps.sort_stats(SortKey.CUMULATIVE)
+        # ps.print_stats()
 
     print("\n - MEM:{:7.2f} MB STOP".format(mem1),file=sys.stderr)
     print(" - MEM:{:7.3f} MB USED".format(mem1-mem0),file=sys.stderr)