Title: Optimize the empty set "literal"
Type: performance Stage: resolved
Components: Interpreter Core Versions: Python 3.8
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: benjamin.peterson, brett.cannon, josh.r, mark.dickinson, ncoghlan, pitrou, serhiy.storchaka, yselivanov
Priority: normal Keywords: patch

Created on 2018-04-01 10:29 by serhiy.storchaka, last changed 2018-04-09 16:35 by serhiy.storchaka. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 6330 closed serhiy.storchaka, 2018-04-01 10:35
Messages (6)
msg314769 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-04-01 10:29
The following PR optimizes bytecode for the empty set "literal": {*()}.

Currently it is compiled to

    LOAD_CONST               0 (())
    BUILD_SET_UNPACK         1

It will optimized to

    BUILD_SET                0

$ ./python -m perf timeit --duplicate 1000 '{*()}'

Unpatched:  Mean +- std dev: 68.6 ns +- 1.1 ns
Patched:    Mean +- std dev: 31.8 ns +- 2.8 ns
msg314771 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2018-04-01 11:35
Does anyone seriously write "{*()}" instead of "set()"?
msg314772 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-04-01 11:56
Don't know. :-)

Consider this as a 1st April egg.
msg314790 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2018-04-01 23:32
> Does anyone seriously write "{*()}" instead of "set()"?

It doesn't seem to be laughing to me.
msg314849 - (view) Author: Josh Rosenberg (josh.r) * (Python triager) Date: 2018-04-03 02:15
I may have immediately latched onto this, dubbing it the "one-eyed monkey operator", the moment the generalized unpacking released.

I always hated the lack of an empty set literal, and enjoyed having this exist just to fill that asymmetry with the other built-in collection types (that said, I never use it in production code, nor teach it in classes I run).
msg315138 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-04-09 16:35
Closed due to the lack of practical applications. Can be reopened if this idiom become popular.
Date User Action Args
2018-04-09 16:35:29serhiy.storchakasetstatus: open -> closed
resolution: rejected
messages: + msg315138

stage: patch review -> resolved
2018-04-03 02:15:45josh.rsetnosy: + josh.r
messages: + msg314849
2018-04-01 23:32:45pitrousetnosy: + pitrou
messages: + msg314790
2018-04-01 11:56:26serhiy.storchakasetmessages: + msg314772
2018-04-01 11:35:45mark.dickinsonsetnosy: + mark.dickinson
messages: + msg314771
2018-04-01 10:35:12serhiy.storchakasetkeywords: + patch
stage: patch review
pull_requests: + pull_request6043
2018-04-01 10:29:26serhiy.storchakacreate