RESOLVED FIXED 71063
HashMap<>::add should return a more descriptive object
https://bugs.webkit.org/show_bug.cgi?id=71063
Summary HashMap<>::add should return a more descriptive object
Ryosuke Niwa
Reported 2011-10-27 14:24:56 PDT
HashMap<>::add returns a pair of HashMap iterator and a boolean but the semantics of this return value is totally unclear in its call sites. We end up code like: pair<EventListenerHashMap::iterator, bool> result = m_hashMap->add(eventType, 0); if (result.second) result.first->second = new EventListenerVector; We should create some struct for this purpose so that semantics is clearer.
Attachments
Patch (150.69 KB, patch)
2012-03-27 18:46 PDT, Caio Marcelo de Oliveira Filho
no flags
Patch (168.87 KB, patch)
2012-03-28 11:17 PDT, Caio Marcelo de Oliveira Filho
no flags
Patch (169.73 KB, patch)
2012-03-28 11:31 PDT, Caio Marcelo de Oliveira Filho
no flags
Patch (173.09 KB, patch)
2012-03-28 13:32 PDT, Caio Marcelo de Oliveira Filho
no flags
Patch (174.17 KB, patch)
2012-03-28 16:15 PDT, Caio Marcelo de Oliveira Filho
no flags
Patch (176.09 KB, patch)
2012-03-29 05:53 PDT, Caio Marcelo de Oliveira Filho
no flags
Patch (177.00 KB, patch)
2012-03-29 06:14 PDT, Caio Marcelo de Oliveira Filho
no flags
Patch (184.41 KB, patch)
2012-03-29 08:22 PDT, Caio Marcelo de Oliveira Filho
no flags
Patch (185.58 KB, patch)
2012-03-29 09:25 PDT, Caio Marcelo de Oliveira Filho
no flags
Patch (186.14 KB, patch)
2012-03-29 10:11 PDT, Caio Marcelo de Oliveira Filho
no flags
Caio Marcelo de Oliveira Filho
Comment 1 2012-01-06 16:22:30 PST
(In reply to comment #0) > We end up code like: > > pair<EventListenerHashMap::iterator, bool> result = m_hashMap->add(eventType, 0); > if (result.second) > result.first->second = new EventListenerVector; What about have the following? EventListenerHashMap::AddResult result = m_hashMap->add(eventType, 0); if (result.isNewKey) result.iterator->second = new EventListenerVector;
Ryosuke Niwa
Comment 2 2012-01-06 16:45:38 PST
(In reply to comment #1) > (In reply to comment #0) > > We end up code like: > > > > pair<EventListenerHashMap::iterator, bool> result = m_hashMap->add(eventType, 0); > > if (result.second) > > result.first->second = new EventListenerVector; > > What about have the following? > > EventListenerHashMap::AddResult result = m_hashMap->add(eventType, 0); > if (result.isNewKey) > result.iterator->second = new EventListenerVector; Yes, that'll be an improvement. We should probably improve the interface of iterator as well so that it'll be something like: if (result.isNewKey) result.iterator->value = new EventListenerVector;
Caio Marcelo de Oliveira Filho
Comment 3 2012-03-27 18:46:21 PDT
Alexis Menard (darktears)
Comment 4 2012-03-27 19:01:16 PDT
Comment on attachment 134195 [details] Patch View in context: https://bugs.webkit.org/attachment.cgi?id=134195&action=review > Source/JavaScriptCore/ChangeLog:9 > + the iterator type, there's a need for its own AddResult type instead of a simple Is there any other benefit that a better readability? > Source/WTF/wtf/HashTable.h:335 > + iterator iter; not sure if iter is a good name. We usually use it no?
Philippe Normand
Comment 5 2012-03-27 19:07:33 PDT
Build Bot
Comment 6 2012-03-27 19:07:43 PDT
Build Bot
Comment 7 2012-03-27 19:08:37 PDT
Gyuyoung Kim
Comment 8 2012-03-27 19:22:37 PDT
WebKit Review Bot
Comment 9 2012-03-27 19:39:48 PDT
Comment on attachment 134195 [details] Patch Attachment 134195 [details] did not pass chromium-ews (chromium-xvfb): Output: http://queues.webkit.org/results/12148762
Caio Marcelo de Oliveira Filho
Comment 10 2012-03-28 06:40:49 PDT
Comment on attachment 134195 [details] Patch View in context: https://bugs.webkit.org/attachment.cgi?id=134195&action=review I'll upload a new version fixing the EWS results. >> Source/JavaScriptCore/ChangeLog:9 >> + the iterator type, there's a need for its own AddResult type instead of a simple > > Is there any other benefit that a better readability? No. As the WTF/ChangeLog pointed out, this change's goal is to improve readability. In practice, this is just a specialized pair. >> Source/WTF/wtf/HashTable.h:335 >> + iterator iter; > > not sure if iter is a good name. We usually use it no? I prefer iter, I would even prefer iterator. I will give it a try here. I think 'it' works better as a local variable with a small scope.
Caio Marcelo de Oliveira Filho
Comment 11 2012-03-28 11:17:44 PDT
Caio Marcelo de Oliveira Filho
Comment 12 2012-03-28 11:31:27 PDT
Build Bot
Comment 13 2012-03-28 11:56:54 PDT
Ryosuke Niwa
Comment 14 2012-03-28 12:02:45 PDT
Comment on attachment 134350 [details] Patch View in context: https://bugs.webkit.org/attachment.cgi?id=134350&action=review > Source/JavaScriptCore/API/JSCallbackObject.h:92 > - m_propertyMap.add(propertyName.impl(), empty).first->second.set(globalData, owner, value); > + m_propertyMap.add(propertyName.impl(), empty).iter->second.set(globalData, owner, value); Can we add key() and value() to AddResult so that we can avoid using first/second? In the long term, we should also add specialized pair for iterators. > Source/JavaScriptCore/bytecompiler/BytecodeGenerator.cpp:972 > - if (result.second) // new entry > + IdentifierMap::AddResult result = m_identifierMap.add(rep, m_codeBlock->numberOfIdentifiers()); > + if (result.isNewKey) Maybe we should call it isNewEntry instead of isNewKey? > Source/JavaScriptCore/runtime/WeakGCMap.h:-59 > - > public: > - Do we really want to remove these blank lines? > Source/WTF/wtf/ListHashSet.h:88 > + struct AddResult { > + AddResult(iterator iter, bool isNewKey) : iter(iter), isNewKey(isNewKey) { } > + iterator iter; > + bool isNewKey; > + }; It's not great that we have to define multiple versions of AddResult. Can we templatize this class and share across hash classes?
Build Bot
Comment 15 2012-03-28 12:55:29 PDT
Build Bot
Comment 16 2012-03-28 13:08:05 PDT
Caio Marcelo de Oliveira Filho
Comment 17 2012-03-28 13:32:19 PDT
Caio Marcelo de Oliveira Filho
Comment 18 2012-03-28 13:40:57 PDT
Comment on attachment 134350 [details] Patch View in context: https://bugs.webkit.org/attachment.cgi?id=134350&action=review >> Source/JavaScriptCore/API/JSCallbackObject.h:92 >> + m_propertyMap.add(propertyName.impl(), empty).iter->second.set(globalData, owner, value); > > Can we add key() and value() to AddResult so that we can avoid using first/second? > In the long term, we should also add specialized pair for iterators. We can, but I'd rather not do it right now. Let's try first add specialized pair for iterators. If we manage to get that, we might not want to have key() since will cause potential confusion with iterator::key (which is not a function). If it happens that we want the shortcuts anyway, we can add them after. >> Source/JavaScriptCore/bytecompiler/BytecodeGenerator.cpp:972 >> + if (result.isNewKey) > > Maybe we should call it isNewEntry instead of isNewKey? Done. >> Source/JavaScriptCore/runtime/WeakGCMap.h:-59 >> - > > Do we really want to remove these blank lines? Fixed. >> Source/WTF/wtf/ListHashSet.h:88 >> + }; > > It's not great that we have to define multiple versions of AddResult. Can we templatize this class and share across hash classes? Good idea. The new patch implements this. Since AddResult was moved outside HashTable, we can use 'iterator' instead of 'iter' too!
Build Bot
Comment 19 2012-03-28 14:08:29 PDT
Build Bot
Comment 20 2012-03-28 16:08:42 PDT
Caio Marcelo de Oliveira Filho
Comment 21 2012-03-28 16:15:30 PDT
Ryosuke Niwa
Comment 22 2012-03-28 16:21:56 PDT
Comment on attachment 134435 [details] Patch Thanks for the patch! New code looks great to me.
Build Bot
Comment 23 2012-03-28 16:53:42 PDT
Caio Marcelo de Oliveira Filho
Comment 24 2012-03-29 05:53:11 PDT
Caio Marcelo de Oliveira Filho
Comment 25 2012-03-29 06:14:49 PDT
Build Bot
Comment 26 2012-03-29 06:55:39 PDT
Caio Marcelo de Oliveira Filho
Comment 27 2012-03-29 08:22:05 PDT
Caio Marcelo de Oliveira Filho
Comment 28 2012-03-29 09:25:09 PDT
Build Bot
Comment 29 2012-03-29 10:07:18 PDT
Caio Marcelo de Oliveira Filho
Comment 30 2012-03-29 10:11:40 PDT
Caio Marcelo de Oliveira Filho
Comment 31 2012-03-29 11:49:38 PDT
Note You need to log in before you can comment on or make changes to this bug.