Closed Bug 1406303 Opened 7 years ago Closed 7 years ago

Refactor malloc_rtree_t

Categories

(Core :: Memory Allocator, enhancement)

enhancement
Not set
normal

Tracking

()

RESOLVED FIXED
mozilla58
Tracking Status
firefox58 --- fixed

People

(Reporter: glandium, Assigned: glandium)

Details

Attachments

(7 files)

      No description provided.
Comment on attachment 8915873 [details]
Bug 1406303 - Refactor malloc_rtree_get/set.

https://reviewboard.mozilla.org/r/187128/#review192574

::: memory/build/mozjemalloc.cpp:1762
(Diff revision 1)
>  
>  	return (ret);
>  }
>  
> -#define	MALLOC_RTREE_GET_GENERATE(f)					\
> -/* The least significant bits of the key are ignored. */		\
> +static inline void**
> +malloc_rtree_get_slot(malloc_rtree_t* aTree, uintptr_t aKey, bool aCreate=false)

Spaces around '='.

Is it worth commenting here that the tree must be locked before this function can be called?

::: memory/build/mozjemalloc.cpp:1776
(Diff revision 1)
> -	MALLOC_RTREE_LOCK(&rtree->lock);				\
> -	for (i = lshift = 0, height = rtree->height, node = rtree->root;\
> -	    i < height - 1;						\
> -	    i++, lshift += bits, node = child) {			\
> -		bits = rtree->level2bits[i];				\
> -		subkey = (key << lshift) >> ((SIZEOF_PTR << 3) - bits);	\
> +       i++, lshift += bits, node = child) {
> +    bits = aTree->level2bits[i];
> +    subkey = (aKey << lshift) >> ((SIZEOF_PTR << 3) - bits);
> +    child = (void**) node[subkey];
> +    if (!child && aCreate) {
> +      child = (void**) base_calloc(1 << aTree->level2bits[i+1], sizeof(void*));

Spaces around '+'.
Attachment #8915873 - Flags: review?(n.nethercote) → review+
Comment on attachment 8915874 [details]
Bug 1406303 - Turn malloc_rtree_t into a C++ class.

https://reviewboard.mozilla.org/r/187130/#review192576

::: memory/build/mozjemalloc.cpp:1770
(Diff revision 1)
> +  }
>  
> -	ret->root = (void**)base_calloc(1, sizeof(void *) << ret->level2bits[0]);
> -	if (!ret->root) {
> +  ret->mRoot = (void**)base_calloc(1 << ret->mLevel2Bits[0], sizeof(void*));
> +  if (!ret->mRoot) {
> -                /*
> +    /*
> -                 * We leak the rtree here, since there's no generic base
> +     * We leak the rtree here, since there's no generic base

I think this comment would fit on a single line, esp. if it uses C++ style.

::: memory/build/mozjemalloc.cpp:1795
(Diff revision 1)
>         i++, lshift += bits, node = child) {
> -    bits = aTree->level2bits[i];
> -    subkey = (aKey << lshift) >> ((SIZEOF_PTR << 3) - bits);
> +    bits = mLevel2Bits[i];
> +    subkey = (key << lshift) >> ((SIZEOF_PTR << 3) - bits);
>      child = (void**) node[subkey];
>      if (!child && aCreate) {
> -      child = (void**) base_calloc(1 << aTree->level2bits[i+1], sizeof(void*));
> +      child = (void**) base_calloc(1 << mLevel2Bits[i+1], sizeof(void*));

Spaces around '+'.
Attachment #8915874 - Flags: review?(n.nethercote) → review+
Comment on attachment 8915875 [details]
Bug 1406303 - Simplify the calculation of AddressRadixTree's bits_per_level.

https://reviewboard.mozilla.org/r/187132/#review192578

Thanks for the good commit message. Have you confirmed empirically that the result matches theory? :)
Attachment #8915875 - Flags: review?(n.nethercote) → review+
Comment on attachment 8915877 [details]
Bug 1406303 - Only store 2 levels of bit sizes for the radix tree.

https://reviewboard.mozilla.org/r/187136/#review192582

::: memory/build/mozjemalloc.cpp:661
(Diff revision 1)
>  #endif
>  
>    malloc_spinlock_t mLock;
>    void** mRoot;
>    unsigned mHeight;
> -  unsigned mLevel2Bits[1]; // Dynamically sized.
> +  unsigned mLevel2Bits[2];

It would be useful to have a comment here about why there are two entries.

Actually, I think it would be better to have two separate fields instead of a 2-element array. Can you do that?
Attachment #8915877 - Flags: review?(n.nethercote) → review+
> Actually, I think it would be better to have two separate fields instead of
> a 2-element array. Can you do that?

Oh, I see the next patch gets rid of the array.
Comment on attachment 8915878 [details]
Bug 1406303 - Make the number of significant bits used by the radix tree a template parameter.

https://reviewboard.mozilla.org/r/187138/#review192584

::: memory/build/mozjemalloc.cpp:669
(Diff revision 1)
>  #if (SIZEOF_PTR == 4)
>    static const size_t kNodeSize2Pow = 14;
>  #else
>    static const size_t kNodeSize2Pow = CACHELINE_2POW;
>  #endif
> +  static const size_t kBitsPerLevel = kNodeSize2Pow - SIZEOF_PTR_2POW;

I don't like this name because it's not clear that it excludes level 1. What about kBitsAtLevel2Plus? (Or something else where the 1 vs. 2+ distinction is clear.)
Attachment #8915878 - Flags: review?(n.nethercote) → review+
Comment on attachment 8915879 [details]
Bug 1406303 - Don't heap-allocate the global chunk radix tree.

https://reviewboard.mozilla.org/r/187140/#review192586
Attachment #8915879 - Flags: review?(n.nethercote) → review+
Comment on attachment 8915876 [details]
Bug 1406303 - Simplify the calculation of AddressRadixTree's height.

https://reviewboard.mozilla.org/r/187134/#review192580

Again: have you checked this empirically?
Attachment #8915876 - Flags: review?(n.nethercote) → review+
(In reply to Nicholas Nethercote [:njn] from comment #8)
> Is it worth commenting here that the tree must be locked before this
> function can be called?

We're actually only locking on write.
(In reply to Nicholas Nethercote [:njn] from comment #10)
> Comment on attachment 8915875 [details]
> Bug 1406303 - Simplify the calculation of AddressRadixTree's bits_per_level.
> 
> https://reviewboard.mozilla.org/r/187132/#review192578
> 
> Thanks for the good commit message. Have you confirmed empirically that the
> result matches theory? :)

As a matter of fact, I did.

(In reply to Nicholas Nethercote [:njn] from comment #15)
> Comment on attachment 8915876 [details]
> Bug 1406303 - Simplify the calculation of AddressRadixTree's height.
> 
> https://reviewboard.mozilla.org/r/187134/#review192580
> 
> Again: have you checked this empirically?

For this one, I actually went in the opposite direction: I had a hunch that it would work this way, checked empirically, and then worked out the math.
Pushed by mh@glandium.org:
https://hg.mozilla.org/integration/autoland/rev/e42bb7139c71
Refactor malloc_rtree_get/set. r=njn
https://hg.mozilla.org/integration/autoland/rev/7b2056d32ff5
Turn malloc_rtree_t into a C++ class. r=njn
https://hg.mozilla.org/integration/autoland/rev/cd3472504445
Simplify the calculation of AddressRadixTree's bits_per_level. r=njn
https://hg.mozilla.org/integration/autoland/rev/387202ecf011
Simplify the calculation of AddressRadixTree's height. r=njn
https://hg.mozilla.org/integration/autoland/rev/94b89dd64fc7
Only store 2 levels of bit sizes for the radix tree. r=njn
https://hg.mozilla.org/integration/autoland/rev/6ee5dedf4f64
Make the number of significant bits used by the radix tree a template parameter. r=njn
https://hg.mozilla.org/integration/autoland/rev/d57bcfffe5ba
Don't heap-allocate the global chunk radix tree. r=njn
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: