Skip to content

Commit d8996a9

Browse files
yoshi-monsterlpil
authored andcommitted
improve performance of string.concat
1 parent 16657db commit d8996a9

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
- The `debug` function in the `io` module has been deprecated in favour of
66
the `echo` keyword.
7-
- The performance of `string.append` has been improved.
7+
- The performance of `string.append` and `string.concat` have been improved.
88

99
## v0.58.0 - 2025-03-23
1010

src/gleam/string.gleam

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -387,10 +387,16 @@ pub fn append(to first: String, suffix second: String) -> String {
387387
/// // -> "nevertheless"
388388
/// ```
389389
///
390+
@external(erlang, "erlang", "list_to_binary")
390391
pub fn concat(strings: List(String)) -> String {
391-
strings
392-
|> string_tree.from_strings
393-
|> string_tree.to_string
392+
do_concat(strings, "")
393+
}
394+
395+
fn do_concat(strings: List(String), accumulator: String) -> String {
396+
case strings {
397+
[string, ..strings] -> do_concat(strings, accumulator <> string)
398+
[] -> accumulator
399+
}
394400
}
395401

396402
/// Creates a new `String` by repeating a `String` a given number of times.

0 commit comments

Comments
 (0)