From 380c324623f2c47bd2ca4843145347f82ec76d31 Mon Sep 17 00:00:00 2001 From: Spencer Brower <6729162+sbrow@users.noreply.github.com> Date: Tue, 4 Aug 2026 15:35:30 -0400 Subject: [PATCH] perf: Replaced `fmt.aprintf`/`fmt.tprintf` calls with `fmt.sbprintf`. --- feed.odin | 45 +++++++++++++++++----------------------- mustache/diagnostic.odin | 2 +- mustache/format.odin | 24 ++++++++++----------- 3 files changed, 32 insertions(+), 39 deletions(-) diff --git a/feed.odin b/feed.odin index 0dc4263..250fbad 100644 --- a/feed.odin +++ b/feed.odin @@ -7,10 +7,9 @@ import "core:time" generate_rss :: proc(site: ^Site) -> string { sb := strings.builder_make() - strings.write_string( + fmt.sbprintf( &sb, - fmt.aprintf( - ` + ` %s @@ -18,11 +17,10 @@ generate_rss :: proc(site: ^Site) -> string { %s en-us `, - xml_escape(site.title), - site.base_url, - xml_escape(site.description), - site.base_url, - ), + xml_escape(site.title), + site.base_url, + xml_escape(site.description), + site.base_url, ) for page in site.pages { @@ -35,10 +33,9 @@ generate_rss :: proc(site: ^Site) -> string { pub_date = format_rfc822(page.date) } - strings.write_string( + fmt.sbprintf( &sb, - fmt.aprintf( - ` + ` %s %s %s @@ -46,12 +43,11 @@ generate_rss :: proc(site: ^Site) -> string { %s `, - xml_escape(page.title), - page.url, - pub_date, - page.url, - xml_escape(page.content), - ), + xml_escape(page.title), + page.url, + pub_date, + page.url, + xml_escape(page.content), ) } @@ -70,11 +66,11 @@ generate_sitemap :: proc(site: ^Site) -> string { ) for page in site.pages { - lastmod := "" + fmt.sbprintf(&sb, "%s", page.url) if page.date != "" { - lastmod = fmt.aprintf("%s", page.date) + fmt.sbprintf(&sb, "%s", page.date) } - strings.write_string(&sb, fmt.aprintf("%s%s\n", page.url, lastmod)) + fmt.sbprintf(&sb, "\n") } // Section index pages (for sections without an index in content) @@ -103,14 +99,11 @@ generate_sitemap :: proc(site: ^Site) -> string { section_lastmod = page.date } } - lm := "" + fmt.sbprintf(&sb, "%s/%s/", site.base_url, section) if section_lastmod != "" { - lm = fmt.aprintf("%s", section_lastmod) + fmt.sbprintf(&sb, "%s", section_lastmod) } - strings.write_string( - &sb, - fmt.aprintf("%s/%s/%s\n", site.base_url, section, lm), - ) + fmt.sbprintf(&sb, "\n") } strings.write_string(&sb, "") diff --git a/mustache/diagnostic.odin b/mustache/diagnostic.odin index c5ef789..9eb0894 100644 --- a/mustache/diagnostic.odin +++ b/mustache/diagnostic.odin @@ -227,7 +227,7 @@ format_error :: proc( } strings.write_string(&sb, "--> ") strings.write_string(&sb, reset) - strings.write_string(&sb, fmt.tprintf("%s:%d:%d\n", path, line, col)) + fmt.sbprintf(&sb, "%s:%d:%d\n", path, line, col) // Top gutter line. write_gutter(&sb, width, faint, reset) diff --git a/mustache/format.odin b/mustache/format.odin index ad67ce6..48296a2 100644 --- a/mustache/format.odin +++ b/mustache/format.odin @@ -129,12 +129,12 @@ match_token :: proc(b: ^strings.Builder, dt: Date_Components, s: string) -> int if strings.has_prefix( s, "January", - ) {strings.write_string(b, fmt.tprintf("%s", time.Month(dt.month))); return 7} + ) {fmt.sbprintf(b, "%s", time.Month(dt.month)); return 7} if strings.has_prefix(s, "Monday") {emit_weekday(b, dt, full = true); return 6} if strings.has_prefix( s, "2006", - ) {strings.write_string(b, fmt.tprintf("%04d", dt.year)); return 4} + ) {fmt.sbprintf(b, "%04d", dt.year); return 4} if strings.has_prefix(s, "MST") { abbr := dt.tz_abbr if len(abbr) == 0 do abbr = "UTC" @@ -146,37 +146,37 @@ match_token :: proc(b: ^strings.Builder, dt: Date_Components, s: string) -> int if strings.has_prefix( s, "06", - ) {strings.write_string(b, fmt.tprintf("%02d", dt.year % 100)); return 2} - if strings.has_prefix(s, "02") {strings.write_string(b, fmt.tprintf("%02d", dt.day)); return 2} + ) {fmt.sbprintf(b, "%02d", dt.year % 100); return 2} + if strings.has_prefix(s, "02") {fmt.sbprintf(b, "%02d", dt.day); return 2} if strings.has_prefix( s, "15", - ) {strings.write_string(b, fmt.tprintf("%02d", dt.hour)); return 2} + ) {fmt.sbprintf(b, "%02d", dt.hour); return 2} if strings.has_prefix( s, "04", - ) {strings.write_string(b, fmt.tprintf("%02d", dt.minute)); return 2} + ) {fmt.sbprintf(b, "%02d", dt.minute); return 2} if strings.has_prefix( s, "05", - ) {strings.write_string(b, fmt.tprintf("%02d", dt.second)); return 2} + ) {fmt.sbprintf(b, "%02d", dt.second); return 2} if strings.has_prefix( s, "01", - ) {strings.write_string(b, fmt.tprintf("%02d", dt.month)); return 2} + ) {fmt.sbprintf(b, "%02d", dt.month); return 2} if strings.has_prefix(s, "03") {emit_hour_12(b, dt, pad = true); return 2} if strings.has_prefix(s, "PM") {emit_am_pm(b, dt); return 2} if strings.has_prefix(s, "pm") {emit_am_pm_lower(b, dt); return 2} if len(s) >= 1 { switch s[0] { case '2': - strings.write_string(b, fmt.tprintf("%d", dt.day)); return 1 + fmt.sbprintf(b, "%d", dt.day); return 1 case '1': - strings.write_string(b, fmt.tprintf("%d", dt.month)); return 1 + fmt.sbprintf(b, "%d", dt.month); return 1 case '4': - strings.write_string(b, fmt.tprintf("%d", dt.minute)); return 1 + fmt.sbprintf(b, "%d", dt.minute); return 1 case '5': - strings.write_string(b, fmt.tprintf("%d", dt.second)); return 1 + fmt.sbprintf(b, "%d", dt.second); return 1 case '3': emit_hour_12(b, dt, pad = false); return 1 case: