diff options
| author | Fuwn <[email protected]> | 2026-04-17 17:13:51 +0000 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2026-04-17 17:13:51 +0000 |
| commit | 6089ff1b9fc921d6aa85287ee52abae951b1bfe0 (patch) | |
| tree | 8184df13e7707fe8469f47f2645a6ea42797d243 /src/svg.gleam | |
| parent | chore(gleam.toml): Bump version (diff) | |
| download | mayu-6089ff1b9fc921d6aa85287ee52abae951b1bfe0.tar.xz mayu-6089ff1b9fc921d6aa85287ee52abae951b1bfe0.zip | |
perf: Reduce per-request work and I/O in render pipeline
Diffstat (limited to 'src/svg.gleam')
| -rw-r--r-- | src/svg.gleam | 18 |
1 files changed, 7 insertions, 11 deletions
diff --git a/src/svg.gleam b/src/svg.gleam index 0178dbd..f6ab4a4 100644 --- a/src/svg.gleam +++ b/src/svg.gleam @@ -2,15 +2,15 @@ import cache import gleam/int import gleam/list import gleam/option.{Some} -import gleam/string_builder +import gleam/string_builder.{type StringBuilder} import image type XmlImages { - XmlImages(xml: String, width: Int, height: Int) + XmlImages(xml: StringBuilder, width: Int, height: Int) } -fn image(base64, image: image.ImageInformation, width) { - string_builder.new() +fn append_image(svgs, base64, image: image.ImageInformation, width) { + svgs |> string_builder.append("<image height=\"") |> string_builder.append(int.to_string(image.height)) |> string_builder.append("\" width=\"") @@ -22,12 +22,11 @@ fn image(base64, image: image.ImageInformation, width) { |> string_builder.append(";base64,") |> string_builder.append(base64) |> string_builder.append("\"/>") - |> string_builder.to_string() } fn images(image_cache, theme, digits, width, height, svgs) { case digits { - [] -> XmlImages(string_builder.to_string(svgs), width, height) + [] -> XmlImages(svgs, width, height) [digit, ..rest] -> case cache.get_image(image_cache, theme, digit) { Some(cached) -> @@ -37,10 +36,7 @@ fn images(image_cache, theme, digits, width, height, svgs) { rest, width + cached.info.width, int.max(height, cached.info.height), - string_builder.append( - svgs, - image(cached.base64, cached.info, width), - ), + append_image(svgs, cached.base64, cached.info, width), ) _ -> images(image_cache, theme, rest, width, height, svgs) } @@ -78,7 +74,7 @@ pub fn xml(image_cache, theme, number, padding) { |> string_builder.append( "\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><title>Mayu</title><g>", ) - |> string_builder.append(xml.xml) + |> string_builder.append_builder(xml.xml) |> string_builder.append("</g></svg>") |> string_builder.to_string() } |