> For the complete documentation index, see [llms.txt](https://php-fhir-tools.ardenexal.net/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://php-fhir-tools.ardenexal.net/fhirpath/performance.md).

# Compilation, Caching & Performance

Parsing a FHIRPath expression into an AST is the costly step. To avoid repeating it, expressions can be pre-compiled once and evaluated many times, and the service caches compiled expressions automatically.

## Compilation

`compile()` parses an expression and returns a `CompiledExpression` (`Ardenexal\FHIRTools\Component\FHIRPath\Service\CompiledExpression`) holding the pre-parsed AST. Evaluating it skips lexing and parsing entirely:

```php
// Compile once, evaluate many times
$compiled = $service->compile('name.where(use = "official").given.first()');

foreach ($patients as $patient) {
    $result = $compiled->evaluate($patient);
}
```

A `CompiledExpression` also exposes `getExpression()`, `getAst()`, and `__toString()`.

## Caching

`evaluate()` and `compile()` both route through an internal cache, so even without calling `compile()` explicitly, repeated evaluation of the same expression string reuses the parsed AST.

The default cache is `InMemoryExpressionCache` (`Ardenexal\FHIRTools\Component\FHIRPath\Cache\InMemoryExpressionCache`), which keys compiled expressions by their source string.

{% hint style="info" %}
The cache holds up to **100 expressions by default** and evicts the **least recently used (LRU)** entry when full. Construct the cache with a different `maxSize` to tune this.
{% endhint %}

### Custom cache

Inject any `ExpressionCacheInterface` implementation via the constructor:

```php
use Ardenexal\FHIRTools\Component\FHIRPath\Cache\InMemoryExpressionCache;
use Ardenexal\FHIRTools\Component\FHIRPath\Service\FHIRPathService;

$service = new FHIRPathService(new InMemoryExpressionCache(maxSize: 500));
```

### Cache statistics and control

```php
$stats = $service->getCacheStats();
// ['hits' => 10, 'misses' => 2, 'size' => 5]

$service->clearCache();          // empty the cache and reset counters
$cache = $service->getCache();   // the underlying ExpressionCacheInterface
```

The in-memory cache additionally exposes `getHitRate()` (0–100) and `getMaxSize()`.

## Tips

* Compile expressions used in hot loops once, outside the loop.
* Reuse a single `FHIRPathService` instance so its cache survives across calls.
* For large batches that exceed the default 100-entry cache, raise `maxSize` to keep all expressions resident and avoid re-parsing on eviction.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://php-fhir-tools.ardenexal.net/fhirpath/performance.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
