From a5ad974bec932c63ddc647c9986a513ae32ef663 Mon Sep 17 00:00:00 2001 From: Aleksey Shipilev Date: Mon, 28 Oct 2024 12:11:51 +0000 Subject: [PATCH] 8343056: C2: Micro-optimize Node lists grow Reviewed-by: kvn, redestad --- src/hotspot/share/libadt/vectset.cpp | 8 ++------ src/hotspot/share/libadt/vectset.hpp | 11 +++++++++-- src/hotspot/share/opto/node.cpp | 4 +--- src/hotspot/share/opto/node.hpp | 11 +++++++++-- 4 files changed, 21 insertions(+), 13 deletions(-) diff --git a/src/hotspot/share/libadt/vectset.cpp b/src/hotspot/share/libadt/vectset.cpp index b0d5d100400..e2eb08b1a34 100644 --- a/src/hotspot/share/libadt/vectset.cpp +++ b/src/hotspot/share/libadt/vectset.cpp @@ -49,9 +49,7 @@ void VectorSet::init(Arena* arena) { // Expand the existing set to a bigger size void VectorSet::grow(uint new_word_capacity) { _nesting.check(_set_arena); // Check if a potential reallocation in the arena is safe - if (new_word_capacity < _size) { - return; // No need to grow - } + assert(new_word_capacity >= _size, "Should have been checked before, use maybe_grow?"); assert(new_word_capacity < (1U << 30), ""); uint x = next_power_of_2(new_word_capacity); if (x > _data_size) { @@ -66,9 +64,7 @@ void VectorSet::grow(uint new_word_capacity) { void VectorSet::insert(uint elem) { uint32_t word = elem >> word_bits; uint32_t mask = 1U << (elem & bit_mask); - if (word >= _size) { - grow(word); - } + maybe_grow(word); _data[word] |= mask; } diff --git a/src/hotspot/share/libadt/vectset.hpp b/src/hotspot/share/libadt/vectset.hpp index eafa60db1fe..fee15566f24 100644 --- a/src/hotspot/share/libadt/vectset.hpp +++ b/src/hotspot/share/libadt/vectset.hpp @@ -49,8 +49,15 @@ private: ReallocMark _nesting; // Safety checks for arena reallocation void init(Arena* arena); + // Grow vector to required word capacity + void maybe_grow(uint new_word_capacity) { + if (new_word_capacity >= _size) { + grow(new_word_capacity); + } + } void grow(uint new_word_capacity); + public: VectorSet(); VectorSet(Arena* arena); @@ -78,7 +85,7 @@ public: // bool test_set(uint elem) { uint32_t word = elem >> word_bits; - grow(word); + maybe_grow(word); uint32_t mask = 1U << (elem & bit_mask); uint32_t data = _data[word]; _data[word] = data | mask; @@ -107,7 +114,7 @@ public: // Fast inlined set void set(uint elem) { uint32_t word = elem >> word_bits; - grow(word); + maybe_grow(word); uint32_t mask = 1U << (elem & bit_mask); _data[word] |= mask; } diff --git a/src/hotspot/share/opto/node.cpp b/src/hotspot/share/opto/node.cpp index 8dbf09f7d4f..3f82c472163 100644 --- a/src/hotspot/share/opto/node.cpp +++ b/src/hotspot/share/opto/node.cpp @@ -2770,9 +2770,7 @@ const RegMask &Node::in_RegMask(uint) const { void Node_Array::grow(uint i) { _nesting.check(_a); // Check if a potential reallocation in the arena is safe - if (i < _max) { - return; // No need to grow - } + assert(i >= _max, "Should have been checked before, use maybe_grow?"); assert(_max > 0, "invariant"); uint old = _max; _max = next_power_of_2(i); diff --git a/src/hotspot/share/opto/node.hpp b/src/hotspot/share/opto/node.hpp index 0afb3c9166b..821e1aa38a7 100644 --- a/src/hotspot/share/opto/node.hpp +++ b/src/hotspot/share/opto/node.hpp @@ -1610,7 +1610,14 @@ protected: Node** _nodes; ReallocMark _nesting; // Safety checks for arena reallocation - void grow( uint i ); // Grow array node to fit + // Grow array to required capacity + void maybe_grow(uint i) { + if (i >= _max) { + grow(i); + } + } + void grow(uint i); + public: Node_Array(Arena* a, uint max = OptoNodeListSize) : _a(a), _max(max) { _nodes = NEW_ARENA_ARRAY(a, Node*, max); @@ -1628,7 +1635,7 @@ public: Node* at(uint i) const { assert(i<_max,"oob"); return _nodes[i]; } Node** adr() { return _nodes; } // Extend the mapping: index i maps to Node *n. - void map( uint i, Node *n ) { grow(i); _nodes[i] = n; } + void map( uint i, Node *n ) { maybe_grow(i); _nodes[i] = n; } void insert( uint i, Node *n ); void remove( uint i ); // Remove, preserving order // Clear all entries in _nodes to null but keep storage