Skip to content

Parsing can become accidentally quadratic because of sscanf #40

@leoetlino

Description

@leoetlino

While investigating a performance issue in a library I am working on, I noticed that parsing a particular document (warning: very large file) causes the program to spend a lot of time in memchr (roughly 70% of its time). Pausing execution in gdb gives the following stack trace: (this is an image to keep the colors)

image

to the point that parsing is only ~3x faster than doing it with PyYAML. The speedup I get for other files is >10x.

It looks like the problem is that reading floats is done using c4::from_chars -> c4::atof -> sscanf which ultimately calls _IO_str_init_static_internal in the glibc implementation. As far as I understand, this essentially causes strlen to be called on the string that rapidyaml passes to it, essentially causing strlen to be called O(n^2) times.

Since there are a lot of floats throughout the entire document, parsing appears to become accidentally quadratic.

Not sure how to fix this. Even using the standard atof might not help since that function doesn't take the length either...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions