Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions tutorials/03-advanced/04-cached-processes/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Using cached processes

When you run an inventory or impact assessment, the engine builds a system of equations that represents all processes and their dependencies.
This system is then solved in one pass to compute the results.

For small models this is fine, but for large models the system can grow very large, making the solve more costly.

The `@cached` annotation helps here: it tells the engine to **pre-solve a subsystem** (a process and its dependencies) during runtime.
The result of this local solve is then reused in the global assessment.
This makes the final system smaller and the overall computation faster while giving exactly the same results.

## Try it yourself

The file `main.lca` defines a `bake` process annotated with `@cached`.

Run the following command once with the annotation, and once without:

```bash
lcaac trace sandwich_factory
```

Compare the two results:

Without `@cached`, you will see `flour_production` and `salt_production` listed as dependencies of `sandwich_factory`.

With `@cached`, these dependencies disappear from the trace. They are pre-solved and collapsed into the `bake` process.

The impacts remain the same in both cases. Only the solving pipeline changes.
56 changes: 56 additions & 0 deletions tutorials/03-advanced/04-cached-processes/main.lca
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
process sandwich_factory {
products {
1 u sandwich
}
inputs {
200 g bread
50 g ham
}
}

@cached
process bake {
products {
1 kg bread
}
inputs {
1 kg flour
30 g salt
}
}

process flour_production {
products {
1 kg flour
}
impacts {
2 kg_CO2_Eq GWP
}
}

process salt_production {
products {
1 g salt
}
impacts {
0.05 kg_CO2_Eq GWP
}
}

process ham_production {
products {
1 g ham
}
impacts {
0.7 kg_CO2_Eq GWP
}
}

test main_with_mock_data {
given {
1 u sandwich from sandwich_factory
}
assert {
GWP between 35.6 kg_CO2_Eq and 35.7 kg_CO2_Eq
}
}
2 changes: 2 additions & 0 deletions tutorials/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ lcaac test -p $TUTORIALS_PATH/03-advanced/02-circular-footprint-formula
lcaac test -p $TUTORIALS_PATH/03-advanced/03-project-file/lcaac.yaml main_with_data
lcaac test -p $TUTORIALS_PATH/03-advanced/03-project-file/lcaac-mock.yaml main_with_mock_data

lcaac test -p $TUTORIALS_PATH/03-advanced/04-cached-processes

# Check custom dimensions tutorial
set -euo
# The following assessment is expected to fail.
Expand Down