Skip to content

Commit

Permalink
Improved HTTPHeaderDict performance
Browse files Browse the repository at this point in the history
  • Loading branch information
seocam committed Feb 5, 2015
1 parent e9c1b98 commit f3d4488
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions urllib3/_collections.py
@@ -1,7 +1,7 @@
from collections import Mapping, MutableMapping
try:
from threading import RLock
except ImportError: # Platform-specific: No threads available
except ImportError: # Platform-specific: No threads available
class RLock:
def __enter__(self):
pass
Expand All @@ -10,7 +10,7 @@ def __exit__(self, exc_type, exc_value, traceback):
pass


try: # Python 2.7+
try: # Python 2.7+
from collections import OrderedDict
except ImportError:
from .packages.ordered_dict import OrderedDict
Expand Down Expand Up @@ -200,8 +200,7 @@ def __repr__(self):
def update(self, *args, **kwds):
headers = args[0]
if isinstance(headers, HTTPHeaderDict):
for key in headers:
for value in headers.getlist(key):
self.add(key, value)
for key in iterkeys(headers._data):
self._data.setdefault(key.lower(), []).extend(headers._data[key])
else:
super(HTTPHeaderDict, self).update(*args, **kwds)

3 comments on commit f3d4488

@ml31415
Copy link
Contributor

@ml31415 ml31415 commented on f3d4488 Feb 5, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this implementation of update seems to be different to the currently implemented version. This one is doing self.add(...), the current one self.__setitem__(...). Though add surely makes more sense.

@seocam
Copy link
Contributor Author

@seocam seocam commented on f3d4488 Feb 5, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ml31415 that's exactly the point of the PR. To implement an update that doesn't treat the value as a string so the data copied as it was before.

The performance improvement here was just a plus because @sigmavirus24 was concerned about it.

@ml31415
Copy link
Contributor

@ml31415 ml31415 commented on f3d4488 Feb 6, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't care too much actually. Nevertheless, the previous implementation is actually what the spec says about what to do with duplicate headers, set-cookie taken aside. Being selective about the header name might be worth implementing, when rewriting the module anyways. See here, e.g. https://github.com/gwik/geventhttpclient/blob/master/src/geventhttpclient/header.py

Please sign in to comment.