mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2026-12-03 08:17:52 +00:00
perf(cache): let nested lookups reuse the unrendered applications view
Every cold render of the applications SPOT walks the templates of all
269 roles, and those templates call lookup('config'), lookup('tls'),
lookup('domain') and others. Each of these re-enters
get_merged_applications while the render guard is set, and each one
rebuilt the unrendered view from scratch: a deepcopy of all role
defaults plus the override merge, about 8 ms, never cached. A local
cold render of the development inventory made 351 such calls, 2.82 s
of a 6.86 s render.
The outer render now keeps a copy of its unrendered view and its cache
key on the render guard, taken before the inventory volumes are
popped, so it is the same view a nested call used to build. A nested
call with the same key returns it; any other key still builds its own.
The guard drops it when the render ends.
Measured on the same inventory: the nested calls took 0.07 s instead
of 2.82 s and the cold render 3.60 s instead of 6.86 s. The rendered
SPOT is unchanged: the old and new dumps differ in 69 user passwords,
which the development inventory regenerates on every read (two runs of
the old code differ in 68), and in one absolute checkout path, the only
other difference.
In CI each cold render took 40 to 77 s and the heaviest swarm cell of
#950 missed its budget by a few minutes (#984).
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
e154ef48fe
commit
0cb8bae88d
3 files changed
+67
-1
No files matched your search
@@ -16,6 +16,7 @@ import tempfile
|
||||
import textwrap
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
from unittest import mock
|
||||
|
||||
from utils.cache import _reset_cache_for_tests
|
||||
from utils.cache import applications as cache_apps
|
||||
@@ -422,6 +423,60 @@ class TestGetMergedApplicationsCarrierFact(unittest.TestCase):
|
||||
)
|
||||
|
||||
|
||||
class TestNestedLookupsReuseTheUnrenderedView(unittest.TestCase):
|
||||
def setUp(self) -> None:
|
||||
_reset_cache_for_tests()
|
||||
|
||||
def test_nested_calls_during_one_render_build_the_defaults_once(self):
|
||||
with tempfile.TemporaryDirectory() as tmp:
|
||||
roles = _seed_minimal_roles(Path(tmp))
|
||||
_write(
|
||||
roles / "web-app-foo" / ROLE_FILE_META_SERVICES,
|
||||
"""
|
||||
foo:
|
||||
image: "pre-{{ nested }}"
|
||||
version: "v-{{ nested }}"
|
||||
""",
|
||||
)
|
||||
variables = {
|
||||
"applications": {
|
||||
"web-app-foo": {"volumes": {"data": {"type": "volume"}}}
|
||||
}
|
||||
}
|
||||
nested_views = []
|
||||
|
||||
class _NestingTemplar:
|
||||
def __init__(self) -> None:
|
||||
self.available_variables: dict = {}
|
||||
|
||||
def template(self, value, **_kwargs):
|
||||
nested_views.append(
|
||||
cache_apps.get_merged_applications(
|
||||
variables=self.available_variables,
|
||||
roles_dir=roles,
|
||||
templar=self,
|
||||
)
|
||||
)
|
||||
return "rendered"
|
||||
|
||||
with mock.patch.object(
|
||||
cache_apps,
|
||||
"get_application_defaults",
|
||||
wraps=cache_apps.get_application_defaults,
|
||||
) as defaults:
|
||||
rendered = cache_apps.get_merged_applications(
|
||||
variables=variables, roles_dir=roles, templar=_NestingTemplar()
|
||||
)
|
||||
|
||||
self.assertEqual(defaults.call_count, 1)
|
||||
self.assertGreaterEqual(len(nested_views), 2)
|
||||
self.assertTrue(all(view is nested_views[0] for view in nested_views))
|
||||
self.assertIn("volumes", nested_views[0]["web-app-foo"])
|
||||
self.assertEqual(
|
||||
rendered["web-app-foo"]["services"]["foo"]["image"], "rendered"
|
||||
)
|
||||
|
||||
|
||||
class TestRecursiveMetaWalk(unittest.TestCase):
|
||||
"""`_build_role_base_config` walks the whole `meta/` tree: every
|
||||
`meta/**/*.yml` lands at the matching nested path under
|
||||
|
||||
Reference in new issue
Block a user