Skip to content

Conversation

@shindonghwi
Copy link

Summary

Replace list with set for membership testing in cookie jar operations to improve performance from O(n) to O(1) per lookup.

Changes

  • list_domains(): Use set comprehension instead of list with in check
  • list_paths(): Use set comprehension instead of list with in check
  • multiple_domains(): Use set for tracking seen domains
  • cookiejar_from_dict(): Use set for existing cookie names lookup

Benchmark Results

cookiejar_from_dict(overwrite=False) - called on every request:

Existing Cookies Before After Improvement
100 + 100 new 0.053s 0.020s 2.6x faster
500 + 100 new 0.258s 0.092s 2.8x faster
1000 + 100 new 0.546s 0.184s 3.0x faster

list_domains() utility method:

Cookies Before After Improvement
500 0.050s 0.025s 2.0x faster
1000 0.096s 0.045s 2.1x faster
2000 0.191s 0.082s 2.3x faster

Test Plan

  • Added unit tests for list_domains(), list_paths(), multiple_domains(), cookiejar_from_dict()
  • All existing cookie tests pass

Replace list with set for membership testing in cookie jar operations
to improve performance from O(n) to O(1) per lookup:

- list_domains(): Use set comprehension
- list_paths(): Use set comprehension
- multiple_domains(): Use set for tracking seen domains
- cookiejar_from_dict(): Use set for existing cookie names lookup

Benchmarks show 2-3x improvement for cookiejar_from_dict() which is
called on every request preparation.
@sigmavirus24
Copy link
Contributor

This breaks implicit behaviour that some people are likely relying on.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants