WebKit Bugzilla
New
Browse
Log In
×
Sign in with GitHub
or
Remember my login
Create Account
·
Forgot Password
Forgotten password account recovery
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
Details
Formatted Diff
Diff
Patch
(168.87 KB, patch)
2012-03-28 11:17 PDT
,
Caio Marcelo de Oliveira Filho
no flags
Details
Formatted Diff
Diff
Patch
(169.73 KB, patch)
2012-03-28 11:31 PDT
,
Caio Marcelo de Oliveira Filho
no flags
Details
Formatted Diff
Diff
Patch
(173.09 KB, patch)
2012-03-28 13:32 PDT
,
Caio Marcelo de Oliveira Filho
no flags
Details
Formatted Diff
Diff
Patch
(174.17 KB, patch)
2012-03-28 16:15 PDT
,
Caio Marcelo de Oliveira Filho
no flags
Details
Formatted Diff
Diff
Patch
(176.09 KB, patch)
2012-03-29 05:53 PDT
,
Caio Marcelo de Oliveira Filho
no flags
Details
Formatted Diff
Diff
Patch
(177.00 KB, patch)
2012-03-29 06:14 PDT
,
Caio Marcelo de Oliveira Filho
no flags
Details
Formatted Diff
Diff
Patch
(184.41 KB, patch)
2012-03-29 08:22 PDT
,
Caio Marcelo de Oliveira Filho
no flags
Details
Formatted Diff
Diff
Patch
(185.58 KB, patch)
2012-03-29 09:25 PDT
,
Caio Marcelo de Oliveira Filho
no flags
Details
Formatted Diff
Diff
Patch
(186.14 KB, patch)
2012-03-29 10:11 PDT
,
Caio Marcelo de Oliveira Filho
no flags
Details
Formatted Diff
Diff
Show Obsolete
(9)
View All
Add attachment
proposed patch, testcase, etc.
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
Created
attachment 134195
[details]
Patch
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
Comment on
attachment 134195
[details]
Patch
Attachment 134195
[details]
did not pass gtk-ews (gtk): Output:
http://queues.webkit.org/results/12140886
Build Bot
Comment 6
2012-03-27 19:07:43 PDT
Comment on
attachment 134195
[details]
Patch
Attachment 134195
[details]
did not pass win-ews (win): Output:
http://queues.webkit.org/results/12140883
Build Bot
Comment 7
2012-03-27 19:08:37 PDT
Comment on
attachment 134195
[details]
Patch
Attachment 134195
[details]
did not pass mac-ews (mac): Output:
http://queues.webkit.org/results/12148750
Gyuyoung Kim
Comment 8
2012-03-27 19:22:37 PDT
Comment on
attachment 134195
[details]
Patch
Attachment 134195
[details]
did not pass efl-ews (efl): Output:
http://queues.webkit.org/results/12143899
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
Created
attachment 134345
[details]
Patch
Caio Marcelo de Oliveira Filho
Comment 12
2012-03-28 11:31:27 PDT
Created
attachment 134350
[details]
Patch
Build Bot
Comment 13
2012-03-28 11:56:54 PDT
Comment on
attachment 134350
[details]
Patch
Attachment 134350
[details]
did not pass win-ews (win): Output:
http://queues.webkit.org/results/12151342
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
Comment on
attachment 134350
[details]
Patch
Attachment 134350
[details]
did not pass mac-ews (mac): Output:
http://queues.webkit.org/results/12157196
Build Bot
Comment 16
2012-03-28 13:08:05 PDT
Comment on
attachment 134350
[details]
Patch
Attachment 134350
[details]
did not pass mac-ews (mac): Output:
http://queues.webkit.org/results/12159247
Caio Marcelo de Oliveira Filho
Comment 17
2012-03-28 13:32:19 PDT
Created
attachment 134383
[details]
Patch
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
Comment on
attachment 134383
[details]
Patch
Attachment 134383
[details]
did not pass win-ews (win): Output:
http://queues.webkit.org/results/12156307
Build Bot
Comment 20
2012-03-28 16:08:42 PDT
Comment on
attachment 134383
[details]
Patch
Attachment 134383
[details]
did not pass mac-ews (mac): Output:
http://queues.webkit.org/results/12153495
Caio Marcelo de Oliveira Filho
Comment 21
2012-03-28 16:15:30 PDT
Created
attachment 134435
[details]
Patch
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
Comment on
attachment 134435
[details]
Patch
Attachment 134435
[details]
did not pass mac-ews (mac): Output:
http://queues.webkit.org/results/12157332
Caio Marcelo de Oliveira Filho
Comment 24
2012-03-29 05:53:11 PDT
Created
attachment 134556
[details]
Patch
Caio Marcelo de Oliveira Filho
Comment 25
2012-03-29 06:14:49 PDT
Created
attachment 134558
[details]
Patch
Build Bot
Comment 26
2012-03-29 06:55:39 PDT
Comment on
attachment 134558
[details]
Patch
Attachment 134558
[details]
did not pass mac-ews (mac): Output:
http://queues.webkit.org/results/12193301
Caio Marcelo de Oliveira Filho
Comment 27
2012-03-29 08:22:05 PDT
Created
attachment 134592
[details]
Patch
Caio Marcelo de Oliveira Filho
Comment 28
2012-03-29 09:25:09 PDT
Created
attachment 134599
[details]
Patch
Build Bot
Comment 29
2012-03-29 10:07:18 PDT
Comment on
attachment 134599
[details]
Patch
Attachment 134599
[details]
did not pass mac-ews (mac): Output:
http://queues.webkit.org/results/12212026
Caio Marcelo de Oliveira Filho
Comment 30
2012-03-29 10:11:40 PDT
Created
attachment 134610
[details]
Patch
Caio Marcelo de Oliveira Filho
Comment 31
2012-03-29 11:49:38 PDT
Committed
r112555
: <
http://trac.webkit.org/changeset/112555
>
Note
You need to
log in
before you can comment on or make changes to this bug.
Top of Page
Format For Printing
XML
Clone This Bug