On 11 May 2016 at 22:51, Ethan Furman <ethan@stoneleaf.us> wrote:
On 05/11/2016 01:44 PM, Serhiy Storchaka wrote:

os.path
'''''''

The various path-manipulation functions of ``os.path`` [#os-path]_
will be updated to accept path objects. For polymorphic functions that
accept both bytes and strings, they will be updated to simply use
code very much similar to
``path.__fspath__() if  hasattr(path, '__fspath__') else path``. This
will allow for their pre-existing type-checking code to continue to
function.

I afraid that this will hit a performance. Some os.path functions are
used in tight loops, they are hard optimized, and adding support of path
protocol can have visible negative effect.

Do you have an example of os.path functions being used in a tight loop?

os.path.getmtime could be used in a tight loop, to sync directories with a lot of files for instance.

% python3 -m timeit -s "import os.path; p = 'out'" "hasattr(p, '__fspath__'), os.path.getmtime(p)"
100000 loops, best of 3: 2.67 usec per loop
% python3 -m timeit -s "import os.path; p = 'out'" "isinstance(p, (str, bytes)), os.path.getmtime(p)"
100000 loops, best of 3: 2.45 usec per loop
% python3 -m timeit -s "import os.path; p = 'out'" "os.path.getmtime(p)" 
100000 loops, best of 3: 2.02 usec per loop

a 25% markup is a lot imo.

a isinstance check prior to the hasattr might be a way to mitigate this a bit (but it doesn't help much)
Granted, this example could be optimised by calling os.stat directly, which is not in os.path, but still, worth considering