# Locked code in benchmarks

**URL:** <https://forum.crystal-lang.org/t/locked-code-in-benchmarks/5580>\
**Category:** Help & Support\
**Created:** [April 18, 2023, 4:12pm UTC](https://forum.crystal-lang.org/t/locked-code-in-benchmarks/5580 "2023-04-18T16:12:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![wontruefree](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/wontruefree/32/1467_2.png) [@wontruefree](https://forum.crystal-lang.org/u/wontruefree)\
**Post date:** [April 18, 2023, 4:12pm UTC](https://forum.crystal-lang.org/t/locked-code-in-benchmarks/5580/1 "2023-04-18T16:12:43Z")

</div>

I have some code that seems to work in practice, in testing, but not in benchmarks.

I am trying to understand why. When I put in print or do any type of debugging it appears I will get multiple test runs but code will get stuck before completing enough runs to generate a benchmark. I am not an experienced MT programmer but I am hoping this is a concept I have not learned yet.

Here is an extract of the code that is not working:

```auto
require "benchmark"

def locking_code(arr)
  channel = Channel(Int64).new

  spawn do
    acc = 0_i64
    index = 0

    while index < arr.size
      acc += arr[index]
      index += 2
    end

    channel.send(acc)
  end

  spawn do
    acc = 0_i64
    index = 1

    while index < arr.size
      acc += arr[index]
      index += 2
    end

    channel.send(acc)
  end

  channel.receive + channel.receive
end

arr = (1_i64..100_000_i64).to_a

Benchmark.ips do |ips|
  ips.report("locking") { locking_code(arr) }
end

```

I am running this on an M2 MacBook pro

```auto
Darwin jbook.local 22.4.0 Darwin Kernel Version 22.4.0: Mon Mar 6 20:59:58 PST 2023; root:xnu-8796.101.5~3/RELEASE_ARM64_T6020 arm64 arm Darwin

```

building the binary with

```auto
crystal build -Dpreview_mt --release benchmarks/locking_code.cr -o bin/locking

```

running with

```auto
CRYSTAL_WORKERS=8 bin/locking

```

Also weirdly using `Fiber.yield` works for me. it has poor performance so I was trying channels. Which is how I got down this path. If anyone can explain `Fiber.yield` vs channel performance I would be really interested.

---

<div class="post-metadata">

**Author:** ![rob](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/rob/32/1409_2.png) [@rob](https://forum.crystal-lang.org/u/rob)\
**Post date:** [April 24, 2023, 12:59pm UTC](https://forum.crystal-lang.org/t/locked-code-in-benchmarks/5580/2 "2023-04-24T12:59:56Z")

</div>

I’m not sure I can get behind the idea of benchmarking anything that locks — or anything that could get blocked by an external system at all. Wouldn’t any blocking that occurs render the benchmark pointless?

It would seem to me that you could separate your algorithm from the mutex concerns. Then benchmark the algorithm and tune it for performance in a single thread environment.

---

<div class="post-metadata">

**Author:** ![zw963](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/zw963/32/1623_2.png) [@zw963](https://forum.crystal-lang.org/u/zw963)\
**Post date:** [April 24, 2023, 1:52pm UTC](https://forum.crystal-lang.org/t/locked-code-in-benchmarks/5580/3 "2023-04-24T13:52:13Z")

</div>

Following your’s process, it output like this:

```sh
 ╰─ $ CRYSTAL_WORKERS=8 ./locking
locking 11.89k ( 84.09µs) (± 7.85%) 432B/op fastest

```

It works for me? ooes it make sense?

```auto
Crystal 1.8.0 [14bfa992e] (2023-04-14)

LLVM: 15.0.7
Default target: x86_64-pc-linux-gnu

```

---

<div class="post-metadata">

**Author:** ![HertzDevil](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/hertzdevil/32/1023_2.png) [@HertzDevil](https://forum.crystal-lang.org/u/HertzDevil)\
**Post date:** [April 24, 2023, 2:05pm UTC](https://forum.crystal-lang.org/t/locked-code-in-benchmarks/5580/4 "2023-04-24T14:05:27Z")

</div>

If it only reproduces on AArch64, it might be some variant of [Bug in atomic operations on aarch64 with multi-threading · Issue #13010 · crystal-lang/crystal · GitHub](https://github.com/crystal-lang/crystal/issues/13010) or [More places might need memory barriers on AArch64 · Issue #13055 · crystal-lang/crystal · GitHub](https://github.com/crystal-lang/crystal/issues/13055)

---

<div class="post-metadata">

**Author:** ![wontruefree](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/wontruefree/32/1467_2.png) [@wontruefree](https://forum.crystal-lang.org/u/wontruefree)\
**Post date:** [April 29, 2023, 6:13am UTC](https://forum.crystal-lang.org/t/locked-code-in-benchmarks/5580/5 "2023-04-29T06:13:35Z")

</div>

This is good to know. Honestly with MT I should check multiple architectures.
