# Why AutoCloseable.use method is inlined?

**URL:** <https://discuss.kotlinlang.org/t/why-autocloseable-use-method-is-inlined/5407>\
**Category:** Libraries\
**Created:** [November 14, 2017, 5:36pm UTC](https://discuss.kotlinlang.org/t/why-autocloseable-use-method-is-inlined/5407 "2017-11-14T17:36:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![imanushin](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/imanushin/32/5471_2.png) [@imanushin](https://discuss.kotlinlang.org/u/imanushin)\
**Post date:** [November 14, 2017, 5:36pm UTC](https://discuss.kotlinlang.org/t/why-autocloseable-use-method-is-inlined/5407/1 "2017-11-14T17:36:09Z")

</div>

Hi !

Does anybody knows, why method below is inlined always?

```auto
public inline fun <T : AutoCloseable?, R> T.use(block: (T) -> R): R

```

We can create lambda for this case in heap (e.g. instead of method inlining - just do library call).  
Why:

1. Inline methods increase total byte code soze, e.g. increases application complexity from JVM side
2. The same code is copy-pasted multiple time, so JIT could not understand, that optimization of it can be the same
3. `close` method is called for case, when we are going to create new objects in heap. This means, that we can create one more object (e.g our function and closed variables) without any big affect (instead of collection iteration, etc.).
4. Inline functions are not default for JVM, e.g. it is better to use them as less as possible
5. If JVM really need to inline this method, it can do this by itself.

Do we have performance reasons for this function inlining? Do we have any other reasons to do this?

---

<div class="post-metadata">

**Author:** ![elizarov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/elizarov/32/2164_2.png) [@elizarov](https://discuss.kotlinlang.org/u/elizarov)\
**Post date:** [November 14, 2017, 5:59pm UTC](https://discuss.kotlinlang.org/t/why-autocloseable-use-method-is-inlined/5407/2 "2017-11-14T17:59:40Z")

</div>

You can implement a non-inline version of `use` and compare various performance tradeoffs versus inline version of it in terms of code size (both raw and zipped), performance, memory consumption, etc. It would do a great blog post and I’m sure many will share.

---

<div class="post-metadata">

**Author:** ![ilogico](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/ilogico/32/3725_2.png) [@ilogico](https://discuss.kotlinlang.org/u/ilogico)\
**Post date:** [November 14, 2017, 6:10pm UTC](https://discuss.kotlinlang.org/t/why-autocloseable-use-method-is-inlined/5407/3 "2017-11-14T18:10:15Z")

</div>

I believe the method is inline to mimic what you would do in Java with the `try-with-resources` statement.

---

<div class="post-metadata">

**Author:** ![vbezhenar](https://avatars.discourse-cdn.com/v4/letter/v/35a633/32.png) [@vbezhenar](https://discuss.kotlinlang.org/u/vbezhenar)\
**Post date:** [November 15, 2017, 10:37am UTC](https://discuss.kotlinlang.org/t/why-autocloseable-use-method-is-inlined/5407/4 "2017-11-15T10:37:55Z")

</div>

I don’t think that JVM would inline lambda methods. Another reason is that with `inline` you can use return and it would be really weird not being able to call `return` inside `use`.

---

<div class="post-metadata">

**Author:** ![yole](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/yole/32/1425_2.png) [@yole](https://discuss.kotlinlang.org/u/yole)\
**Post date:** [November 15, 2017, 6:23pm UTC](https://discuss.kotlinlang.org/t/why-autocloseable-use-method-is-inlined/5407/5 "2017-11-15T18:23:38Z")

</div>

The key reason why this is `inline` is to support non-local returns. In Java, if you put a `return` statement in a `try`-with-resources block, this will return out of the enclosing function. Since we offer `use` as a replacement for `try`-with-resources, it’s only natural to preserve this ability. And non-local returns are only supported for inline functions.
