Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve red-black tree performance #131

Closed
wants to merge 2 commits into from
Closed

Conversation

EagleTw
Copy link
Collaborator

@EagleTw EagleTw commented May 18, 2023

map.[ch] is the C Implementation for C++ std::map using red-black tree. It works well, but there is room for improvements.

Close #29

src/map.c Fixed Show resolved Hide resolved
src/map.c Fixed Show fixed Hide fixed
src/map.c Fixed Show fixed Hide fixed
src/map.c Fixed Show fixed Hide fixed
src/map.c Fixed Show fixed Hide fixed
@jserv jserv changed the title Improve rb tree performance by introducing jemalloc rb Improve rbtree performance May 18, 2023
jserv

This comment was marked as outdated.

@jserv
Copy link
Contributor

jserv commented May 18, 2023

Squash git commits and write down precise commit messages.

Meanwhile, resolve the concerns raised by CodeQL.

@jserv
Copy link
Contributor

jserv commented May 21, 2023

TODO:

  • Resolve the concerns raised by CodeQL.
  • Provide the comprehensive benchmark to illustrate why we shall migrate to the proposed rbtree implementation
  • Implement the dedicated test suite for map.[ch].

@EagleTw EagleTw force-pushed the master branch 2 times, most recently from e1753a8 to 87c7f21 Compare May 21, 2023 08:04
src/map.c Outdated Show resolved Hide resolved
src/map.h Outdated Show resolved Hide resolved
src/map.c Outdated Show resolved Hide resolved
src/map.c Outdated Show resolved Hide resolved
src/map.h Outdated Show resolved Hide resolved
src/map.h Outdated Show resolved Hide resolved
src/map.h Outdated Show resolved Hide resolved
src/map.c Outdated Show resolved Hide resolved
internal_map_path_entry_t path[RB_MAX_DEPTH];
internal_map_path_entry_t *pathp;
rbt_node_init(node);
/* Wind. */
Copy link
Contributor

Choose a reason for hiding this comment

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

Improve the comment.

src/map.h Outdated Show resolved Hide resolved
src/map.c Outdated Show resolved Hide resolved
src/map.c Outdated Show resolved Hide resolved
rbtn_black_set(left);
if (pathp == path) {
rbtree->root = left;
/* Nothing to summarize -- the subtree rooted at the
Copy link
Contributor

Choose a reason for hiding this comment

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

See if "Nothing to summarize" makes sense or not. We shall ignore the handling of summary.

src/map.c Show resolved Hide resolved
@EagleTw EagleTw force-pushed the master branch 5 times, most recently from a72f62f to 0035e63 Compare May 21, 2023 12:40
src/map.c Outdated Show resolved Hide resolved
src/map.h Outdated Show resolved Hide resolved
src/map.h Outdated Show resolved Hide resolved
src/map.c Outdated Show resolved Hide resolved
src/map.c Outdated Show resolved Hide resolved
src/map.c Outdated Show resolved Hide resolved
src/map.h Outdated Show resolved Hide resolved
src/map.c Outdated Show resolved Hide resolved
* Each node in the red-black tree consumes at least 1 byte of space (for the
* linkage if nothing else, so there are a maximum of sizeof(void *) << 3 rb
* tree nodes in any process (and thus, at most sizeof(void *) << 3 nodes in any
* rb tree). The choice of algorithm bounds the depth of a tree to twice the
Copy link
Contributor

Choose a reason for hiding this comment

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

Improve the English writing.

src/map.c Outdated Show resolved Hide resolved
src/map.c Outdated Show resolved Hide resolved
src/map.c Outdated Show resolved Hide resolved
src/map.c Outdated Show resolved Hide resolved
jserv

This comment was marked as duplicate.

Copy link
Contributor

@jserv jserv left a comment

Choose a reason for hiding this comment

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

Prepare test program(s) for map.[ch] which appear in directory tests/map.
Example: https://github.com/sysprog21/rv32emu/tree/master/tests/cache

src/map.c Outdated Show resolved Hide resolved
@EagleTw EagleTw force-pushed the master branch 2 times, most recently from e40ed35 to 7541a26 Compare May 24, 2023 12:16
map_r_r(obj, node, parent, grandparent, uncle);
int cmp;
map_node_t *ret = rbtree->root;
while (ret && (cmp = (rbtree->cmp)(keynode->key, ret->key)) != 0) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Replace 0 with _CMP_EQUAL for readability.

path->node = rbtree->root;
for (pathp = path; pathp->node; pathp++) {
int cmp = pathp->cmp = (rbtree->cmp)(node->key, pathp->node->key);
if (cmp == 0) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I would propose the following:

    switch (cmp) {
    case _CMP_LESS:
        pathp[1].node = rbnode_get_left(pathp->node);
        break;
    case _CMP_GREATER:
        pathp[1].node = rbnode_get_right(pathp->node);
        break;
    default: /* _CMP_EQUAL */
        break;
    }

/* If there is no left child, keep going up until there is a left child */
it->prev = it->node;
it->node = rb_parent(it->node);
map_t tree = (map_t) malloc(sizeof(map_head_t));
Copy link
Contributor

Choose a reason for hiding this comment

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

No cast is required. That is,

map_t tree = malloc(sizeof(map_head_t));

@jserv
Copy link
Contributor

jserv commented May 25, 2023

Close in favor of incoming revised pull request for reviewing.

@jserv jserv closed this May 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve red-black tree implementation in map
2 participants