summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGregory P. Smith <greg@krypto.org>2020-05-19 18:37:41 -0700
committerGitHub <noreply@github.com>2020-05-19 20:37:41 -0500
commit05c4f5178b75f6e6725324494aab8b27221328ea (patch)
treeb0f983d7c2e95e7b56af1de049945052df6ae7d9
parenta5bb7aab524f4edb986d33e4a47975a8e2d0c43c (diff)
downloadsix-git-05c4f5178b75f6e6725324494aab8b27221328ea.tar.gz
Optimize ensure_str and ensure_binary. (#331)
We found that large applications that have undergone a 2 -> 3 migration and wound up with a lot of six.ensure_str and six.ensure_binary calls could save 1-2% CPU usage by optimizing these for the common case. Further optimization could be done by replacing them with extension module implementations - assumed out of scope for the pure Python six project itself. Ideally all of these calls and use of six in people's code would be removed after there all need for any Python 2 compatibility is gone. But completing that kind of type cleanup requires a lot of human engineering time. This lowers the ongoing costs in the interim. Contributed by YouTube.
-rw-r--r--six.py18
1 files changed, 10 insertions, 8 deletions
diff --git a/six.py b/six.py
index 5fe9f8e..86da168 100644
--- a/six.py
+++ b/six.py
@@ -890,12 +890,11 @@ def ensure_binary(s, encoding='utf-8', errors='strict'):
- `str` -> encoded to `bytes`
- `bytes` -> `bytes`
"""
+ if isinstance(s, binary_type):
+ return s
if isinstance(s, text_type):
return s.encode(encoding, errors)
- elif isinstance(s, binary_type):
- return s
- else:
- raise TypeError("not expecting type '%s'" % type(s))
+ raise TypeError("not expecting type '%s'" % type(s))
def ensure_str(s, encoding='utf-8', errors='strict'):
@@ -909,12 +908,15 @@ def ensure_str(s, encoding='utf-8', errors='strict'):
- `str` -> `str`
- `bytes` -> decoded to `str`
"""
- if not isinstance(s, (text_type, binary_type)):
- raise TypeError("not expecting type '%s'" % type(s))
+ # Optimization: Fast return for the common case.
+ if type(s) is str:
+ return s
if PY2 and isinstance(s, text_type):
- s = s.encode(encoding, errors)
+ return s.encode(encoding, errors)
elif PY3 and isinstance(s, binary_type):
- s = s.decode(encoding, errors)
+ return s.decode(encoding, errors)
+ elif not isinstance(s, (text_type, binary_type)):
+ raise TypeError("not expecting type '%s'" % type(s))
return s