Bug 73784 - Use HashMap<OwnPtr> in CollectionCache
Summary: Use HashMap<OwnPtr> in CollectionCache
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: DOM (show other bugs)
Version: 528+ (Nightly build)
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Darin Adler
URL:
Keywords:
Depends on:
Blocks: 73757
  Show dependency treegraph
 
Reported: 2011-12-04 15:25 PST by Darin Adler
Modified: 2013-05-07 08:57 PDT (History)
1 user (show)

See Also:


Attachments
Patch (7.90 KB, patch)
2011-12-04 15:27 PST, Darin Adler
kling: review+
Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Darin Adler 2011-12-04 15:25:33 PST
Use HashMap<OwnPtr> in CollectionCache
Comment 1 Darin Adler 2011-12-04 15:27:30 PST
Created attachment 117812 [details]
Patch
Comment 2 Andreas Kling 2011-12-04 18:35:29 PST
Comment on attachment 117812 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=117812&action=review

> Source/WebCore/html/HTMLCollection.cpp:344
> +            OwnPtr<Vector<Element*> >& nameVector = m_info->nameCache.add(nameAttrVal.impl(), nullptr).first->second;
> +            if (!nameVector)
> +                nameVector = adoptPtr(new Vector<Element*>);

Given how much this pattern repeats, it seems like we could factor them into functions, e.g ensureNameCache() and ensureIdCache().
That would also make the "// add to foo cache" comments even more redundant.
Comment 3 Darin Adler 2011-12-05 15:40:26 PST
Comment on attachment 117812 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=117812&action=review

>> Source/WebCore/html/HTMLCollection.cpp:344
>> +                nameVector = adoptPtr(new Vector<Element*>);
> 
> Given how much this pattern repeats, it seems like we could factor them into functions, e.g ensureNameCache() and ensureIdCache().
> That would also make the "// add to foo cache" comments even more redundant.

I think I can make a helper function that we can use at all 6 call sites.
Comment 4 Darin Adler 2011-12-06 09:30:06 PST
Committed r102147: <http://trac.webkit.org/changeset/102147>
Comment 5 Antti Koivisto 2011-12-06 09:48:45 PST
Wouldn't it be even better to just to use HashMap<AtomicStringImpl*, Vector<Element*> > ? Do we lack optimization for moving Vectors in HashMaps?
Comment 6 Darin Adler 2011-12-06 10:11:12 PST
(In reply to comment #5)
> Wouldn't it be even better to just to use HashMap<AtomicStringImpl*, Vector<Element*> > ? Do we lack optimization for moving Vectors in HashMaps?

I don’t know if it would be better. I don’t know how well we accommodate vectors when moving hash table entries around. Also, empty and deleted slots would use a lot more memory than they do for OwnPtr.
Comment 7 Antti Koivisto 2011-12-06 10:16:27 PST
(In reply to comment #6)
> I don’t know if it would be better. I don’t know how well we accommodate vectors when moving hash table entries around. Also, empty and deleted slots would use a lot more memory than they do for OwnPtr.

Empty vector is a pointer and a size_t. Considering that used values now use more memory the difference shouldn't be huge. Extra indirection is slower and the type looks uglier so it would be nice if the pure vector case was efficient too.
Comment 8 Darin Adler 2011-12-06 10:20:38 PST
(In reply to comment #7)
> (In reply to comment #6)
> > I don’t know if it would be better. I don’t know how well we accommodate vectors when moving hash table entries around. Also, empty and deleted slots would use a lot more memory than they do for OwnPtr.
> 
> Empty vector is a pointer and a size_t. Considering that used values now use more memory the difference shouldn't be huge. Extra indirection is slower and the type looks uglier so it would be nice if the pure vector case was efficient too.

Sure, sounds like a neat project.
Comment 9 Darin Adler 2011-12-06 10:22:26 PST
(In reply to comment #7)
> Empty vector is a pointer and a size_t.

I believe it’s a pointer (m_buffer) and two size_t's (m_capacity and m_size).
Comment 10 Antti Koivisto 2011-12-06 10:35:35 PST
(In reply to comment #9)
> (In reply to comment #7)
> > Empty vector is a pointer and a size_t.
> 
> I believe it’s a pointer (m_buffer) and two size_t's (m_capacity and m_size).

You are right. Optimizing m_capacity away from Vector in null case might be a good memory win.