# Crystal string build failure

**URL:** <https://forum.crystal-lang.org/t/crystal-string-build-failure/7184>\
**Category:** Community\
**Created:** [September 17, 2024, 12:28am UTC](https://forum.crystal-lang.org/t/crystal-string-build-failure/7184 "2024-09-17T00:28:06Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![dsisnero](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/dsisnero/32/1843_2.png) [@dsisnero](https://forum.crystal-lang.org/u/dsisnero)\
**Post date:** [September 17, 2024, 12:28am UTC](https://forum.crystal-lang.org/t/crystal-string-build-failure/7184/1 "2024-09-17T00:28:07Z")

</div>

I was reading this post that mentioned this language comparison  
[Evaluating Language Performance when Handling Extensive String Constructions](https://github.com/shagrouni/langs_string_build_test/tree/main) the crystal program fails for large buffers and can’t see why.

---

<div class="post-metadata">

**Author:** ![yxhuvud](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/yxhuvud/32/27_2.png) [@yxhuvud](https://forum.crystal-lang.org/u/yxhuvud)\
**Post date:** [September 17, 2024, 6:05am UTC](https://forum.crystal-lang.org/t/crystal-string-build-failure/7184/2 "2024-09-17T06:05:49Z")

</div>

How large are your large buffers? Are you hitting the `Int32::MAX` size limit on strings?

---

<div class="post-metadata">

**Author:** ![dsisnero](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/dsisnero/32/1843_2.png) [@dsisnero](https://forum.crystal-lang.org/u/dsisnero)\
**Post date:** [September 17, 2024, 2:55pm UTC](https://forum.crystal-lang.org/t/crystal-string-build-failure/7184/3 "2024-09-17T14:55:38Z")

</div>

Not my code - saw a blog post about speed comparision on bufer building and clicked link.  
200,000,000 iterations, 2.13 GB text size is when it fails. Also, not as fast as the other implementations

| Language | Time | Notes |
| --- | --- | --- |
| Rust | 00:01:625 | |
| Java | 00:01:77 | Failed to save |
| C# | 00:02:89 | Failed to save |
| Nim | 00:03:89 | |
| C++ | 00:04:14 | |
| C | 00:04:41 | |
| Perl | 00:04:497 | |
| Crystal | 00:05:90 | |

```auto
require "time"

def test(num : Int64)
  
  print " Crystal"
    
    start_time = Time.monotonic

    s = String.build do |str|
    
    (1..num).each do |i|
      str << " C #{i}"
    end

    elapsed_time = Time.monotonic - start_time
    
    mins = elapsed_time.total_minutes.floor
    secs = elapsed_time.total_seconds.floor % 60
    millis = elapsed_time.total_milliseconds % 1000
    
    puts " #{mins}:#{secs}:#{millis} Iter #{num} Len #{str.bytesize} "
     
 
    File.write("out/crystal_output.txt", str.to_s) rescue puts "Error saving string to file."

  end
end

num = ARGV[0].to_i
test(num)

```

---

<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:** [September 17, 2024, 3:33pm UTC](https://forum.crystal-lang.org/t/crystal-string-build-failure/7184/4 "2024-09-17T15:33:27Z")

</div>

The code does not actually work in the first place:

```auto
  Crystal 0.0:2.0:662.8273509999999 Iter 50000000 Len 538888897 
Unhandled exception: Can only invoke 'to_s' once on String::Builder (Exception)
  from src/string/builder.cr:101:5 in 'to_s'
  from usr/test.cr:96:1 in '__crystal_main'
  from src/crystal/main.cr:118:5 in 'main'
  from /lib/x86_64-linux-gnu/libc.so.6 in '??'
  from /lib/x86_64-linux-gnu/libc.so.6 in '__libc_start_main'
  from ./test in '_start'
  from ???

```

Even if it worked, it would indeed fail to save because `String` only handles byte sizes up to around `Int32::MAX`.

---

<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 17, 2024, 3:41pm UTC](https://forum.crystal-lang.org/t/crystal-string-build-failure/7184/5 "2024-09-17T15:41:41Z")

</div>

Yeah the Crystal code is broken. And the comparison is completely flawed.  
Some implementations (including Crystal’s) construct an intermediary string in the loop, others don’t (Java and Rust for example).

The difference in Crystal is that `str << " C #{i}"` builds an intermediary before appending to `str` , while `str << " C " << i` does not (so less allocations) and appends directly.

---

<div class="post-metadata">

**Author:** ![dsisnero](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/dsisnero/32/1843_2.png) [@dsisnero](https://forum.crystal-lang.org/u/dsisnero)\
**Post date:** [September 17, 2024, 4:01pm UTC](https://forum.crystal-lang.org/t/crystal-string-build-failure/7184/6 "2024-09-17T16:01:09Z")

</div>

```auto
# crystal build -o ..\exe\cr_strbld.exe --release cr_strbld.cr

require "time"

def test(num : Int64)
  
  print " Crystal"
    
    start_time = Time.monotonic

    s = String.build do |str|
    
    (1..num).each do |i|
      str << " C #{i}"
    end

    elapsed_time = Time.monotonic - start_time
    
    mins = elapsed_time.total_minutes.floor
    secs = elapsed_time.total_seconds.floor % 60
    millis = elapsed_time.total_milliseconds % 1000
    
    puts " #{mins}:#{secs}:#{millis} Iter #{num} Len #{str.bytesize} "
     
 
    File.write("out/crystal_output.txt", str.to_s) rescue puts "Error saving string to file."

  end
end

num = ARGV[0].to_i
test(num)

```

---

<div class="post-metadata">

**Author:** ![dsisnero](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/dsisnero/32/1843_2.png) [@dsisnero](https://forum.crystal-lang.org/u/dsisnero)\
**Post date:** [September 17, 2024, 4:07pm UTC](https://forum.crystal-lang.org/t/crystal-string-build-failure/7184/7 "2024-09-17T16:07:05Z")

</div>

I got it to compile on my windows machine - cloned the repo and compiled it . To make it the same as Rust - how would you get rid of the allocations - or is it possible? Every time on interpolation it creates a String.

---

<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 17, 2024, 4:17pm UTC](https://forum.crystal-lang.org/t/crystal-string-build-failure/7184/8 "2024-09-17T16:17:54Z")

</div>

It’s a simple change:

```diff
- str << " C #{i}"
+ str << " C " << i

```

Now we don’t have an intermediate string `" C #{i}"` anymore, `" C "` and `i` are appended directly to `str`.  
This should cut the execution time almost in half.

---

<div class="post-metadata">

**Author:** ![dsisnero](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/dsisnero/32/1843_2.png) [@dsisnero](https://forum.crystal-lang.org/u/dsisnero)\
**Post date:** [September 17, 2024, 8:50pm UTC](https://forum.crystal-lang.org/t/crystal-string-build-failure/7184/9 "2024-09-17T20:50:40Z")

</div>

Sorry - it is broken but not sure why. Can someone explain the error and how to fix it?

---

<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 17, 2024, 10:28pm UTC](https://forum.crystal-lang.org/t/crystal-string-build-failure/7184/10 "2024-09-17T22:28:57Z")

</div>

I made the optimization @straight-shoota mentioned and converted the counter variable to a `UInt64` and now it’s performing much closer to the Rust code — which also has that same optimization. Down from 5.357s to 1.344s on 100M. I also made some other edits to make it more idiomatic Crystal code.

```plaintext
➜ langs_string_build_test git:(main) ✗ for lang in c rs cr; do; exe/${lang}_strbld.exe 100000000; done
  C 0:5:5115 Iter 100000000: Len 1088888898
  Rust 00:01:211 iter 100000000 len 1088888898
  Crystal 00:01:344 iter 100000000 len 1088888898

```

Assuming the ratio of the Rust implementation’s performance on my machine vs theirs also applies to the Crystal implementation, that puts it at roughly 1.803s on their machine, making it the second-fastest unbroken implementation for the 100M table.

The code:

```crystal
# crystal build -o ..\exe\cr_strbld.exe --release cr_strbld.cr

require "time"

def test(num : UInt64)
  print " Crystal"

  start_time = Time.monotonic

  s = String.build do |str|
    (1u64..num).each do |i|
      str << " C " << i
    end
  end

  elapsed_time = Time.monotonic - start_time

  mins = elapsed_time.total_minutes.floor.to_i
  secs = elapsed_time.total_seconds.floor % 60
  millis = elapsed_time.total_milliseconds % 1000

  puts " %02d:%02d:%03d iter #{num} len #{s.bytesize} " % {mins, secs, millis}

  begin
    File.write("out/crystal_output.txt", s)
  rescue ex
    puts "Error saving string to file: #{ex}"
  end
end

num = ARGV[0].to_u64
test(num)

```

I have a feeling the performance difference may be in stringifying the integer. I haven’t checked, though.

---

<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 17, 2024, 10:48pm UTC](https://forum.crystal-lang.org/t/crystal-string-build-failure/7184/11 "2024-09-17T22:48:28Z")

</div>

One thing I found hilarious was the project’s readme says:

> To ensure a fair comparison, I avoided using any optimization tricks or advanced features that could enhance the programs’ performance, relying solely on the default language features.

And then it has [this optimization](https://github.com/shagrouni/langs_string_build_test/commit/2eb2699c5822b1463728566503663e815b046b10#diff-406b5e160cc45ef4fbb88df3c9918d76fb71f1b2fcf0b9e10767d0dc58d683e5L25-R29) only applied to the Rust implementation, which just happens to be the most performant in their benchmarks:

```diff
diff --git a/src/rs_strbld.rs b/src/rs_strbld.rs
index 3e6c2da..084b501 100644
--- a/src/rs_strbld.rs
+++ b/src/rs_strbld.rs
@@ -3,6 +3,7 @@ use std::fs;
 use std::time::Instant;
 use std::env;
 use std::io::{self, Write};
+use std::fmt::Write as _;
 
 fn format_time(time_ms: u128) -> String {
   let milliseconds = time_ms % 1000;
@@ -22,7 +23,10 @@ pub fn test(num: i64) {
      
     for _ in 1..=num {
         i += 1;
- s.push_str(&format!(" R {}", i)); 
+ //s.push_str(&format!(" R {}", i)); 
+ s.push_str(" R "); 
+ write!(s, "{}", i).unwrap();
+

```

---

<div class="post-metadata">

**Author:** ![kojix2](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/kojix2/32/1583_2.png) [@kojix2](https://forum.crystal-lang.org/u/kojix2)\
**Post date:** [September 18, 2024, 12:26am UTC](https://forum.crystal-lang.org/t/crystal-string-build-failure/7184/12 "2024-09-18T00:26:15Z")

</div>

That’s interesting.  
It’s impossible for one person to fully understand the details of 20 programming languages, so comparing languages is always hard. Such comparisons only make sense after community members of each language review and make optimizations. A very human way to go about it.

---

<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 18, 2024, 7:28am UTC](https://forum.crystal-lang.org/t/crystal-string-build-failure/7184/13 "2024-09-18T07:28:47Z")

</div>

Rust is not the only one. At least Java avoids the intermediary allocation as well: `s.append(" J ").append(i);`.  
It’s almost as fast as the Rust implementation.

---

<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:** [September 18, 2024, 2:40pm UTC](https://forum.crystal-lang.org/t/crystal-string-build-failure/7184/14 "2024-09-18T14:40:59Z")

</div>

Create a PR [there](https://github.com/shagrouni/langs_string_build_test/pull/4)

Anyway, there are no drawbacks let new user know Crystal faster.

---

<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:** [September 18, 2024, 6:01pm UTC](https://forum.crystal-lang.org/t/crystal-string-build-failure/7184/15 "2024-09-18T18:01:40Z")

</div>

Try to change code to works on bigger number as some others programming language do, but following code not work.

```auto
require "time"

class Iter
  include Iterator(UInt64)

  def initialize(@num : UInt64)
    @produced = 0
  end

  def next
    if @produced < @num
      @produced &+= 1
      @produced
    else
      stop
    end
  end
end

num = 200_000_000_u64

file = File.open("crystal_output.txt", "w")

def test(iter : Iter)
  number = 0

  str = String.build do |io|
    loop do
      number = iter.next

      io << " C " << number
    end
  end

  {str, ""}
rescue ex : IO::EOFError
  pp!({str, number})
  {str, number}
end

iter = Iter.new(num)

begin
  loop do
    test(iter).each do |e|
      file << e
    end
  end
end

file.close

```

When String buffer reach it max limit(Int32::MAX), the `IO::EOFError` exception raised, when this exception happen, what i expect is, String.build still build and return the string, but it return `nil` instead.

I can’t figure out others way to achieve this, unless call `to_s` on every `i`, then counting the string number, make the `String.build` always less than limit, but that fallback to the initial question, we don’t want create new string …

Any idea?

---

<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 19, 2024, 12:58am UTC](https://forum.crystal-lang.org/t/crystal-string-build-failure/7184/16 "2024-09-19T00:58:18Z")

</div>

> [@straight-shoota](#):
>
> Rust is not the only one. At least Java avoids the intermediary allocation as well: `s.append(" J ").append(i);`.  
> It’s almost as fast as the Rust implementation.

It does, but the Java implementation had it from the beginning. They went back and optimized the Rust implementation later. I was linking to a diff with an optimization in it.
