Skip to content

findfont scans all fonts every time #67

Open
@ederag

Description

@ederag

findfont works well, but takes several seconds - which may be considered fast, considering - here (openSUSE Leap-15.3),
but led to the significant system-dependent overhead found in cesaraustralia/DynamicGrids.jl#194 (comment).

findfont is always scanning all fonts, opening them
to get their name and properties and find the best one.

The issue is that on my system there were over 10_000 fonts installed, most quick to load (median about 7.5 µs),
but some taking much longer (see the diagnosis histogram), so the average is about 270 µs.

diagnosis
using FreeTypeAbstraction: fontpaths, try_load, match_font
font_folders = copy(fontpaths())

t1_ = Float64[]
path_ = String[]
@time for folder in font_folders
           for font in readdir(folder)
               fpath = joinpath(folder, font)
               
               t1 = @elapsed face = try_load(fpath)
               push!(t1_, t1)
               push!(path_, fpath)
               face === nothing && continue
               finalize(face)
           end
       end

  2.855721 seconds (159.18 k allocations: 23.692 MiB)

julia> sum(t1_)
2.832251035

julia> using UnicodePlots
julia> histogram(t1_)
                  ┌                                        ┐ 
   [0.0  , 0.001) ┤█████████████████████████████████  10063  
   [0.001, 0.002) ┤▌ 151                                     
   [0.002, 0.003) ┤▍ 83                                      
   [0.003, 0.004) ┤▎ 32                                      
   [0.004, 0.005) ┤▏ 4                                       
   [0.005, 0.006) ┤▏ 14                                      
   [0.006, 0.007) ┤▏ 7                                       
   [0.007, 0.008) ┤▏ 15                                      
   [0.008, 0.009) ┤▏ 9                                       
   [0.009, 0.01 ) ┤▏ 1                                       
   [0.01 , 0.011) ┤▏ 3                                       
   [0.011, 0.012) ┤  0                                       
   [0.012, 0.013) ┤▏ 7                                       
   [0.013, 0.014) ┤▏ 1                                       
   [0.014, 0.015) ┤▏ 1                                       
                  └                                        ┘ 
                                   Frequency                

julia> using Statistics
julia> mean(t1_)
0.00027256770618804736
julia> median(t1_)
7.4702e-5

There are several solutions on the other side (e.g. removing slow fonts, using FTFont(font_path) to load the specific font directly).

But it might be nice to have a kind of cache so that findfont(font_name) would be even faster on subsequent calls ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions