bpo-36778: Avoid functools in encodings.cp65001 by vstinner · Pull Request #13110 · python/cpython (original) (raw)

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Conversation7 Commits1 Checks0 Files changed

Conversation

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters

[ Show hidden characters]({{ revealButtonHref }})

vstinner

Replace functools.partial() with lambda to reduce the number of
imports at startup. Avoid the following imports at startup:

https://bugs.python.org/issue36778

serhiy-storchaka

if not hasattr(codecs, 'code_page_encode'):
raise LookupError("cp65001 encoding is only available on Windows")
### Codec APIs
encode = functools.partial(codecs.code_page_encode, 65001)
_decode = functools.partial(codecs.code_page_decode, 65001)
encode = lambda *args, **kw: codecs.code_page_encode(65001, *args, **kw)

Choose a reason for hiding this comment

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

Could you please measure how much overhead it adds?

@vstinner

Oh, my PR looks wrong. Most CP65001Test tests fail...

@paulmon

Oh, my PR looks wrong. Most CP65001Test tests fail...

I don't know if it helps, but test_startup_imports passes on IoT Core using this change with a default codepage of cp65001. So it looks like this strategy could work if there's a way to remove functools without breaking other things.

@methane

staticmethod is required, maybe.

@vstinner

Replace functools.partial() with lambda to reduce the number of imports at startup. Avoid the following imports at startup:

@vstinner

staticmethod is required, maybe.

Right. I fixed my PR with that. I also rebased my PR and added a NEWS entry.

@vstinner

@serhiy-storchaka: "Could you please measure how much overhead it adds?"

Benchmark in release mode:

>python -m venv env
>env\scripts\python_d.exe -m pip install perf
>env\scripts\python_d.exe -m perf timeit -s "import codecs; encode=codecs.lookup('cp65001').encode" "encode('')" -v -o ref.json
>git co master
>env\scripts\python_d.exe -m perf timeit -s "import codecs; encode=codecs.lookup('cp65001').encode" "encode('')" -v -o patch.json
env\scripts\python_d.exe -m perf compare_to ref.json patch.json

Mean +- std dev: [ref] 156 ns +- 3 ns -> [patch] 417 ns +- 17 ns: 2.68x slower (+168%)

@vstinner

I merged my PR #13230 instead.