Skip to content

Commit

Permalink
Use branches when choosing how to merge face attrs
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
KristofferC authored and tecosaur committed Aug 6, 2024
1 parent eada2dc commit 9b9cf71
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions src/faces.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 9b9cf71

Please sign in to comment.