ceylon/ceylon-compiler — Issue #316: Optimise access to boxed constants
Author: FroMage
Created: 2012-01-06 18:37:48+00:00
State: open
Labels: FEATURE

We could detect that some constants need to be boxed more than once in a method and optimise it.

For example we could optimise the following code:

```java
n1 = getBoxed(.ceylon.language.Integer.instance(1L)).getPositiveValue().longValue();
n1 = getBoxed(.ceylon.language.Integer.instance(1L)).getNegativeValue().longValue();
n1 = n2.plus(.ceylon.language.Integer.instance(n3)).longValue();
n1 = n2.minus(.ceylon.language.Integer.instance(n3)).longValue();
n1 = n2.times(.ceylon.language.Integer.instance(n3)).longValue();
n1 = n2.divided(.ceylon.language.Integer.instance(n3)).longValue();
n1 = n2.remainder(.ceylon.language.Integer.instance(n3)).longValue();
n1 = n2.power(.ceylon.language.Integer.instance(n3)).longValue();
```

into:

```java
.ceylon.language.Integer ONE = .ceylon.language.Integer.instance(1L);
n1 = getBoxed(ONE).getPositiveValue().longValue();
n1 = getBoxed(ONE).getNegativeValue().longValue();
.ceylon.language.Integer N3 = .ceylon.language.Integer.instance(n3);
n1 = n2.plus(N3).longValue();
n1 = n2.minus(N3).longValue();
n1 = n2.times(N3).longValue();
n1 = n2.divided(N3).longValue();
n1 = n2.remainder(N3).longValue();
n1 = n2.power(N3).longValue();
```

If we can prove that an expression will need boxing more than once and is a constant.