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

chore: introduce sorted_map #1558

Merged
merged 1 commit into from
Jul 18, 2023
Merged

chore: introduce sorted_map #1558

merged 1 commit into from
Jul 18, 2023

Conversation

romange
Copy link
Collaborator

@romange romange commented Jul 17, 2023

Consolidate skiplist based zset functionality into a dedicated class.

@romange romange requested a review from chakaz July 17, 2023 17:16

namespace dfly {

class ZSetTest : public ::testing::Test {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

this unit test is not directly related to this Pr but it shows the potential of using a different data structure than skiplist. In this case I compared it to absl::btree.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe add a comment saying that this is not a real unit-test (in that it doesn't CHECK or EXPECT anything), but rather a way to compare zset and absl::btree_map? Maybe elaborate more on what exactly you're trying to compare (memory usage?)

@romange romange force-pushed the Pr1 branch 3 times, most recently from 4015ec5 to 38d42c7 Compare July 17, 2023 17:55

namespace detail {

class SortedMap {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please add a comment roughly describing what this class is. "Sorted map, implemented using Redis' dict / ..."

SortedMap();
~SortedMap();

static SortedMap* FromListPack(const uint8_t* lp);
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd strongly recommend returning by-value. This class is very small in size, and so that shouldn't be a problem.
Further, you can even make this class movable quite easily, which may be useful.

If you choose to disagree, please document who owns the returned instance. Consider returning unique_ptr if owned by caller.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The reason I return a pointer is because the context demands so. I have no use for this object on the stack because I must pass its pointer into the CompactObject.

Please note that while it looks like a new class, it's really about consolidation of dispersed code under a single entity.
I will add an ownership comment.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd return a unique_ptr rather than document it, it's self documenting and safer. It also won't have any performance implications (just call .release() from the callsite)

}

namespace dfly {

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd remove this empty line

Comment on lines 87 to 91
int incr = (in_flags & ZADD_IN_INCR) != 0;
int nx = (in_flags & ZADD_IN_NX) != 0;
int xx = (in_flags & ZADD_IN_XX) != 0;
int gt = (in_flags & ZADD_IN_GT) != 0;
int lt = (in_flags & ZADD_IN_LT) != 0;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Make all of these const bool?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

ok, but again - i just copied the code.

src/core/zset_test.cc Outdated Show resolved Hide resolved
zskiplist* zsl = zslCreate();
LOG(INFO) << "zskiplist before: " << zmalloc_used_memory_tl << " bytes";

for (int i = 0; i < 10000; ++i) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Consider using 10'000 syntax


namespace dfly {

class ZSetTest : public ::testing::Test {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe add a comment saying that this is not a real unit-test (in that it doesn't CHECK or EXPECT anything), but rather a way to compare zset and absl::btree_map? Maybe elaborate more on what exactly you're trying to compare (memory usage?)

@romange romange force-pushed the Pr1 branch 3 times, most recently from 48cdc11 to 09741c5 Compare July 17, 2023 22:06
SortedMap();
~SortedMap();

static SortedMap* FromListPack(const uint8_t* lp);
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd return a unique_ptr rather than document it, it's self documenting and safer. It also won't have any performance implications (just call .release() from the callsite)

Comment on lines 36 to 38
SortedMap(SortedMap&& other) noexcept = default;
SortedMap& operator=(SortedMap&& other) noexcept = default;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Actually I don't think that the default move operations are a good fit here, as they won't nullify the members of the moved-from object.
If this class, like you implied, is only ever used on the heap then you can delete those as well. If you do think it might be useful to have these movable, then I think you'll have to implement these operations yourself, and make sure that in case the members are nullptr the d'tor works as expected.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

good catch

@romange romange force-pushed the Pr1 branch 2 times, most recently from 938dfbd to 8eb67bb Compare July 18, 2023 05:49
Consolidate skiplist based zset functionality into a dedicated class
called SortedMap. The code is adapted from t_zset.c
The old code in t_zset.c is deleted.

Signed-off-by: Roman Gershman <[email protected]>
@romange romange merged commit e5be30c into main Jul 18, 2023
@romange romange deleted the Pr1 branch July 18, 2023 06:56
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.

2 participants