Skip to content

Conversation

@vyavdoshenko
Copy link

This fix is to be used for dragonflydb/dragonfly#5480

Copy link

@romange romange left a comment

Choose a reason for hiding this comment

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

@dranikpg can you please reviews?

My thought is that it's possible to do it externally without making changes to the library itself.

@romange romange requested a review from dranikpg October 7, 2025 06:18
@dranikpg
Copy link

dranikpg commented Oct 8, 2025

As far as I could find, the is no way to compute the requested memory usage, neither the real memory usage (i.e. mi_good_size) as the library doesn't have control over this. What it does support is polymorphic allocators and we're already using them to get the total memory usage of all json objects.

A simpler solution in terms of code, but with higher overhead, would be "copying" the json object with a custom memory resource and counting the memory usage from within. Should be just a few lines of code inside Dragonfly... Though we'll end up copying the object

@romange
Copy link

romange commented Oct 8, 2025 via email

@vyavdoshenko
Copy link
Author

@romange
I changed the behavior, I pass a functor as a parameter. It decouples the code from mimalloc dependency.

// Callback type for getting usable size of allocated memory
using memory_size_callback = std::function<std::size_t(const void*)>;

// Recursive implementation of compute_memory_size with callback

Choose a reason for hiding this comment

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

Won't this blow the stack for large objects ? (fibers do not have a lot of stack memory)

Copy link
Author

Choose a reason for hiding this comment

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

Fixed. No recursion any longer.

@romange
Copy link

romange commented Oct 12, 2025

Do these tests fail without your changes as well?

@vyavdoshenko
Copy link
Author

Do these tests fail without your changes as well?

Yes, I didn't change any code in the library; I added only one method. My initial version was under the define macro, and tests failed, but the code should be the same as before. My guess is the CI should be set specifically.

Co-authored-by: Roman Gershman <romange@gmail.com>
Signed-off-by: Volodymyr Yavdoshenko <v.yavdoshenko@gmail.com>
// Optimization: only add elements that have dynamic memory
for (const auto& elem : arr)
{
auto kind = elem.storage_kind();
Copy link

Choose a reason for hiding this comment

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

why not use if (elem.capacity() > 0) here?

Copy link
Author

Choose a reason for hiding this comment

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

Fixed

const auto& value = member.value();
auto kind = value.storage_kind();
// Skip inline storage types (primitives, short strings, empty objects)
if (kind != json_storage_kind::null &&
Copy link

Choose a reason for hiding this comment

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

not only it's more verbose than calling capacity(), you also need to duplicate this.

Copy link
Author

Choose a reason for hiding this comment

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

Fixed

Copy link

@romange romange left a comment

Choose a reason for hiding this comment

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

LGTM

@vyavdoshenko vyavdoshenko requested a review from romange October 12, 2025 20:32
@vyavdoshenko
Copy link
Author

@romange
I don't have the right to merge this PR. Could you please merge it?

@romange romange merged commit 990b0a2 into dragonflydb:Dragonfly.178 Oct 13, 2025
3 of 54 checks passed
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.

4 participants