# Sequences and Inlined Lambdas

**URL:** <https://discuss.kotlinlang.org/t/sequences-and-inlined-lambdas/17228>\
**Category:** Language Design\
**Created:** [April 20, 2020, 3:52am UTC](https://discuss.kotlinlang.org/t/sequences-and-inlined-lambdas/17228 "2020-04-20T03:52:33Z")\
**Posts on this page:** 7\
**Page:** 2

<div class="post-metadata">

**Author:** ![broot](https://avatars.discourse-cdn.com/v4/letter/b/a88e57/32.png) [@broot](https://discuss.kotlinlang.org/u/broot)\
**Post date:** [March 5, 2022, 11:45am UTC](https://discuss.kotlinlang.org/t/sequences-and-inlined-lambdas/17228/21 "2022-03-05T11:45:50Z")

</div>

Ohh, this is interesting read.

I was thinking about the same problem and if / how it could be improved. My gut feeling was always that the performance hit is relatively small, so it is not worth working on. But I was too lazy to verify this. 13% difference is definitely not worth added complexity, but 1.5-2.5x difference - well, maybe.

Anyway, regardless of the above, it is a very interesting topic and good stretch for our brains 🙂

Did you publish your benchmarks anywhere? By looking at very first benchmarks you posted here, you don’t consume the results in `optimal` case. Because of that, JVM could entirely remove all your code and in fact benchmark an empty function. Performance results seem like this is not happening, but still, I think it should be fixed.

---

<div class="post-metadata">

**Author:** ![derektom14](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/derektom14/32/7411_2.png) [@derektom14](https://discuss.kotlinlang.org/u/derektom14)\
**Post date:** [March 5, 2022, 7:49pm UTC](https://discuss.kotlinlang.org/t/sequences-and-inlined-lambdas/17228/22 "2022-03-05T19:49:19Z")

</div>

The 13% performance improvement might not always be worth the individual developers replacing their use of Kotlin’s convenience functions with properly optimized loops, but enabling developers to get that benefit conveniently by doing more work on the compiler, letting all developers get a 13% performance improvement for little additional effort, would be phenomenal. Also, for the benchmarks, I called `Blackhole.consumeResult`, which consumes the result of the method so that it doesn’t get optimized away, though I later learned that just returning the result from the benchmark method is sufficient.

---

<div class="post-metadata">

**Author:** ![broot](https://avatars.discourse-cdn.com/v4/letter/b/a88e57/32.png) [@broot](https://discuss.kotlinlang.org/u/broot)\
**Post date:** [March 5, 2022, 7:55pm UTC](https://discuss.kotlinlang.org/t/sequences-and-inlined-lambdas/17228/23 "2022-03-05T19:55:19Z")

</div>

Yes, you used blackhole, but you didn’t return `resultList`, so it was unused. You might make the same mistake in other benchmarks as well, because the classic loop is the only case when we create the result collection manually and we have to return it at the end.

---

<div class="post-metadata">

**Author:** ![derektom14](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/derektom14/32/7411_2.png) [@derektom14](https://discuss.kotlinlang.org/u/derektom14)\
**Post date:** [March 6, 2022, 8:44am UTC](https://discuss.kotlinlang.org/t/sequences-and-inlined-lambdas/17228/24 "2022-03-06T08:44:43Z")

</div>

Ah, I see, I was mistakenly consuming a `Unit` instead of the list I was creating! I’ll try to revive my benchmarking setup sometime to see if anything changes.

---

<div class="post-metadata">

**Author:** ![Cr0wly](https://avatars.discourse-cdn.com/v4/letter/c/8dc957/32.png) [@Cr0wly](https://discuss.kotlinlang.org/u/Cr0wly)\
**Post date:** [March 6, 2022, 9:50pm UTC](https://discuss.kotlinlang.org/t/sequences-and-inlined-lambdas/17228/25 "2022-03-06T21:50:35Z")

</div>

Why the developer has not yet given an answer how it works. I think this is a good place to discuss with them 🤔

---

<div class="post-metadata">

**Author:** ![TrevorHackman](https://avatars.discourse-cdn.com/v4/letter/t/f05b48/32.png) [@TrevorHackman](https://discuss.kotlinlang.org/u/TrevorHackman)\
**Post date:** [September 5, 2022, 12:59am UTC](https://discuss.kotlinlang.org/t/sequences-and-inlined-lambdas/17228/26 "2022-09-05T00:59:09Z")

</div>

This is a simple but difficult, and really important problem to solve. We should not have a standard library that encourages the writing of highly unoptimal code.

---

<div class="post-metadata">

**Author:** ![derektom14](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/derektom14/32/7411_2.png) [@derektom14](https://discuss.kotlinlang.org/u/derektom14)\
**Post date:** [March 5, 2026, 7:29pm UTC](https://discuss.kotlinlang.org/t/sequences-and-inlined-lambdas/17228/27 "2026-03-05T19:29:10Z")

</div>

I re-ran the Scrabble benchmark using Kotlin 2.1 and Kotlin compiler 2.2.21, and updating the code a bit to account for some deprecated methods. I got this result:

```auto
Benchmark Mode Cnt Score Error Units
ScrabbleBenchmark.kotlinScrabbleFunctional avgt 20 368.605 ± 14.725 ms/op
ScrabbleBenchmark.kotlinScrabbleImperative avgt 20 263.723 ± 12.468 ms/op

```

When I ran this about six years ago on a different machine, imperative was a 13% time reduction. Here, it’s a 29% time reduction, making it even more important that this issue be addressed.

[Previous page](https://discuss.kotlinlang.org/t/sequences-and-inlined-lambdas/17228.md?page=1)
