Skip to content

Quadratic time complexity of known_hosts file parsing #2251

Open

Description

@jack-stanek-hpe

Hello,
I'm running into poor performance when loading a large known_hosts file with around 7,000 entries. For instance:

$ wc -l ./known_hosts
    7000 ./known_hosts
$ python3
Python 3.9.16 (main, Dec  7 2022, 10:15:43)
[Clang 14.0.0 (clang-1400.0.29.202)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import paramiko
>>> import timeit
>>> timeit.timeit(lambda: paramiko.hostkeys.HostKeys('./known_hosts'), number=1)
23.574272852999997

(I threw together a known_hosts generator script, as well as a synthetic known_hosts file here as a reproducer for reference.)

After digging into it a bit, I think this is due to the quadratic time complexity of HostKeys loading due to the way that key entries are stored in a flat list, and so constant-time random access isn't possible. This seems to be the same issue as observed in #437.

Going off the discussion in that issue, I decided to take a crack at addressing this, and I have a branch on my own fork which re-implements parts of the HostKeys class to speed up lookups. My approach is to split out entries with hashed hostnames and plaintext hostnames, and place entries with plaintext hostnames into a dict for constant-time lookups. With hashed hostnames, I don't think constant time lookups are actually possible, since looking up a hashed hostname entry for some plaintext hostname string requires hashing the hostname with every hashed entry's salt, which is going to be linear time anyway. For the use-case with a large known_hosts file with only plaintext hostnames, this approach should nonetheless provide substantial performance benefits.

Please let me know if you'd like me to open a PR from my branch, I'd be happy to be able to contribute! I'm not sure what your process is for contributions, and I don't want to step on any toes. 😅

Activity

  1. bskinn commented on Jun 12, 2023

    @bskinn
    Contributor

    Thanks for the report! And, thanks for mentioning the contributing info -- looks like it's not mentioned in the README anywhere, but paramiko.org refers to this general contributions guide.

    In any event, a PR is definitely welcome -- please do create one.

    I do need to note that bitprophet's attention is focused primarily on the key/auth rework of #387 and #23 at the moment, so other non-critical fixes and features are generally on pause until that's done. But, if we can get a PR into shape, it might get slipstreamed into one of those releases; or, at worst, be ready to go once the key/auth stuff is all tied off.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions