Skip to content

feat: caching resource location with file content in mecha - #517

Open
edayot wants to merge 1 commit into
mcbeet:mainfrom
edayot:mecha_cache
Open

edayot wants to merge 1 commit into
mcbeet:mainfrom
edayot:mecha_cache

Conversation

@edayot

@edayot edayot commented Jul 26, 2026 •

Copy link
Copy Markdown
Contributor

This PR improve caching in mecha. Very useful if you generate bolt code in python like i do.

Here's an example with SimpleDrawer repo :

Left: Current mecha
Right: This PR
image

Redo of mcbeet/mecha#313

@edayot
edayot marked this pull request as ready for review July 26, 2026 19:07
@rx-dev
rx-dev requested a review from vberlier October 8, 2026 05:36
@vberlier

vberlier commented Oct 8, 2026

Copy link
Copy Markdown
Member

The issue with this is now there's no way to opt out of ast caching.

It's pretty reasonable to assume ast caching is a net benefit when parsing from disk, but not necessarily when parsing code that's already in memory. Depending on how mecha is configured, the extra sha256 and I/O overhead could perform worse than simply re-parsing what's already in memory, for example a function tree generated by a plugin with tons of really small files. Of course in most cases it's better to generate code as plain mcfunction, this way you can just configure your pipeline to not process the generated code through mecha at all, but I understand this might not always be practical.

The good news is you can actually opt into ast caching for generated code by simply caching your generated code itself:

n = 128
path = ctx.cache["thing"].get_path(f"{n=}")
if not path.exists():
    path.write_text("".join([f"say {i}\n" for i in range(n)]))

ctx.data["demo:thing"] = Function(source_path=path)

I'd recommend ctx.generate.draft() for caching pack resources specifically:

n = 128
with ctx.generate.draft() as draft:
    draft.cache("thing", f"{n=}")
    draft.data["demo:thing"] = Function([f"say {i}" for i in range(n)])

Just note that right now with this approach you'll have to wait for the second rebuild for mecha to cache the ast. On the first build, drafted files are saved to the cache but their contents remain in memory so mecha's ast caching will not kick in. It's only starting from the second build that drafted files are loaded from disk, and only then will mecha attempt to cache the ast. But we could consider making ctx.generate.draft() clear the in-memory contents and force a re-load from disk after saving drafted files.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants