{"id":13822271,"url":"https://github.com/typst/comemo","last_synced_at":"2025-05-15T12:03:42.583Z","repository":{"id":61489959,"uuid":"533273563","full_name":"typst/comemo","owner":"typst","description":"Incremental computation through constrained memoization.","archived":false,"fork":false,"pushed_at":"2024-11-04T10:33:05.000Z","size":89,"stargazers_count":508,"open_issues_count":0,"forks_count":22,"subscribers_count":5,"default_branch":"main","last_synced_at":"2025-05-15T02:34:50.365Z","etag":null,"topics":["constraints","incremental","memoization","tracked"],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/typst.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE-APACHE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null},"funding":{"github":["typst"]}},"created_at":"2022-09-06T10:28:56.000Z","updated_at":"2025-05-13T12:58:10.000Z","dependencies_parsed_at":"2024-03-05T13:48:59.545Z","dependency_job_id":"88c675eb-4281-44ac-a039-570d64d949ed","html_url":"https://github.com/typst/comemo","commit_stats":{"total_commits":32,"total_committers":2,"mean_commits":16.0,"dds":0.03125,"last_synced_commit":"15f5d91329d0511623ebd7ed6f8909013a330907"},"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/typst%2Fcomemo","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/typst%2Fcomemo/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/typst%2Fcomemo/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/typst%2Fcomemo/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/typst","download_url":"https://codeload.github.com/typst/comemo/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254337612,"owners_count":22054253,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["constraints","incremental","memoization","tracked"],"created_at":"2024-08-04T08:01:51.999Z","updated_at":"2025-05-15T12:03:37.560Z","avatar_url":"https://github.com/typst.png","language":"Rust","funding_links":["https://github.com/sponsors/typst"],"categories":["Rust"],"sub_categories":[],"readme":"# comemo\n[![Crates.io](https://img.shields.io/crates/v/comemo.svg)](https://crates.io/crates/comemo)\n[![Documentation](https://docs.rs/comemo/badge.svg)](https://docs.rs/comemo)\n\nIncremental computation through constrained memoization.\n\n```toml\n[dependencies]\ncomemo = \"0.4\"\n```\n\nA _memoized_ function caches its return values so that it only needs to be\nexecuted once per set of unique arguments. This makes for a great optimization\ntool. However, basic memoization is rather limited. For more advanced use cases\nlike incremental compilers, it lacks the necessary granularity. Consider, for\nexample, the case of the simple `.calc` scripting language. Scripts in this\nlanguage consist of a sum of numbers and `eval` statements that reference other\n`.calc` scripts. A few examples are:\n\n- `alpha.calc`: `\"2 + eval beta.calc\"`\n- `beta.calc`: `\"2 + 3\"`\n- `gamma.calc`: `\"8 + 3\"`\n\nWe can easily write an interpreter that computes the output of a `.calc` file:\n\n```rust\n/// Evaluate a `.calc` script.\nfn evaluate(script: \u0026str, files: \u0026Files) -\u003e i32 {\n    script\n        .split('+')\n        .map(str::trim)\n        .map(|part| match part.strip_prefix(\"eval \") {\n            Some(path) =\u003e evaluate(\u0026files.read(path), files),\n            None =\u003e part.parse::\u003ci32\u003e().unwrap(),\n        })\n        .sum()\n}\n\nimpl Files {\n    /// Read a file from storage.\n    fn read(\u0026self, path: \u0026str) -\u003e String {\n        ...\n    }\n}\n```\n\nBut what if we want to make this interpreter _incremental,_ meaning that it only\nrecomputes a script's result if it or any of its dependencies change? Basic\nmemoization won't help us with this because the interpreter needs the whole set\nof files as input—meaning that a change to any file invalidates all memoized\nresults.\n\nThis is where comemo comes into play. It implements _constrained memoization_\nwith more fine-grained access tracking. To use it, we can just:\n\n- Add the `#[memoize]` attribute to the `evaluate` function.\n- Add the `#[track]` attribute to the impl block of `Files`.\n- Wrap the `files` argument in comemo's `Tracked` container.\n\nThis instructs comemo to memoize the evaluation and to automatically track all\nfile accesses during a memoized call. As a result, we can reuse the result of a\n`.calc` script evaluation as long as its dependencies stay the same—even if\nother files change.\n\n```rust\nuse comemo::{memoize, track, Tracked};\n\n/// Evaluate a `.calc` script.\n#[memoize]\nfn evaluate(script: \u0026str, files: Tracked\u003cFiles\u003e) -\u003e i32 {\n    ...\n}\n\n#[track]\nimpl Files {\n    /// Read a file from storage.\n    fn read(\u0026self, path: \u0026str) -\u003e String {\n        ...\n    }\n}\n```\n\nFor the full example see [`examples/calc.rs`][calc].\n\n[calc]: https://github.com/typst/comemo/blob/main/examples/calc.rs\n\n## License\nThis crate is dual-licensed under the MIT and Apache 2.0 licenses.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftypst%2Fcomemo","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftypst%2Fcomemo","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftypst%2Fcomemo/lists"}