From 92e3df411fb68bb6ab606675bcbd0d906bce4a3f Mon Sep 17 00:00:00 2001 From: Zed Date: Wed, 3 Jul 2019 07:18:19 +0200 Subject: [PATCH] Improve memory usage by making Thread a ref object --- src/api.nim | 2 ++ src/parser.nim | 1 + src/types.nim | 2 +- src/views/user.nimf | 6 +++--- 4 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/api.nim b/src/api.nim index 150401c..876f683 100644 --- a/src/api.nim +++ b/src/api.nim @@ -110,6 +110,7 @@ proc getVideo*(tweet: Tweet; token: string) {.async.} = tokenUses.inc proc getVideos*(thread: Thread; token="") {.async.} = + if thread == nil: return var gToken = token if gToken.len == 0: @@ -150,6 +151,7 @@ proc getPoll*(tweet: Tweet) {.async.} = tweet.poll = some(parsePoll(html)) proc getPolls*(thread: Thread) {.async.} = + if thread == nil: return var polls = thread.tweets.filterIt(it.poll.isSome) await all(polls.map(getPoll)) diff --git a/src/parser.nim b/src/parser.nim index 0f6d06b..17f514e 100644 --- a/src/parser.nim +++ b/src/parser.nim @@ -88,6 +88,7 @@ proc parseTweet*(node: XmlNode): Tweet = proc parseThread*(nodes: XmlNode): Thread = if nodes == nil: return + result = Thread() for n in nodes.filterIt(it.kind != xnText): let class = n.attr("class").toLower() if "tombstone" in class or "unavailable" in class: diff --git a/src/types.nim b/src/types.nim index 639aee2..82b745b 100644 --- a/src/types.nim +++ b/src/types.nim @@ -91,7 +91,7 @@ type photos*: seq[string] poll*: Option[Poll] - Thread* = object + Thread* = ref object tweets*: seq[Tweet] more*: int diff --git a/src/views/user.nimf b/src/views/user.nimf index 0aec49d..e7ce059 100644 --- a/src/views/user.nimf +++ b/src/views/user.nimf @@ -104,7 +104,7 @@ #proc renderConversation*(conversation: Conversation): string =
- #if conversation.before.tweets.len > 0: + #if conversation.before != nil:
#for i, tweet in conversation.before.tweets: ${renderTweet(tweet, first=(i == 0))} @@ -112,10 +112,10 @@
#end if
- #let afterClass = if conversation.after.tweets.len > 0: "thread thread-line" else: "" + #let afterClass = if conversation.after != nil: "thread thread-line" else: "" ${renderTweet(conversation.tweet, class=afterClass)}
- #if conversation.after.tweets.len > 0: + #if conversation.after != nil:
#for i, tweet in conversation.after.tweets: ${renderTweet(tweet, first=(i == 0), last=(i == conversation.after.tweets.high))}