This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author dino.viehland
Recipients dino.viehland, methane, vstinner
Date 2021-03-20.01:16:24
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1616202985.14.0.898497661194.issue43452@roundup.psfhosted.org>
In-reply-to
Content
Setup a micro-benchmark, foo.c:

#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include <string.h>
#include <stdlib.h>

int
main(int argc, char *argv[])
{
    wchar_t *program = Py_DecodeLocale(argv[0], NULL);
    if (program == NULL) {
        fprintf(stderr, "Fatal error: cannot decode argv[0]\n");
        exit(1);
    }
    Py_SetProgramName(program);  /* optional but recommended */
    Py_Initialize();
    PyObject *pName = PyUnicode_DecodeFSDefault("foo");
    if (pName == NULL) { printf("no foo\n"); PyErr_Print(); }
    PyObject *pModule = PyImport_Import(pName);
    if (pModule == NULL) { printf("no mod\n"); PyErr_Print(); return 0; }
    PyObject *cls = PyObject_GetAttrString(pModule, "C");
    if (cls == NULL) { printf("no cls\n"); }
    PyObject *fs[20];
    for(int i = 0; i<20; i++) {
         char buf[4];
         sprintf(buf, "f%d", i);
         fs[i] = PyUnicode_DecodeFSDefault(buf);
    }
    for(int i = 0; i<100000000; i++) {
         for(int j = 0; j<20; j++) {
             if(_PyType_Lookup(cls, fs[j])==NULL) {
		printf("Uh oh\n");
	     }
         }
    }

   if (Py_FinalizeEx() < 0) {
        exit(120);
    }
    PyMem_RawFree(program);
    return 0;
}


Lib/foo.py:
import time


class C:
    pass

for i in range(20):
    setattr(C, f"f{i}", lambda self: None)


obj hash: 0m6.222s
str hash: 0m6.327s
baseline: 0m6.784s
History
Date User Action Args
2021-03-20 01:16:25dino.viehlandsetrecipients: + dino.viehland, vstinner, methane
2021-03-20 01:16:25dino.viehlandsetmessageid: <1616202985.14.0.898497661194.issue43452@roundup.psfhosted.org>
2021-03-20 01:16:25dino.viehlandlinkissue43452 messages
2021-03-20 01:16:24dino.viehlandcreate