Skip to content

Conversation

@pengxiaolong
Copy link

@pengxiaolong pengxiaolong commented Nov 12, 2025

Current alloc request type enum:

  enum Type {
    _alloc_shared, // Allocate common, outside of TLAB
    _alloc_shared_gc, // Allocate common, outside of GCLAB/PLAB
    _alloc_cds, // Allocate for CDS
    _alloc_tlab, // Allocate TLAB
    _alloc_gclab, // Allocate GCLAB
    _alloc_plab, // Allocate PLAB
    _ALLOC_LIMIT
  };

With current design, we have to use switch statement in multiple places resulting in unnecessary branches, for instance the function is_mutator_alloc:

  inline bool is_mutator_alloc() const {
    switch (_alloc_type) {
      case _alloc_tlab:
      case _alloc_shared:
      case _alloc_cds:
        return true;
      case _alloc_gclab:
      case _alloc_plab:
      case _alloc_shared_gc:
        return false;
      default:
        ShouldNotReachHere();
        return false;
    }
  }

In PR, I have re-designed the enum to make the function like is_mutator_alloc much simpler by making the values of the enum follow two simple rules:

  1. Smaller value for mutator alloc, larger value for gc alloc; GC alloc types are always greater than any of mutator alloc types.
  2. Odd for lab, even number for non-lab

Three functions have been simplified to one-line impl w/o branches in machine code:

  inline bool is_mutator_alloc() const {
    return _alloc_type <= _alloc_shared;
  }

  inline bool is_gc_alloc() const {
    return _alloc_type >= _alloc_shared_gc;
  }

  inline bool is_lab_alloc() const {
    return (_alloc_type & 1) == 1;
  }

I didn't check compiled assemble code of hotspot, in instead, I wrote similar/equivalent code and compile with gcc for comparison using godbolt.org:

bool is_lab_alloc(int alloc_type) {
    return (alloc_type & 1) == 1;
}

bool is_lab_alloc_switch(int alloc_type) {
    switch (alloc_type) {
        case 0:
        case 2: 
        case 4:
          return false;
        case 1:
        case 3:
        case 5:
          return true;
        default:
          throw "Should not reach here";

    }
}

x86_64 assembly code (https://godbolt.org/z/h7xfz8PaT):

is_lab_alloc(int):
        push    rbp
        mov     rbp, rsp
        mov     DWORD PTR [rbp-4], edi
        mov     eax, DWORD PTR [rbp-4]
        and     eax, 1
        and     eax, 1
        pop     rbp
        ret
.LC0:
        .string "Should not reach here"
is_lab_alloc_switch(int):
        push    rbp
        mov     rbp, rsp
        sub     rsp, 16
        mov     DWORD PTR [rbp-4], edi
        cmp     DWORD PTR [rbp-4], 5
        je      .L4
        cmp     DWORD PTR [rbp-4], 5
        jg      .L5
        cmp     DWORD PTR [rbp-4], 4
        je      .L6
        cmp     DWORD PTR [rbp-4], 4
        jg      .L5
        cmp     DWORD PTR [rbp-4], 3
        je      .L4
        cmp     DWORD PTR [rbp-4], 3
        jg      .L5
        cmp     DWORD PTR [rbp-4], 2
        je      .L6
        cmp     DWORD PTR [rbp-4], 2
        jg      .L5
        cmp     DWORD PTR [rbp-4], 0
        je      .L6
        cmp     DWORD PTR [rbp-4], 1
        je      .L4
        jmp     .L5
.L6:
        mov     eax, 0
        jmp     .L7
.L4:
        mov     eax, 1
        jmp     .L7
.L5:
        mov     edi, 8
        call    __cxa_allocate_exception
        mov     QWORD PTR [rax], OFFSET FLAT:.LC0
        mov     edx, 0
        mov     esi, OFFSET FLAT:typeinfo for char const*
        mov     rdi, rax
        call    __cxa_throw
.L7:
        leave
        ret

Test:

  • TEST=hotspot_gc_shenandoah
  • Tier 1

Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue

Issue

  • JDK-8371667: Shenandoah: Re-design alloc request type enum for better efficiency and cleaner code (Enhancement - P4)

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/28247/head:pull/28247
$ git checkout pull/28247

Update a local copy of the PR:
$ git checkout pull/28247
$ git pull https://git.openjdk.org/jdk.git pull/28247/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 28247

View PR using the GUI difftool:
$ git pr show -t 28247

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/28247.diff

Using Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Nov 12, 2025

👋 Welcome back xpeng! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk
Copy link

openjdk bot commented Nov 12, 2025

@pengxiaolong This change now passes all automated pre-integration checks.

ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details.

After integration, the commit message for the final commit will be:

8371667: Shenandoah: Re-design alloc request type enum for better efficiency and cleaner code

Reviewed-by: shade, kdnilsen

You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed.

At the time when this comment was updated there had been 184 new commits pushed to the master branch:

As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details.

➡️ To integrate this PR with the above commit message to the master branch, type /integrate in a new comment.

@openjdk openjdk bot added hotspot-gc hotspot-gc-dev@openjdk.org shenandoah shenandoah-dev@openjdk.org labels Nov 12, 2025
@openjdk
Copy link

openjdk bot commented Nov 12, 2025

@pengxiaolong The following labels will be automatically applied to this pull request:

  • hotspot-gc
  • shenandoah

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing lists. If you would like to change these labels, use the /label pull request command.

@pengxiaolong pengxiaolong marked this pull request as ready for review November 12, 2025 02:02
@openjdk openjdk bot added the rfr Pull request is ready for review label Nov 12, 2025
@mlbridge
Copy link

mlbridge bot commented Nov 12, 2025

Copy link
Member

@shipilev shipilev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly, I don't believe switching from explicit switch cases to bit manipulation is that much readable here. If you want to pursue this, then maybe do the proper bitmask manipulation, something like:

 // bit 0: mutator (0) or GC (1) alloc 
 // bit 1: LAB (0) or shared (1) alloc 
 // bit 2: if LAB, then GCLAB (0) or PLAB (1)
 // bit 3: if mutator, then normal (0) or CDS (1)
 typedef int AllocType;

 constexpr int bit_gc_alloc = 1 << 1;
 constexpr int bit_lab_alloc = 1 << 2;
 constexpr int bit_plab_alloc = 1 << 3;
 constexpr int bit_cds_alloc = 1 << 4;
 ...
 const bool is_lab_alloc(AllocType type) {
    return (type & bit_lab_alloc) != 0;
 }
 
 constexpr AllocType _alloc_tlab = bit_lab_alloc;
 constexpr AllocType _alloc_plab = bit_gc_alloc | bit_lab_alloc | bit_plab_alloc;
 ...

Remains to be seen what is the most sensible encoding scheme here.

@pengxiaolong
Copy link
Author

Honestly, I don't believe switching from explicit switch cases to bit manipulation is that much readable here. If you want to pursue this, then maybe do the proper bitmask manipulation, something like:

 // bit 0: mutator (0) or GC (1) alloc 
 // bit 1: LAB (0) or shared (1) alloc 
 // bit 2: if LAB, then GCLAB (0) or PLAB (1)
 // bit 3: if mutator, then normal (0) or CDS (1)
 typedef int AllocType;

 constexpr int bit_gc_alloc = 1 << 1;
 constexpr int bit_lab_alloc = 1 << 2;
 constexpr int bit_plab_alloc = 1 << 3;
 constexpr int bit_cds_alloc = 1 << 4;
 ...
 const bool is_lab_alloc(AllocType type) {
    return (type & bit_lab_alloc) != 0;
 }
 
 constexpr AllocType _alloc_tlab = bit_lab_alloc;
 constexpr AllocType _alloc_plab = bit_gc_alloc | bit_lab_alloc | bit_plab_alloc;
 ...

Remains to be seen what is the most sensible encoding scheme here.

Thanks you for the suggestion.

I didn't think about changing the enum to constexpr int for alloc request type, just wanted to re-shuffle the values of current enum, so I only used bit manipulation when test if it lab allocation. I'll update the PR and use proper bitmask manipulation as you suggested.

In terms of readability, explicit switch cases is definitely more readable, but machine code size will be also larger with more branches, so in theory it is less efficient.

Copy link
Member

@shipilev shipilev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that we are doing this... I do wonder if we want to fold _is_promotion and _affiliation into the same bitset?

@pengxiaolong
Copy link
Author

Now that we are doing this... I do wonder if we want to fold _is_promotion and _affiliation into the same bitset?

I'll take a look today and see if folding _is_promotion and _affiliation into the same bitset provides benefits we want.

if (ShenandoahHeapRegion::requires_humongous(req.size())) {
switch (req.type()) {
case ShenandoahAllocRequest::_alloc_shared:
case ShenandoahAllocRequest::_alloc_shared_gc:
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

humongous objects never move, not possible to be _alloc_shared_gc here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I am not sure why we have the case for _alloc_shared_gc here. But I would leave it for extra safety in this "optimization/cleanup" change. We can yank this case later.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Humongous objects do move under full gc, and may eventually move under concurrent GC with some refactoring.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added the line case ShenandoahAllocRequest::_alloc_shared_gc: back.
For full gc, we may move humongous objects in compaction, not by evacuation, so it won't call into ShenandoahFreeSet::allocate method, right?

@pengxiaolong
Copy link
Author

Now that we are doing this... I do wonder if we want to fold _is_promotion and _affiliation into the same bitset?

I'll take a look today and see if folding _is_promotion and _affiliation into the same bitset provides benefits we want.

I have made the change to fold _is_promotion and _affiliation into the same bitset of _alloc, but I am not 100% sure about whether we should do it or not.

Copy link
Member

@shipilev shipilev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bit confusing still. How about this:

[x|xx|xxx|xx]
           ^---- Requester:   
                     00 -- mutator
                     10 -- mutator (CDS)
                     01 -- GC
        ^------- Purpose:
                     00 -- shared
                     01 -- TLAB/GCLAB
                     11 -- PLAB
     ^---------- Affiliation: 
                     00 -- YOUNG
                     01 -- OLD
                     11 -- OLD, promotion

Then:

  static constexpr int bit_gc_alloc         = 1 << 0;
  static constexpr int bit_cds_alloc        = 1 << 1;
  static constexpr int bit_lab_alloc        = 1 << 2;
  static constexpr int bit_plab_alloc       = 1 << 3;
  static constexpr int bit_old_alloc        = 1 << 4;
  static constexpr int bit_promotion_alloc  = 1 << 5;

  static constexpr Type _alloc_shared              = 0;
  static constexpr Type _alloc_tlab                = bit_lab_alloc;
  static constexpr Type _alloc_cds                 = bit_cds_alloc;
  static constexpr Type _alloc_shared_gc           = bit_gc_alloc;
  static constexpr Type _alloc_shared_gc_old       = bit_gc_alloc | bit_old_alloc;
  static constexpr Type _alloc_shared_gc_promotion = bit_gc_alloc | bit_old_alloc | bit_promotion_alloc;
  static constexpr Type _alloc_gclab               = bit_gc_alloc | bit_lab_alloc;
  static constexpr Type _alloc_plab                = bit_gc_alloc | bit_plab_alloc | bit_old_alloc;     

@pengxiaolong
Copy link
Author

A bit confusing still. How about this:

[x|xx|xxx|xx]
           ^---- Requester:   
                     00 -- mutator
                     10 -- mutator (CDS)
                     01 -- GC
        ^------- Purpose:
                     00 -- shared
                     01 -- TLAB/GCLAB
                     11 -- PLAB
     ^---------- Affiliation: 
                     00 -- YOUNG
                     01 -- OLD
                     11 -- OLD, promotion

Then:

  static constexpr int bit_gc_alloc         = 1 << 0;
  static constexpr int bit_cds_alloc        = 1 << 1;
  static constexpr int bit_lab_alloc        = 1 << 2;
  static constexpr int bit_plab_alloc       = 1 << 3;
  static constexpr int bit_old_alloc        = 1 << 4;
  static constexpr int bit_promotion_alloc  = 1 << 5;

  static constexpr Type _alloc_shared              = 0;
  static constexpr Type _alloc_tlab                = bit_lab_alloc;
  static constexpr Type _alloc_cds                 = bit_cds_alloc;
  static constexpr Type _alloc_shared_gc           = bit_gc_alloc;
  static constexpr Type _alloc_shared_gc_old       = bit_gc_alloc | bit_old_alloc;
  static constexpr Type _alloc_shared_gc_promotion = bit_gc_alloc | bit_old_alloc | bit_promotion_alloc;
  static constexpr Type _alloc_gclab               = bit_gc_alloc | bit_lab_alloc;
  static constexpr Type _alloc_plab                = bit_gc_alloc | bit_plab_alloc | bit_old_alloc;     

Thanks Aleksey! This is good, I have applied the change, and fixed the wrong value for _alloc_plab, it should be bit_gc_alloc | bit_lab_alloc | bit_plab_alloc | bit_old_alloc.

Copy link
Member

@shipilev shipilev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, looks reasonable. A few more nits.

if (ShenandoahHeapRegion::requires_humongous(req.size())) {
switch (req.type()) {
case ShenandoahAllocRequest::_alloc_shared:
case ShenandoahAllocRequest::_alloc_shared_gc:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I am not sure why we have the case for _alloc_shared_gc here. But I would leave it for extra safety in this "optimization/cleanup" change. We can yank this case later.

@kdnilsen
Copy link
Contributor

kdnilsen commented Nov 17, 2025

Regarding the original code, was that release-build code, with optimization? That looks more like slowdebug code. I would have expected a computed goto rather than cascaded if-then-elses.

With computed-goto and with range checks, I suspect there may still be issues with branch-prediction efficiency in hardware. Do we see any measurable impact on performance?

@pengxiaolong
Copy link
Author

pengxiaolong commented Nov 17, 2025

Regarding the original code, was that release-build code, with optimization? That looks more like slowdebug code. I would have expected a computed goto rather than cascaded if-then-elses.

With computed-goto and with range checks, I suspect there may still be issues with branch-prediction efficiency in hardware. Do we see any measurable impact on performance?

It was release-build code, I used https://godbolt.org to generated the assembly code for comparison, the one in description is less optimization, here is the one with -O3 flag: https://godbolt.org/z/3317zYqYh

I am not really expecting much measurable impact on performance, but I did push the change to gitfram CI, here is the result from last run, I say kind of neutral:

Genshen
-------------------------------------------------------------------------------------------------------
+2.76% mnemonics/mnemonics_duration p=0.00019 (Wilcoxon)
  Control:      4.325s  (+/- 87.20ms)         64
  Test:         4.445s  (+/- 80.49ms)         64

-2.68% fj-kmeans/fj-kmeans_duration p=0.00000 (Wilcoxon)
  Control:      2.450s  (+/- 27.01ms)        120
  Test:         2.384s  (+/- 60.06ms)        120

-1.55% finagle-chirper/finagle-chirper_duration p=0.00000 (Wilcoxon)
  Control:      3.026s  (+/-132.97ms)        360
  Test:         2.979s  (+/-139.38ms)        360

-1.14% philosophers/philosophers_duration p=0.00286 (Wilcoxon)
  Control:      2.690s  (+/-163.97ms)        120
  Test:         2.659s  (+/-178.57ms)        120


Shenandoah
-------------------------------------------------------------------------------------------------------
+1.11% dotty/dotty_duration p=0.00000 (Wilcoxon)
  Control:      1.141s  (+/-103.32ms)        200
  Test:         1.153s  (+/-101.47ms)        200

-4.11% par-mnemonics/par-mnemonics_duration p=0.00009 (Wilcoxon)
  Control:      3.349s  (+/- 31.22ms)         64
  Test:         3.212s  (+/- 90.24ms)         64

-1.53% finagle-chirper/finagle-chirper_duration p=0.00000 (Wilcoxon)
  Control:      2.725s  (+/-121.14ms)        360
  Test:         2.683s  (+/-121.11ms)        360

-1.12% scrabble/scrabble_duration p=0.00000 (Wilcoxon)
  Control:    160.283ms (+/-  2.72ms)        200
  Test:       158.495ms (+/-  6.73ms)        200

Copy link
Contributor

@kdnilsen kdnilsen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this cleanup and these improvements. Just two minor suggestions.

Copy link
Member

@shipilev shipilev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks fine, assuming it passes tests.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Nov 18, 2025
Copy link
Contributor

@kdnilsen kdnilsen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks.

@shipilev
Copy link
Member

Note the test failures in GHA.

@openjdk openjdk bot removed the ready Pull request is ready to be integrated label Nov 21, 2025
@pengxiaolong
Copy link
Author

Note the test failures in GHA.

Thanks, I looked into the GHA test, it is strange that I only updated some methods and added the missing inline keywords, it has to be related to one of the methods, more likely the affiliation_name(), I'll figure it out.

@pengxiaolong
Copy link
Author

@shipilev @kdnilsen Now it works, the inline added to affiliation_name() caused some GHA failure on Windows.

Copy link
Member

@shipilev shipilev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still good.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Nov 24, 2025
@pengxiaolong
Copy link
Author

/integrate

@openjdk
Copy link

openjdk bot commented Nov 24, 2025

Going to push as commit e00dec5.
Since your change was applied there have been 197 commits pushed to the master branch:

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated Pull request has been integrated label Nov 24, 2025
@openjdk openjdk bot closed this Nov 24, 2025
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review labels Nov 24, 2025
@openjdk
Copy link

openjdk bot commented Nov 24, 2025

@pengxiaolong Pushed as commit e00dec5.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

hotspot-gc hotspot-gc-dev@openjdk.org integrated Pull request has been integrated shenandoah shenandoah-dev@openjdk.org

Development

Successfully merging this pull request may close these issues.

3 participants