Skip to content

Improve performance of http.cookiejar.join_header_words #128982

Description

@picnixz

Feature or enhancement

Proposal:

Currently, we use if not re.search(r"^\w+$", v):. This can be replaced by if not v.isalnum() and '_' not in v. Benchmarks are as follows:

$ ./python -m pyperf timeit -s 'x = [[("text/plain", None), ("charset", "iso-8859-1\"")]]; from http.cookiejar import join_header_words as f' 'f(x)'
Mean +- std dev: [ref] 795 ns +- 8 ns -> [new] 604 ns +- 20 ns: 1.32x faster

It doesn't help having a compiled regex by the way.

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Linked PRs

Activity

  1. added
    performancePerformance or resource usage
    stdlibStandard Library Python modules in the Lib/ directory
    type-featureA feature request or enhancement
    on Jan 18, 2025
  2. self-assigned this
    on Jan 18, 2025
  3. added a commit that references this issue on Feb 26, 2025
  4. encukou commented on Feb 26, 2025

    @encukou
    Member

    Thank you!

  5. serhiy-storchaka commented on Feb 26, 2025

    @serhiy-storchaka
    Member

    This is incorrect change. For example, it produces incorrect results for "foo;_" and "foo_bar".

  6. encukou commented on Feb 26, 2025

    @encukou
    Member

    Oof, thanks for noticing.
    I'll revert and add a test.

  7. added 2 commits that reference this issue on Feb 26, 2025
  8. serhiy-storchaka commented on Feb 27, 2025

    @serhiy-storchaka
    Member

    \w here looks arbitrary. And it is. There are other red flags. See #130631.

  9. picnixz commented on Mar 1, 2025

    @picnixz
    MemberAuthor

    I'll close this one and we'll move to 130631

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

Metadata

Metadata

Assignees

Labels

performancePerformance or resource usagestdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions