LeoWoerteler/basex — Issue #4: Inline Function Calls
Author: LeoWoerteler
Created: 2012-02-20 15:46:02+00:00
State: open

User-defined functions should be inlined where possible, eliminating the call overhead and enabling more optimizations.

Example:
-------------
```xquery
declare function local:foo($x as xs:integer) as xs:integer {
if($x lt 100) then $x
else 100
};

2 * local:foo(21)
```

After inlining this would be

```xquery
2 * (
let $x as xs:integer := 21
return
if($x lt 100) then $x
else 100
)
```

which BaseX can evaluate at compile-time:

**Compiling:**

- binding static variable $x as xs:integer
- pre-evaluating 21 lt 100
- pre-evaluating if(true()) then $x else 100
- removing variable $x as xs:integer
- simplifying flwor expression
- pre-evaluating 2 * 21

**Result:** 42

Preliminaries:
-------------------

* argument and result casts have to be explicit (working on it)
* function body has to be copyable
* one has to be careful while inlining recursive functions, otherwise the compiler might loop