Skip to content

Commit 6863348

Browse files
KristofferCtecosaur
authored andcommitted
Improve type inference of face merging
Help Julia's type inference by extracting out a field access to outside type checks. Running a benchmark on a large, heavily annotated string we see a 50% reduction in runtime (27.5ms -> 13.6ms) and a 25% reduction in allocations (554k -> 412k).
1 parent f588218 commit 6863348

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

src/faces.jl

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -495,12 +495,16 @@ later faces taking priority.
495495
"""
496496
function Base.merge(a::Face, b::Face)
497497
if isempty(b.inherit)
498+
# Extract the heights to help type inference a bit to be able
499+
# to narrow the types in e.g. `aheight * bheight`
500+
aheight = a.height
501+
bheight = b.height
498502
Face(ifelse(isnothing(b.font), a.font, b.font),
499-
if isnothing(b.height) a.height
500-
elseif isnothing(a.height) b.height
501-
elseif b.height isa Int b.height
502-
elseif a.height isa Int round(Int, a.height * b.height)
503-
else a.height * b.height end,
503+
if isnothing(bheight) aheight
504+
elseif isnothing(aheight) bheight
505+
elseif bheight isa Int bheight
506+
elseif aheight isa Int round(Int, aheight * bheight)
507+
else aheight * bheight end,
504508
ifelse(isnothing(b.weight), a.weight, b.weight),
505509
ifelse(isnothing(b.slant), a.slant, b.slant),
506510
ifelse(isnothing(b.foreground), a.foreground, b.foreground),

0 commit comments

Comments
 (0)