Skip to content

stream.hls_playlist: refactor M3U8Parser #4540

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 22, 2022

Conversation

bastimeyer
Copy link
Member

  • Generate a mapping of tags->callbacks on parser init once
    and cache mapping as class attribute (avoid parent class lookups)
  • Performance: look up tags in cached mapping in parse_line()
  • Re-order parse_tag_... methods by definition order of RFC 8216
  • Add docstring to each parse_tag_... method with link to RFC 8216
  • Add typing annotations to each parse_tag_... method
  • Fix minor typing issues in regards to optional URIs in Key and Media
    and update parse methods accordingly
  • Pass keywords to various named tuples instead of positional args

This improves the performance of the M3U8Parser, as it doesn't have to generate a string with the name of supported tag methods for each tag anymore, and it can simply look up tags in a pre-generated mapping of tags->callbacks that get only built once after the first instantiation of the class. This is fully compatible with M3U8Parser subclasses which override or add tag methods, and nothing has to be changed by those subclasses (tag method names stay the same). Subclasses have their own cache.

One thing to note is that the parser doesn't call tag.lower() anymore in parse_line(), which means that if for some reason an HLS playlist includes lowercase tag names or anything that doesn't match the exact case of the tag names, those invalid tags won't be recognized anymore, because tags are case sensitive.

These changes will introduce a merge conflict with #4538.

- Generate a mapping of tags->callbacks on parser init once
  and cache mapping as class attribute (avoid parent class lookups)
- Performance: look up tags in cached mapping in `parse_line()`
- Re-order `parse_tag_...` methods by definition order of RFC 8216
- Add docstring to each `parse_tag_...` method with link to RFC 8216
- Add typing annotations to each `parse_tag_...` method
- Fix minor typing issues in regards to optional URIs in Key and Media
  and update parse methods accordingly
- Pass keywords to various named tuples instead of positional args
@back-to back-to merged commit c6b68b0 into streamlink:master May 22, 2022
@bastimeyer bastimeyer deleted the stream/hls/refactor-parser branch May 22, 2022 16:17
@bastimeyer bastimeyer mentioned this pull request May 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants