# What is the benefit of inlining methods with \`yield\`?

**URL:** <https://forum.crystal-lang.org/t/what-is-the-benefit-of-inlining-methods-with-yield/7194>\
**Category:** Help & Support\
**Created:** [September 20, 2024, 7:49am UTC](https://forum.crystal-lang.org/t/what-is-the-benefit-of-inlining-methods-with-yield/7194 "2024-09-20T07:49:04Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![jgaskins](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/jgaskins/32/2449_2.png) [@jgaskins](https://forum.crystal-lang.org/u/jgaskins)\
**Post date:** [September 20, 2024, 7:49am UTC](https://forum.crystal-lang.org/t/what-is-the-benefit-of-inlining-methods-with-yield/7194/1 "2024-09-20T07:49:04Z")

</div>

I’m trying to understand the benefits of the method inlining that comes with using a method that `yield`s. Is it compile-time performance? Runtime performance? A secret third thing?

The reason I’m thinking about this is that I keep running into problems where a method that `yield`s cannot be recursive (it would inline infinitely) and captured blocks can’t have code that `yield`s inside of them. I understand this pretty well, but I don’t understand why the distinction exists in the first place. I assumed it was for runtime performance, but LLVM already does automatic inlining, so maybe there are aspects I’m missing.

---

<div class="post-metadata">

**Author:** ![ysbaddaden](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/ysbaddaden/32/2252_2.png) [@ysbaddaden](https://forum.crystal-lang.org/u/ysbaddaden)\
**Post date:** [September 20, 2024, 7:55am UTC](https://forum.crystal-lang.org/t/what-is-the-benefit-of-inlining-methods-with-yield/7194/2 "2024-09-20T07:55:26Z")

</div>

@asterite should have the actual answer.

I think it’s not much a benefit but a requirement to implement blocks in Crystal, especially with the `next` and `break` statements.

---

<div class="post-metadata">

**Author:** ![bcardiff](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/bcardiff/32/3_2.png) [@bcardiff](https://forum.crystal-lang.org/u/bcardiff)\
**Post date:** [September 20, 2024, 8:21am UTC](https://forum.crystal-lang.org/t/what-is-the-benefit-of-inlining-methods-with-yield/7194/3 "2024-09-20T08:21:46Z")

</div>

IIRC the motivation was partially runtime performance to avoid the cost of calling a method when using methods like `#each` that mimics iteration. So yes, inlining. This also allows to keep direct access of variables of the caller in the stack as opposed to have a reference to them in the closure block.

But also, when the block has no type restriction it was convenient for the compiler implementation to have it this way. Maybe this is more historical, not completely sure. But at the very beginning I think we didn’t even have &block syntax and it was only the yield that marked the method. As such there was no type restriction.

Although all methods are like “C++ templates”, the yielding are even more because of this. That was my lame mental model at the very beginning.

One drawback of this approach is that we don’t reutilize typing and codegen of the yielding methods.

---

<div class="post-metadata">

**Author:** ![straight-shoota](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/straight-shoota/32/36_2.png) [@straight-shoota](https://forum.crystal-lang.org/u/straight-shoota)\
**Post date:** [September 20, 2024, 9:33am UTC](https://forum.crystal-lang.org/t/what-is-the-benefit-of-inlining-methods-with-yield/7194/4 "2024-09-20T09:33:37Z")

</div>

> [@bcardiff](#):
>
> One drawback of this approach is that we don’t reutilize typing and codegen of the yielding methods.

I guess that means it’s even adverse to compile-time performance 🤷

---

<div class="post-metadata">

**Author:** ![bcardiff](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/bcardiff/32/3_2.png) [@bcardiff](https://forum.crystal-lang.org/u/bcardiff)\
**Post date:** [September 20, 2024, 12:37pm UTC](https://forum.crystal-lang.org/t/what-is-the-benefit-of-inlining-methods-with-yield/7194/5 "2024-09-20T12:37:24Z")

</div>

Oh yeah. next and break are definitely a constraint.

---

<div class="post-metadata">

**Author:** ![jgaskins](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/jgaskins/32/2449_2.png) [@jgaskins](https://forum.crystal-lang.org/u/jgaskins)\
**Post date:** [September 20, 2024, 11:08pm UTC](https://forum.crystal-lang.org/t/what-is-the-benefit-of-inlining-methods-with-yield/7194/6 "2024-09-20T23:08:58Z")

</div>

Do `next` and `break` not work with captured blocks? I thought they did but I don’t use them enough to remember off the top of my head.

---

<div class="post-metadata">

**Author:** ![bcardiff](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/bcardiff/32/3_2.png) [@bcardiff](https://forum.crystal-lang.org/u/bcardiff)\
**Post date:** [September 21, 2024, 12:33am UTC](https://forum.crystal-lang.org/t/what-is-the-benefit-of-inlining-methods-with-yield/7194/7 "2024-09-21T00:33:42Z")

</div>

Next yes. Break and return no.

[https://crystal-lang.org/reference/1.13/syntax\_and\_semantics/capturing\_blocks.html#break-and-next](https://crystal-lang.org/reference/1.13/syntax_and_semantics/capturing_blocks.html#break-and-next)

---

<div class="post-metadata">

**Author:** ![jgaskins](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/jgaskins/32/2449_2.png) [@jgaskins](https://forum.crystal-lang.org/u/jgaskins)\
**Post date:** [September 21, 2024, 6:22am UTC](https://forum.crystal-lang.org/t/what-is-the-benefit-of-inlining-methods-with-yield/7194/8 "2024-09-21T06:22:17Z")

</div>

Fascinating, I’ve somehow never noticed (or maybe forgot about?) this distinction.

I think something just clicked for me that hadn’t before. It seems `break` is really about escaping the underlying `while` loop (or similar construct) once all the layers of blocks are unrolled?

---

<div class="post-metadata">

**Author:** ![jgaskins](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/jgaskins/32/2449_2.png) [@jgaskins](https://forum.crystal-lang.org/u/jgaskins)\
**Post date:** [September 21, 2024, 6:34am UTC](https://forum.crystal-lang.org/t/what-is-the-benefit-of-inlining-methods-with-yield/7194/9 "2024-09-21T06:34:07Z")

</div>

> [@straight-shoota](#):
>
> I guess that means it’s even adverse to compile-time performance 🤷

I actually had this as a followup question in mind in posting this thread. Basically, whether it could speed up compilation times if we replaced `yield` entirely with captured blocks so we wouldn’t have to recompile the `each` method every time an `Enumerable` method was called. But `break` and `return` are too valuable to give up in blocks, so if that can’t be implemented without `yield` it’s a moot point.

---

<div class="post-metadata">

**Author:** ![bcardiff](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/bcardiff/32/3_2.png) [@bcardiff](https://forum.crystal-lang.org/u/bcardiff)\
**Post date:** [September 21, 2024, 12:46pm UTC](https://forum.crystal-lang.org/t/what-is-the-benefit-of-inlining-methods-with-yield/7194/10 "2024-09-21T12:46:31Z")

</div>

If we would compile using some kind of CPS ([Continuation-passing style - Wikipedia](https://en.m.wikipedia.org/wiki/Continuation-passing_style)) I think we could compile once and reuse them. But the cost is that the runtime will be heavier probably. I don’t think llvm ir supports cps directly so we will need to represent them. Maybe MLIR have some support for them but that would be an entire different backend for the compiler.

---

<div class="post-metadata">

**Author:** ![ysbaddaden](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/ysbaddaden/32/2252_2.png) [@ysbaddaden](https://forum.crystal-lang.org/u/ysbaddaden)\
**Post date:** [September 23, 2024, 9:01am UTC](https://forum.crystal-lang.org/t/what-is-the-benefit-of-inlining-methods-with-yield/7194/11 "2024-09-23T09:01:25Z")

</div>

Hum, naive thinking here, but… could blocks have some special signature? For example pass the control flow through the return type (and pass the return value as an `out` arg). Like success=0, next=-1, break=-2, return=-3, …

```auto
(Foo) -> Bar
fun (Bar*, Foo) : Int32

```

Then `yield` becomes a `case` and the state should bubble up until it’s meaningful and handled? Well, that’s probably where the naive thinking gets kicked by reality 😅

Note: compiling with optimizations should still inline the blocks.

---

<div class="post-metadata">

**Author:** ![asterite](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/asterite/32/60_2.png) [@asterite](https://forum.crystal-lang.org/u/asterite)\
**Post date:** [September 23, 2024, 12:46pm UTC](https://forum.crystal-lang.org/t/what-is-the-benefit-of-inlining-methods-with-yield/7194/12 "2024-09-23T12:46:33Z")

</div>

The main thing about yield is that doing return inside it returns from the outer method. That’s not the case with closures or lambdas in general. In Ruby (and our interpreter too, if I remember correctly) that’s implemente with some thing similar to exceptions, but that’s slower than inlining.

---

<div class="post-metadata">

**Author:** ![beta-ziliani](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/beta-ziliani/32/1821_2.png) [@beta-ziliani](https://forum.crystal-lang.org/u/beta-ziliani)\
**Post date:** [September 23, 2024, 1:56pm UTC](https://forum.crystal-lang.org/t/what-is-the-benefit-of-inlining-methods-with-yield/7194/13 "2024-09-23T13:56:08Z")

</div>

I’ve wondered in the past about merging procs and blocks by having blocks return a union type `(T | Break | Next | Return)`, and have the compiler build the boilerplate code to handle it. IIRC it gets complicated quite easily, and certainly wouldn’t be as performant, but it would be nice from the perspective of semantics “a block is just a proc with control operators”.
