From 9b9cf718fa7a1d4688756c1a2bec09e8eca71ddf Mon Sep 17 00:00:00 2001 From: Kristoffer Date: Tue, 6 Aug 2024 14:44:22 +0200 Subject: [PATCH] Use branches when choosing how to merge face attrs While ifelse looked like a reasonable choice, it turns out that the performance (allocations in particular) is improved by creating a branch. We go from 2 allocations to 0 when merging faces, which very much adds up over time. --- src/faces.jl | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/src/faces.jl b/src/faces.jl index 1e4216b..dd55a0f 100644 --- a/src/faces.jl +++ b/src/faces.jl @@ -499,19 +499,20 @@ function Base.merge(a::Face, b::Face) # to narrow the types in e.g. `aheight * bheight` aheight = a.height bheight = b.height - Face(ifelse(isnothing(b.font), a.font, b.font), - if isnothing(bheight) aheight - elseif isnothing(aheight) bheight - elseif bheight isa Int bheight - elseif aheight isa Int round(Int, aheight * bheight) - else aheight * bheight end, - ifelse(isnothing(b.weight), a.weight, b.weight), - ifelse(isnothing(b.slant), a.slant, b.slant), - ifelse(isnothing(b.foreground), a.foreground, b.foreground), - ifelse(isnothing(b.background), a.background, b.background), - ifelse(isnothing(b.underline), a.underline, b.underline), - ifelse(isnothing(b.strikethrough), a.strikethrough, b.strikethrough), - ifelse(isnothing(b.inverse), a.inverse, b.inverse), + abheight = if isnothing(bheight) aheight + elseif isnothing(aheight) bheight + elseif bheight isa Int bheight + elseif aheight isa Int round(Int, aheight * bheight) + else aheight * bheight end + Face(if isnothing(b.font) a.font else b.font end, + abheight, + if isnothing(b.weight) a.weight else b.weight end, + if isnothing(b.slant) a.slant else b.slant end, + if isnothing(b.foreground) a.foreground else b.foreground end, + if isnothing(b.background) a.background else b.background end, + if isnothing(b.underline) a.underline else b.underline end, + if isnothing(b.strikethrough) a.strikethrough else b.strikethrough end, + if isnothing(b.inverse) a.inverse else b.inverse end, a.inherit) else b_noinherit = Face(