Skip to content

Commit

Permalink
perf: multi-threading word rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
guo-yong-zhi committed Oct 14, 2024
1 parent ecbb920 commit 69ea810
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
10 changes: 5 additions & 5 deletions src/strategy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,13 @@ function wordsoccupancy!(wc)
sizemax = size(wc.mask) .* (getparameter(wc, :volume) / prod(size(wc.mask))) .* 0.9
check = getparameter(wc, :maxfontsize0) == :auto
check && setparameter!(wc, minimum(sizemax), :maxfontsize)
imgs = []
imgs = Vector(undef, length(words))
for i in 1:3
empty!(imgs)
fontsizes = getfontsizes(wc)
success = true
for (c, sz, ft, θ) in zip(words, fontsizes, fonts, angles)
Threads.@threads for j in 1:length(words)
success || break
c, sz, ft, θ = words[j], fontsizes[j], fonts[j], angles[j]
img = Render.rendertext(string(c), sz, backgroundcolor=(0, 0, 0, 0), font=ft, border=border)
a, b = size(img)
imsz = max(a*abs(cos(θ)), b*abs(sin(θ))), max(a*abs(sin(θ)), b*abs(cos(θ)))
Expand All @@ -70,10 +71,9 @@ function wordsoccupancy!(wc)
setparameter!(wc, mfz, :maxfontsize)
println("The word \"$c\"($sz) is too big. Set maxfontsize = $mfz.")
success = false
break
end
end
push!(imgs, img)
imgs[j] = img
end
success && break
end
Expand Down
2 changes: 1 addition & 1 deletion src/wc-method.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function initialize!(wc, i::Integer; backgroundcolor=(0, 0, 0, 0), spacing=getpa
initqtree!(wc, i, backgroundcolor=backgroundcolor, spacing=spacing)
nothing
end
initialize!(wc, i; kargs...) = initialize!.(wc, index(wc, i); kargs...)
initialize!(wc, ind; kargs...) = fetch.([Threads.@spawn initialize!(wc, i; kargs...) for i in WordCloud.index(wc, ind)]);
function initialize!(wc::WC; maxiter=5, tolerance=0.02)
params = wc.params
weights = wc.weights
Expand Down

0 comments on commit 69ea810

Please sign in to comment.