Bug 205834 - [bmalloc] Extract constants from Heap and share it among Heaps.
Summary: [bmalloc] Extract constants from Heap and share it among Heaps.
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: bmalloc (show other bugs)
Version: WebKit Nightly Build
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Basuke Suzuki
URL:
Keywords: InRadar
Depends on:
Blocks:
 
Reported: 2020-01-06 14:31 PST by Basuke Suzuki
Modified: 2020-01-09 14:58 PST (History)
10 users (show)

See Also:


Attachments
PATCH (23.17 KB, patch)
2020-01-07 10:37 PST, Basuke Suzuki
no flags Details | Formatted Diff | Diff
PATCH (23.48 KB, patch)
2020-01-07 10:50 PST, Basuke Suzuki
no flags Details | Formatted Diff | Diff
PATCH (23.78 KB, patch)
2020-01-08 17:08 PST, Basuke Suzuki
no flags Details | Formatted Diff | Diff
PATCH (21.56 KB, patch)
2020-01-09 13:15 PST, Basuke Suzuki
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Basuke Suzuki 2020-01-06 14:31:11 PST
The following member variables of Heap are dependent only for vmPageSize and calculating same value for each Heap.
  m_vmPageSizePhysical, m_smallLineMetadata and m_pageClasses

Extract them as a separate class and access them from each Heap reduces sizeof(Heap) for m_pageClasses and Vector for m_smallLineMetadata.
Comment 1 Basuke Suzuki 2020-01-07 10:37:36 PST
Created attachment 386991 [details]
PATCH

- Extracted member variables for metadata and initialization code of those.
- Merge LineMetadata.h and Metadata.h
- Add simple wrapper class to get metadata for specific sizeClass.
Comment 2 Basuke Suzuki 2020-01-07 10:50:50 PST
Created attachment 386995 [details]
PATCH

build fix
Comment 3 Basuke Suzuki 2020-01-07 10:51:37 PST
The style error is false positive and it will be solved by https://bugs.webkit.org/show_bug.cgi?id=205840 .
Comment 4 Basuke Suzuki 2020-01-07 15:06:31 PST
jsc fails because it uses old version of forwarding header. Heap.h doesn't include LineMetadata.h any more and the line doesn't exist.
Comment 5 Basuke Suzuki 2020-01-08 17:08:17 PST
Created attachment 387165 [details]
PATCH

Add ChangeLog description.
Comment 6 Yusuke Suzuki 2020-01-08 17:18:09 PST
(In reply to Basuke Suzuki from comment #4)
> jsc fails because it uses old version of forwarding header. Heap.h doesn't
> include LineMetadata.h any more and the line doesn't exist.

I think the problem is that "bmalloc.h" and "Heap.h" are not copied correctly via CpHeader. Can you make it work?
Comment 7 Geoffrey Garen 2020-01-08 17:19:21 PST
What's the benefit here?

Heap is a per-process singleton, so I don't understand the value of moving its data into another per-process singleton.
Comment 8 Geoffrey Garen 2020-01-08 17:20:28 PST
Side note: I think the word "metadata" is too generic. Can you think of a name that describes the data recorded here?
Comment 9 Basuke Suzuki 2020-01-08 17:28:55 PST
(In reply to Geoffrey Garen from comment #7)
> What's the benefit here?
> 
> Heap is a per-process singleton, so I don't understand the value of moving
> its data into another per-process singleton.

Heap is created as PerProcess<PerHeapKind>() singleton. So four copies of pageClass and smallLineMetadata tables are created. Also smallLineMetadata is a Vector, so it allocates another page for each Heap kind.
Comment 10 Basuke Suzuki 2020-01-08 17:31:07 PST
(In reply to Geoffrey Garen from comment #8)
> Side note: I think the word "metadata" is too generic. Can you think of a
> name that describes the data recorded here?

How about HeapMetadata? Or I can merge them into Heap.(h|cpp) if that is more suitable.
Comment 11 Basuke Suzuki 2020-01-08 17:32:12 PST
(In reply to Yusuke Suzuki from comment #6)
> (In reply to Basuke Suzuki from comment #4)
> > jsc fails because it uses old version of forwarding header. Heap.h doesn't
> > include LineMetadata.h any more and the line doesn't exist.
> 
> I think the problem is that "bmalloc.h" and "Heap.h" are not copied
> correctly via CpHeader. Can you make it work?

Does jsc build use different path than other Xcode targets?
Comment 12 Basuke Suzuki 2020-01-08 17:33:35 PST
Just in case this is the goal of this patch. I'm working on making those values `constxpr`ed for specific VM page sizes.

https://github.com/WebKit/webkit/compare/master...basuke:basuke/bmalloc-metadata-constexpr?expand=1#diff-590a267487c8ee077df1036f99e1c38cR75-R98
Comment 13 Yusuke Suzuki 2020-01-08 17:39:12 PST
Yes, Heap exists at least three due to Gigacage IIRC (Primitive, JSValue, FastMalloc).
And IIRC, Metadata takes measurable size (I noticed that when doing memory reduction work. If we avoid Heap initialization completely when Malloc=1 is enabled (we use DebugHeap), we get large improvement in RAMification LuaJSFight).
Comment 14 Geoffrey Garen 2020-01-08 21:03:34 PST
Comment on attachment 387165 [details]
PATCH

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

> Source/bmalloc/bmalloc/Heap.cpp:284
> -        size_t pageClass = m_pageClasses[sizeClass];
> +        Metadata metadata { sizeClass };
> +        size_t pageClass = metadata.pageClass();

This idiom surprised me. There's nothing about the Metadata class name that says to me "I will look up the right page class for a size class upon initialization".

What do you think about "m_constants.pageClass(sizeClass)" instead?

> Source/bmalloc/bmalloc/Metadata.hSource/bmalloc/bmalloc/LineMetadata.h:42
> +class MetadataStorage : public StaticPerProcess<MetadataStorage> {

How about if we call this class "HeapConstants", since it's the factored out set of constants shared by all heaps -- and then keep a reference m_heapConstants in each Heap?
Comment 15 Basuke Suzuki 2020-01-09 10:10:30 PST
Comment on attachment 387165 [details]
PATCH

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

>> Source/bmalloc/bmalloc/Heap.cpp:284
>> +        size_t pageClass = metadata.pageClass();
> 
> This idiom surprised me. There's nothing about the Metadata class name that says to me "I will look up the right page class for a size class upon initialization".
> 
> What do you think about "m_constants.pageClass(sizeClass)" instead?

I agree with that. Actually that was my first implementation. I saw lot of sizeClass passing to get value from metadata, so I decided to make helper class. But yes that idiom is not regular.

>> Source/bmalloc/bmalloc/Metadata.hSource/bmalloc/bmalloc/LineMetadata.h:42
>> +class MetadataStorage : public StaticPerProcess<MetadataStorage> {
> 
> How about if we call this class "HeapConstants", since it's the factored out set of constants shared by all heaps -- and then keep a reference m_heapConstants in each Heap?

HeapConstants for class name and having a reference to that instance in Heap are good to me. I want to call it m_constants because it's already used in Heap object.
Comment 16 Basuke Suzuki 2020-01-09 13:15:39 PST
Created attachment 387259 [details]
PATCH
Comment 17 Geoffrey Garen 2020-01-09 13:40:09 PST
Comment on attachment 387259 [details]
PATCH

r=me
Comment 18 WebKit Commit Bot 2020-01-09 14:57:51 PST
Comment on attachment 387259 [details]
PATCH

Clearing flags on attachment: 387259

Committed r254303: <https://trac.webkit.org/changeset/254303>
Comment 19 WebKit Commit Bot 2020-01-09 14:57:53 PST
All reviewed patches have been landed.  Closing bug.
Comment 20 Radar WebKit Bug Importer 2020-01-09 14:58:18 PST
<rdar://problem/58459009>