RFR: 8349211: Add support for intrusive trees to the utilities red-black tree [v5]

Casper Norrbin cnorrbin at openjdk.org
Thu Feb 6 15:30:10 UTC 2025


On Thu, 6 Feb 2025 15:27:32 GMT, Casper Norrbin <cnorrbin at openjdk.org> wrote:

>> Hi everyone,
>> 
>> The recently integrated red-black tree can be made more flexible by adding support of intrusive trees. In an intrusive tree, the user has full control over node allocation and placement instead of having the tree manage it internally.
>> 
>> Two key changes enable this feature:
>> 1. Nodes can now be created outside of the tree's internal allocation mechanism, enabling users to allocate and prepare nodes before inserting them into the tree.
>> 2. Cursors have been added to simplify navigation and iteration over the tree. These cursors are when inserting and removing nodes in an intrusive tree, where the internal tree allocator is not used. Additionally, cursors enable iteration over the tree and provide a convenient way to access node values.
>> 
>> 
>> Many of the auxiliary tree functions have been updated to use these new features, resulting in simplified and cleaned-up code. More tests have also been added to cover both new and existing functionality.
>> 
>> An example of how you could use the intrusive tree is found below:
>> 
>> ```c++
>>     struct MyIntrusiveStructure {
>>       Node node; // The tree node is part of an external structure
>>       int data;
>> 
>>       MyIntrusiveStructure(int data, Node node) : node(node), data(data) {}
>>       Node* get_node() { return &node; }
>>       static MyIntrusiveStructure* cast_to_self(Node* node) { return (MyIntrusiveStructure*)node; }
>>     };
>> 
>>     Tree my_intrusive_tree;
>> 
>>     Cursor insert_cursor = my_intrusive_tree.cursor_find(0);
>>     Node insert_node = Node(0);
>> 
>>     // Custom allocation here is just malloc
>>     MyIntrusiveStructure* place = (MyIntrusiveStructure*)os::malloc(sizeof(MyIntrusiveStructure), mtTest);
>>     new (place) MyIntrusiveStructure(0, insert_node);
>> 
>>     my_intrusive_tree.insert_at_cursor(place->get_node(), insert_cursor);
>> 
>>     Cursor find_cursor = my_intrusive_tree.cursor_find(0);
>>     int found_data = MyIntrusiveStructure::cast_to_self(find_cursor.node())->data;
>> 
>> 
>> 
>> Please let me know any feedback or concerns!
>
> Casper Norrbin has updated the pull request incrementally with one additional commit since the last revision:
> 
>   initialize node on insert + more tests

I changed the way `insert_at_cursor` so it initializes the node so we can have completely uninitialized memory. To do this, the key used when creating the cursor is now stored in it.

The example would now look something like this:
```c++
    struct MyIntrusiveStructure {
      Node node; // The tree node is part of an external structure
      int data;

      MyIntrusiveStructure(int data) : data(data) {}
      Node* get_node() { return &node; }
      static MyIntrusiveStructure* cast_to_self(Node* node) { return (MyIntrusiveStructure*)node; }
    };

    Tree my_intrusive_tree;
    Cursor insert_cursor = my_intrusive_tree.cursor_find(0);

    // Custom allocation here is just malloc
    MyIntrusiveStructure* place = (MyIntrusiveStructure*)os::malloc(sizeof(MyIntrusiveStructure), mtTest);
    new (place) MyIntrusiveStructure(123);

    my_intrusive_tree.insert_at_cursor(place->get_node(), insert_cursor);

    Cursor find_cursor = my_intrusive_tree.cursor_find(0);
    int found_data = MyIntrusiveStructure::cast_to_self(find_cursor.node())->data;

-------------

PR Comment: https://git.openjdk.org/jdk/pull/23416#issuecomment-2640143273


More information about the hotspot-dev mailing list