calling dictresize outside dictobject.c
Hi, I'm trying to write a C extension which is a subclass of dict. I want to do something like a setdefault() but with a single lookup. Looking through the dictobject code, the three workhorse routines lookdict, insertdict and dictresize are not available directly for functions outside dictobject.c, but I can get at lookdict through dict->ma_lookup(). So I use lookdict to get the PyDictEntry (call it ep) I'm looking for. The comments for lookdict say ep is ready to be set... so I do that. Then I check whether the dict needs to be resized--following the nice example of PyDict_SetItem. But I can't call dictresize to finish off the process. Should I be using PyDict_SetItem directly? No... it does its own lookup. I don't want a second lookup! I already know which entry will be filled. So then I look at the code for setdefault and it also does a double lookup for checking and setting an entry. What subtle issue am I missing? Why does setdefault do a double lookup? More globally, why isn't dictresize available through the C-API? If there isn't a reason to do a double lookup I have a patch for setdefault, but I thought I should ask here first. Thanks! Dan
On Mon, Apr 06, 2009, Dan Schult wrote:
I'm trying to write a C extension which is a subclass of dict. I want to do something like a setdefault() but with a single lookup.
python-dev is for core development, not for questions about using Python. Please use comp.lang.python or the capi-sig list. -- Aahz (aahz@pythoncraft.com) <*> http://www.pythoncraft.com/ "...string iteration isn't about treating strings as sequences of strings, it's about treating strings as sequences of characters. The fact that characters are also strings is the reason we have problems, but characters are strings for other good reasons." --Aahz
Did you read the post until the end? The OP is asking a question
related to a very low level detail of dict implementation and making
an offer to write a patch that could speed-up dict.setdefault() in
core CPython... IMHO, a poll on python-dev do makes sense...
On Tue, Apr 7, 2009 at 1:34 PM, Aahz
On Mon, Apr 06, 2009, Dan Schult wrote:
I'm trying to write a C extension which is a subclass of dict. I want to do something like a setdefault() but with a single lookup.
python-dev is for core development, not for questions about using Python. Please use comp.lang.python or the capi-sig list. -- Aahz (aahz@pythoncraft.com) <*> http://www.pythoncraft.com/
"...string iteration isn't about treating strings as sequences of strings, it's about treating strings as sequences of characters. The fact that characters are also strings is the reason we have problems, but characters are strings for other good reasons." --Aahz _______________________________________________ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/dalcinl%40gmail.com
-- Lisandro Dalcín --------------- Centro Internacional de Métodos Computacionales en Ingeniería (CIMEC) Instituto de Desarrollo Tecnológico para la Industria Química (INTEC) Consejo Nacional de Investigaciones Científicas y Técnicas (CONICET) PTLC - Güemes 3450, (3000) Santa Fe, Argentina Tel/Fax: +54-(0)342-451.1594
Hi Dan,
Thanks for your interest.
2009/4/6 Dan Schult
Hi, I'm trying to write a C extension which is a subclass of dict. I want to do something like a setdefault() but with a single lookup.
Looking through the dictobject code, the three workhorse routines lookdict, insertdict and dictresize are not available directly for functions outside dictobject.c, but I can get at lookdict through dict->ma_lookup().
So I use lookdict to get the PyDictEntry (call it ep) I'm looking for. The comments for lookdict say ep is ready to be set... so I do that. Then I check whether the dict needs to be resized--following the nice example of PyDict_SetItem. But I can't call dictresize to finish off the process.
Should I be using PyDict_SetItem directly? No... it does its own lookup. I don't want a second lookup! I already know which entry will be filled.
So then I look at the code for setdefault and it also does a double lookup for checking and setting an entry.
What subtle issue am I missing? Why does setdefault do a double lookup? More globally, why isn't dictresize available through the C-API?
Because it's not useful outside the intimate implementation details of dictobject.c
If there isn't a reason to do a double lookup I have a patch for setdefault, but I thought I should ask here first.
Raymond tells me the cost of the second lookup is negligible because of caching, but PyObject_Hash needn't be called two times. He's working on a patch later today. -- Regards, Benjamin
participants (4)
-
Aahz
-
Benjamin Peterson
-
Dan Schult
-
Lisandro Dalcin