Improve memory usage by making Thread a ref object

This commit is contained in:
Zed 2019-07-03 07:18:19 +02:00
parent 640bb2fadf
commit 92e3df411f
4 changed files with 7 additions and 4 deletions

View file

@ -110,6 +110,7 @@ proc getVideo*(tweet: Tweet; token: string) {.async.} =
tokenUses.inc tokenUses.inc
proc getVideos*(thread: Thread; token="") {.async.} = proc getVideos*(thread: Thread; token="") {.async.} =
if thread == nil: return
var gToken = token var gToken = token
if gToken.len == 0: if gToken.len == 0:
@ -150,6 +151,7 @@ proc getPoll*(tweet: Tweet) {.async.} =
tweet.poll = some(parsePoll(html)) tweet.poll = some(parsePoll(html))
proc getPolls*(thread: Thread) {.async.} = proc getPolls*(thread: Thread) {.async.} =
if thread == nil: return
var polls = thread.tweets.filterIt(it.poll.isSome) var polls = thread.tweets.filterIt(it.poll.isSome)
await all(polls.map(getPoll)) await all(polls.map(getPoll))

View file

@ -88,6 +88,7 @@ proc parseTweet*(node: XmlNode): Tweet =
proc parseThread*(nodes: XmlNode): Thread = proc parseThread*(nodes: XmlNode): Thread =
if nodes == nil: return if nodes == nil: return
result = Thread()
for n in nodes.filterIt(it.kind != xnText): for n in nodes.filterIt(it.kind != xnText):
let class = n.attr("class").toLower() let class = n.attr("class").toLower()
if "tombstone" in class or "unavailable" in class: if "tombstone" in class or "unavailable" in class:

View file

@ -91,7 +91,7 @@ type
photos*: seq[string] photos*: seq[string]
poll*: Option[Poll] poll*: Option[Poll]
Thread* = object Thread* = ref object
tweets*: seq[Tweet] tweets*: seq[Tweet]
more*: int more*: int

View file

@ -104,7 +104,7 @@
#proc renderConversation*(conversation: Conversation): string = #proc renderConversation*(conversation: Conversation): string =
<div class="conversation" id="tweets"> <div class="conversation" id="tweets">
<div class="main-thread"> <div class="main-thread">
#if conversation.before.tweets.len > 0: #if conversation.before != nil:
<div class="before-tweet thread-line"> <div class="before-tweet thread-line">
#for i, tweet in conversation.before.tweets: #for i, tweet in conversation.before.tweets:
${renderTweet(tweet, first=(i == 0))} ${renderTweet(tweet, first=(i == 0))}
@ -112,10 +112,10 @@
</div> </div>
#end if #end if
<div class="main-tweet"> <div class="main-tweet">
#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)} ${renderTweet(conversation.tweet, class=afterClass)}
</div> </div>
#if conversation.after.tweets.len > 0: #if conversation.after != nil:
<div class="after-tweet thread-line"> <div class="after-tweet thread-line">
#for i, tweet in conversation.after.tweets: #for i, tweet in conversation.after.tweets:
${renderTweet(tweet, first=(i == 0), last=(i == conversation.after.tweets.high))} ${renderTweet(tweet, first=(i == 0), last=(i == conversation.after.tweets.high))}