Skip to content

Commit

Permalink
Optimize ensure_str and ensure_binary. (#331)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
gpshead committed May 20, 2020
1 parent a5bb7aa commit 05c4f51
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions six.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'):
Expand All @@ -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


Expand Down

0 comments on commit 05c4f51

Please sign in to comment.