Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

feat: Cache text and version files to avoid read/import costs on all … (#9513)

Michael Chavinda07592149a979
diff --git a/marimo/_server/templates/api.py b/marimo/_server/templates/api.py
index 77348e6934..03e5f2319f 100644
--- a/marimo/_server/templates/api.py
+++ b/marimo/_server/templates/api.py
@@ -7,6 +7,7 @@ to HTML without needing to use internal template functions directly.
from __future__ import annotations
+import functools
from pathlib import Path
from typing import Any, Literal, cast
@@ -25,6 +26,7 @@ from marimo._session.model import SessionMode
from marimo._utils.code import hash_code
[email protected]_cache(maxsize=1)
def _get_html_template() -> str:
"""Get the base HTML template."""
from marimo._utils.paths import marimo_package_path
diff --git a/marimo/_server/templates/templates.py b/marimo/_server/templates/templates.py
index 4b7cbf5b11..daca9c3b62 100644
--- a/marimo/_server/templates/templates.py
+++ b/marimo/_server/templates/templates.py
@@ -1,6 +1,7 @@
# Copyright 2026 Marimo. All rights reserved.
from __future__ import annotations
+import functools
import html
import json
import os
@@ -619,7 +620,11 @@ def _del_none_or_empty(d: Any) -> Any:
}
[email protected]_cache(maxsize=1)
def get_version() -> str:
+ # Invariant for the lifetime of the process; cache so the three
+ # render-path call sites (mount_config + two .replace passes) don't
+ # each pay the importlib.metadata lookup.
return (
f"{__version__} (editable)" if is_editable("marimo") else __version__
)
diff --git a/marimo/_utils/versions.py b/marimo/_utils/versions.py
index 1e52b78204..33c32b0821 100644
--- a/marimo/_utils/versions.py
+++ b/marimo/_utils/versions.py
@@ -3,11 +3,19 @@ from __future__ import annotations
import json
import re
+from functools import cache
from importlib.metadata import Distribution
+@cache
def is_editable(pkg_name: str) -> bool:
- """Check if a package is an editable install"""
+ """Check if a package is an editable install.
+
+ Result is invariant within a process — a package can't become editable
+ while we're running — so we cache to avoid the importlib.metadata +
+ direct_url.json file read on every call. Render-path hot spot before
+ caching: ~10 posix.stat calls per render via templates.get_version.
+ """
try:
direct_url = Distribution.from_name(pkg_name).read_text(