Skip to content

Avoid zmalloc_size() in kvobjAllocSize() and approximate instead#14704

Open
skaslev wants to merge 1 commit intoredis:unstablefrom
skaslev:no-zmalloc-size
Open

Avoid zmalloc_size() in kvobjAllocSize() and approximate instead#14704
skaslev wants to merge 1 commit intoredis:unstablefrom
skaslev:no-zmalloc-size

Conversation

@skaslev
Copy link
Collaborator

@skaslev skaslev commented Jan 16, 2026

Avoid zmalloc_size() in kvobjAllocSize() and use approximation instead.

Since for ongoing key allocation histograms work (#14695) we need to call
kvobjAllocSize() more often on hot paths, using zmalloc_size() would cause
unnecessary performance overhead.


Note

Optimizes memory usage accounting for kv objects by avoiding allocator-sized queries on hot paths.

  • Reimplements kvobjAllocSize to approximate allocation size: sums sizeof(kvobj), metadata slots, embedded key header/SDS, and embedded string (for OBJ_ENCODING_EMBSTR), then adds per-type sizes
  • Adds debugServerAssert(o->iskvobj) to ensure the function is used only with kv objects

Written by Cursor Bugbot for commit 24f6116. This will update automatically on new commits. Configure here.

@skaslev skaslev requested a review from moticless January 16, 2026 11:58
@jit-ci
Copy link

jit-ci bot commented Jan 16, 2026

Hi, I’m Jit, a friendly security platform designed to help developers build secure applications from day zero with an MVS (Minimal viable security) mindset.

In case there are security findings, they will be communicated to you as a comment inside the PR.

Hope you’ll enjoy using Jit.

Questions? Comments? Want to learn more? Get in touch with us.

@jit-ci
Copy link

jit-ci bot commented Jan 16, 2026

❌ Security scan failed

Security scan failed: Branch no-zmalloc-size does not exist in the remote repository


💡 Need to bypass this check? Comment @sera bypass to override.

@augmentcode
Copy link

augmentcode bot commented Jan 16, 2026

🤖 Augment PR Summary

Summary: Replaces kvobjAllocSize()'s use of zmalloc_size() with an approximate size computation using the kvobj header, metadata prefix, embedded key SDS, and (for EMBSTR) inline value SDS.
Why: Reduces allocator-size-query overhead now that kvobjAllocSize() is called more frequently on hot paths for allocation-size/key histogram tracking.

🤖 Was this summary useful? React with 👍 or 👎

Copy link

@augmentcode augmentcode bot left a comment

Choose a reason for hiding this comment

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

Review completed. 1 suggestions posted.

Fix All in Augment

Comment augment review to trigger a new review at any time.

size_t kvobjAllocSize(kvobj *o) {
/* All kv-objects has at least kvobj header and embedded key */
size_t asize = zmalloc_size(kvobjGetAllocPtr(o));
debugServerAssert(o->iskvobj);
Copy link

Choose a reason for hiding this comment

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

Since robj and kvobj are typedefs of the same underlying struct, kvobjAllocSize() can be called with iskvobj==0; with debugServerAssert compiled out, kvobjGetKey()/sdsAllocSize() will read past the allocation and can crash.
Consider an always-on guard (e.g., serverAssert) or an unlikely(!o->iskvobj) safe fallback for release builds.

Fix This in Augment

🤖 Was this useful? React with 👍 or 👎

@ShooterIT ShooterIT added the action:run-benchmark Triggers the benchmark suite for this Pull Request label Jan 16, 2026
@fcostaoliveira
Copy link
Collaborator

CE Performance Automation : step 1 of 2 (build) STARTING...

This comment was automatically generated given a benchmark was triggered.
Started building at 2026-01-16 13:06:20.813857
You can check each build/benchmark progress in grafana:

  • git hash: 24f6116
  • git branch: skaslev:no-zmalloc-size
  • commit date and time: n/a
  • commit summary: n/a
  • test filters:
    • command priority lower limit: 0
    • command priority upper limit: 10000
    • test name regex: .*
    • command group regex: .*

@fcostaoliveira
Copy link
Collaborator

fcostaoliveira commented Jan 16, 2026

CE Performance Automation : step 2 of 2 (benchmark) RUNNING...

This comment was automatically generated given a benchmark was triggered.

Started benchmark suite at 2026-01-17 18:08:52.477198 and took 101.54245 seconds up until now.
Status: [--------------------------------------------------------------------------------] 0.37% completed.

In total will run 273 benchmarks.
- 272 pending.
- 1 completed:
- 1 successful.
- 0 failed.
You can check a the status in detail via the grafana link

@@ -1236,8 +1236,16 @@ size_t kvobjComputeSize(robj *key, kvobj *o, size_t sample_size, int dbid) {
}

size_t kvobjAllocSize(kvobj *o) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Add a comment explaining that this returns the requested size, not the actual allocated size.

Also verify that using here requested instead of allocated doesn't create kind of drift such that at one place we rely on allocation size and on another place on requested size.

@sundb sundb added this to Redis 8.8 Feb 10, 2026
@sundb sundb moved this from Todo to In Review in Redis 8.8 Feb 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

action:run-benchmark Triggers the benchmark suite for this Pull Request

Projects

Status: In Review

Development

Successfully merging this pull request may close these issues.

4 participants