Skip to content

Importing ev3dev.fonts takes a lot of time #456

Closed

Description

Activity

  1. ddemidov commented on Apr 16, 2018

    @ddemidov
    MemberAuthor

    We could probably get rid of pkg_resources by using __file__ variable inside ev3dev.fonts.__init__ to get the path to the fonts directory. Then we could just use glob.glob() as a replacement for package_listdir, and os.path.join() for package_filename.

  2. ndward commented on Apr 16, 2018

    @ndward

    40s just to load some file names. Wow. I tried

    #!/usr/bin/env python3
    from pkg_resources import package_listdir, package_filename
    

    and after 48s (39s adjusted for download etc time) I got
    ImportError: cannot import name 'package_listdir'

    I also tried

    #!/usr/bin/env python3
    from pkg_resources import resource_listdir, resource_filename
    

    and that completed in about 50s, so no time saved.

    I believe the font files we are talking about are at usr\lib\python3\dist-packages\ev3dev\fonts. That folder contains 402 items (1.34MB), of which 200 .pbm files and 200 .pil files. It also contains a file _int_.py which begins:

    import pkg_resources
    import os.path
    from PIL import ImageFont
    
    def available():
        """
        Returns list of available font names.
        """
        names = []
        for f in pkg_resources.resource_listdir('ev3dev.fonts', ''):
            name, ext = os.path.splitext(os.path.basename(f))
            if ext == '.pil':
                names.append(name)
        return sorted(names)
    

    Is that available() function relevant or useful?

    If there is no other way of speeding up the loading of file names then maybe one solution would be to split the fonts into multiple folders so that only one small folder then has to have its file names loaded? Or maybe we don't need 200 different fonts? Or maybe we don't need both the .pil and the .pbm files?

  3. ndward commented on Apr 20, 2018

    @ndward

    Using Stretch, it seems to take about 45s for this command to run
    import ev3dev.fonts as fonts

    @ddemidov says that this command does not import any fonts but does this instead:

    import pkg_resources # 40s
    import os.path # 1s
    from PIL import ImageFont # 3s
    

    I have added comments above to indicate how long each import seems to take. The only import that takes a long time is
    import pkg_resources #40s

    @ddemidov says

    pkg_resources is used to get file names for the font files packaged with ev3dev.

    If I'm running import ev3dev.fonts as fonts it is because I intend to use a specific font. If I know the file name and location then why should I have to wait 40s for import pkg_resources to get file names? I could supply this information in a line of code if necessary, saving 40s of waiting. I believe the files are located in this directory usr\lib\python3\dist-packages\ev3dev\fonts and I believe the extensions for the filenames are .pbm and .pil (I don't know which format is needed – perhaps both?)

  4. ddemidov commented on Apr 22, 2018

    @ddemidov
    MemberAuthor

    #458 should help, but needs testing.

  5. ensonic commented on Apr 26, 2018

    @ensonic
    Contributor

    @ndward how did you measure the import times? tool or stopwatch?

  6. ndward commented on Apr 26, 2018

    @ndward
  7. ddemidov commented on Apr 30, 2018

    @ddemidov
    MemberAuthor

    Fixed by #458

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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