From 85316f8f8d265ea4449e7166c1f6651fb595c41d Mon Sep 17 00:00:00 2001 From: Zed Date: Sun, 27 Nov 2022 15:50:08 +0100 Subject: [PATCH] Reduce usage of strformat, minor perf improvement --- src/formatters.nim | 4 ++-- src/routes/rss.nim | 51 ++++++++++++++++++++++------------------- src/routes/search.nim | 4 ++-- src/routes/timeline.nim | 4 ++-- src/views/general.nim | 2 +- src/views/profile.nim | 2 +- 6 files changed, 36 insertions(+), 31 deletions(-) diff --git a/src/formatters.nim b/src/formatters.nim index 7b939ca..87ff647 100644 --- a/src/formatters.nim +++ b/src/formatters.nim @@ -58,11 +58,11 @@ proc replaceUrls*(body: string; prefs: Prefs; absolute=""): string = result = result.replace(ytRegex, prefs.replaceYouTube) if prefs.replaceTwitter.len > 0 and ("twitter.com" in body or tco in body): - result = result.replace(tco, &"{https}{prefs.replaceTwitter}/t.co") + result = result.replace(tco, https & prefs.replaceTwitter & "/t.co") result = result.replace(cards, prefs.replaceTwitter & "/cards") result = result.replace(twRegex, prefs.replaceTwitter) result = result.replacef(twLinkRegex, a( - prefs.replaceTwitter & "$2", href = &"{https}{prefs.replaceTwitter}$1")) + prefs.replaceTwitter & "$2", href = https & prefs.replaceTwitter & "$1")) if prefs.replaceReddit.len > 0 and ("reddit.com" in result or "redd.it" in result): result = result.replace(rdShortRegex, prefs.replaceReddit & "/comments/") diff --git a/src/routes/rss.nim b/src/routes/rss.nim index 2b23d0a..5da29b0 100644 --- a/src/routes/rss.nim +++ b/src/routes/rss.nim @@ -10,6 +10,11 @@ include "../views/rss.nimf" export times, hashes +proc redisKey*(page, name, cursor: string): string = + result = page & ":" & name + if cursor.len > 0: + result &= ":" & cursor + proc timelineRss*(req: Request; cfg: Config; query: Query): Future[Rss] {.async.} = var profile: Profile let @@ -42,8 +47,8 @@ proc timelineRss*(req: Request; cfg: Config; query: Query): Future[Rss] {.async. template respRss*(rss, page) = if rss.cursor.len == 0: let info = case page - of "User": &""" "{@"name"}" """ - of "List": &""" "{@"id"}" """ + of "User": " \"" & @"name" & "\" " + of "List": " \"" & @"id" & "\" " else: " " resp Http404, showError(page & info & "not found", cfg) @@ -67,7 +72,7 @@ proc createRssRouter*(cfg: Config) = let cursor = getCursor() - key = &"search:{hash(genQueryUrl(query))}:cursor" + key = redisKey("search", $hash(genQueryUrl(query)), cursor) var rss = await getCachedRss(key) if rss.cursor.len > 0: @@ -84,9 +89,8 @@ proc createRssRouter*(cfg: Config) = cond cfg.enableRss cond '.' notin @"name" let - cursor = getCursor() name = @"name" - key = &"twitter:{name}:{cursor}" + key = redisKey("twitter", name, getCursor()) var rss = await getCachedRss(key) if rss.cursor.len > 0: @@ -101,18 +105,20 @@ proc createRssRouter*(cfg: Config) = cond cfg.enableRss cond '.' notin @"name" cond @"tab" in ["with_replies", "media", "search"] - let name = @"name" - let query = - case @"tab" - of "with_replies": getReplyQuery(name) - of "media": getMediaQuery(name) - of "search": initQuery(params(request), name=name) - else: Query(fromUser: @[name]) + let + name = @"name" + tab = @"tab" + query = + case tab + of "with_replies": getReplyQuery(name) + of "media": getMediaQuery(name) + of "search": initQuery(params(request), name=name) + else: Query(fromUser: @[name]) - var key = &"""{@"tab"}:{@"name"}:""" - if @"tab" == "search": - key &= $hash(genQueryUrl(query)) & ":" - key &= getCursor() + let searchKey = if tab != "search": "" + else: ":" & $hash(genQueryUrl(query)) + + let key = redisKey(tab, name & searchKey, getCursor()) var rss = await getCachedRss(key) if rss.cursor.len > 0: @@ -132,28 +138,27 @@ proc createRssRouter*(cfg: Config) = cursor = getCursor() if list.id.len == 0: - resp Http404, showError(&"""List "{@"slug"}" not found""", cfg) + resp Http404, showError("List \"" & @"slug" & "\" not found", cfg) - let url = &"/i/lists/{list.id}/rss" + let url = "/i/lists/" & list.id & "/rss" if cursor.len > 0: - redirect(&"{url}?cursor={encodeUrl(cursor, false)}") + redirect(url & "?cursor=" & encodeUrl(cursor, false)) else: redirect(url) get "/i/lists/@id/rss": cond cfg.enableRss let + id = @"id" cursor = getCursor() - key = - if cursor.len == 0: "lists:" & @"id" - else: &"""lists:{@"id"}:{cursor}""" + key = redisKey("lists", id, cursor) var rss = await getCachedRss(key) if rss.cursor.len > 0: respRss(rss, "List") let - list = await getCachedList(id=(@"id")) + list = await getCachedList(id=id) timeline = await getListTimeline(list.id, cursor) rss.cursor = timeline.bottom rss.feed = renderListRss(timeline.content, list, cfg) diff --git a/src/routes/search.nim b/src/routes/search.nim index 554f2f6..3fc44a9 100644 --- a/src/routes/search.nim +++ b/src/routes/search.nim @@ -1,5 +1,5 @@ # SPDX-License-Identifier: AGPL-3.0-only -import strutils, strformat, uri +import strutils, uri import jester @@ -37,7 +37,7 @@ proc createSearchRouter*(cfg: Config) = resp Http404, showError("Invalid search", cfg) get "/hashtag/@hash": - redirect(&"""/search?q={encodeUrl("#" & @"hash")}""") + redirect("/search?q=" & encodeUrl("#" & @"hash")) get "/opensearch": let url = getUrlPrefix(cfg) & "/search?q=" diff --git a/src/routes/timeline.nim b/src/routes/timeline.nim index 9d97c29..a0a6e21 100644 --- a/src/routes/timeline.nim +++ b/src/routes/timeline.nim @@ -1,5 +1,5 @@ # SPDX-License-Identifier: AGPL-3.0-only -import asyncdispatch, strutils, strformat, sequtils, uri, options, times +import asyncdispatch, strutils, sequtils, uri, options, times import jester, karax/vdom import router_utils @@ -102,7 +102,7 @@ proc showTimeline*(request: Request; query: Query; cfg: Config; prefs: Prefs; template respTimeline*(timeline: typed) = let t = timeline if t.len == 0: - resp Http404, showError(&"""User "{@"name"}" not found""", cfg) + resp Http404, showError("User \"" & @"name" & "\" not found", cfg) resp t template respUserId*() = diff --git a/src/views/general.nim b/src/views/general.nim index f242e66..b18dae5 100644 --- a/src/views/general.nim +++ b/src/views/general.nim @@ -81,7 +81,7 @@ proc renderHead*(prefs: Prefs; cfg: Config; req: Request; titleText=""; desc=""; title: if titleText.len > 0: - text &"{titleText}|{cfg.title}" + text titleText & " | " & cfg.title else: text cfg.title diff --git a/src/views/profile.nim b/src/views/profile.nim index 73ce59d..2a007b1 100644 --- a/src/views/profile.nim +++ b/src/views/profile.nim @@ -108,7 +108,7 @@ proc renderProfile*(profile: var Profile; prefs: Prefs; path: string): VNode = renderBanner(profile.user.banner) let sticky = if prefs.stickyProfile: " sticky" else: "" - tdiv(class=(&"profile-tab{sticky}")): + tdiv(class=("profile-tab" & sticky)): renderUserCard(profile.user, prefs) if profile.photoRail.len > 0: renderPhotoRail(profile)