# HG changeset patch # User David Barts # Date 1559012995 25200 # Node ID 0fb455b46e5f0c763178054487f47be566cdfadb # Parent cc975bf7a3fae8c48ad6982001d56c180abe133d Add cache. diff -r cc975bf7a3fa -r 0fb455b46e5f tincan.py --- a/tincan.py Mon May 27 18:14:14 2019 -0700 +++ b/tincan.py Mon May 27 20:09:55 2019 -0700 @@ -350,6 +350,18 @@ if not self.fname.endswith(_IEXTEN): raise ValueError("file does not end in {0}".format(_IEXTEN)) +# Using a cache is likely to help efficiency a lot, since many pages +# will typically #include the same standard stuff. +_tcache = {} +def _get_template(name, direct): + aname = os.path.abspath(os.path.join(direct, name)) + if aname not in _tcache: + tmpl = ChameleonTemplate(name=name, lookup=[direct]) + tmpl.prepare() + assert aname == tmpl.filename + _tcache[aname] = tmpl + return _tcache[aname] + # R o u t e s # # Represents a route in TinCan. Our launcher creates these on-the-fly based @@ -483,8 +495,7 @@ else: fdir = os.path.join(self._fsroot, *self._subdir) try: - tmpl = ChameleonTemplate(name=include.fname, lookup=[fdir]) - tmpl.prepare() + tmpl = _get_template(include.fname, fdir) except Exception as e: raise TinCanError("{0}: bad #include: {1!s}".format(self._urlpath, e)) from e self._includes[include.vname] = tmpl.tpl