Skip to content

Conversation

@abhijat
Copy link
Contributor

@abhijat abhijat commented Jan 29, 2026

The code is not hooked into jsoncons yet, it will be done in a small, final PR, kept separate because a lot of tests have to be adjusted at that point.

@abhijat abhijat force-pushed the abhijat/feat/ref-cnt-str branch 3 times, most recently from 1b99cf1 to 1b96eec Compare January 29, 2026 06:39
@abhijat abhijat force-pushed the abhijat/feat/ref-cnt-str branch 6 times, most recently from 2f0dad8 to 3269461 Compare January 29, 2026 08:18
@abhijat abhijat marked this pull request as ready for review January 29, 2026 08:19
Copilot AI review requested due to automatic review settings January 29, 2026 08:19
@abhijat abhijat force-pushed the abhijat/feat/ref-cnt-str branch from 3269461 to 9b3c115 Compare January 29, 2026 08:25
@augmentcode
Copy link

augmentcode bot commented Jan 29, 2026

🤖 Augment PR Summary

Summary: Introduces a new reference-counted string wrapper to support JSON key/value lifetimes on top of the existing interned-blob representation.

Changes:

  • Adds detail::InternedString, a RAII type that interns strings into a thread-local InternedBlobPool and manages blob refcounts on copy/move/destruction.
  • Extends InternedBlobHandle to better support empty values (empty handle, Size()=0, string_view conversion to "") and adds convenience operators.
  • Adds pool management helpers (InternedString::GetPoolRef(), ResetPool()) to control shutdown ordering vs. the thread-local memory resource.
  • Updates JSON build targets to compile the new implementation file.
  • Expands unit tests to validate interning behavior, refcounting, move/copy semantics, and the exposed string-like API expected by jsoncons.

Technical Notes: The PR explicitly does not wire InternedString into jsoncons yet; that integration (and associated test adjustments) is deferred to a follow-up PR.

🤖 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. 2 suggestions posted.

Fix All in Augment

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

@abhijat abhijat force-pushed the abhijat/feat/ref-cnt-str branch from 9b3c115 to 7cd8f4d Compare January 29, 2026 08:27
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR introduces InternedString, a reference-counted string class that wraps the InternedBlobHandle from PR #6456. The class manages string interning and reference counting for JSON keys, providing a memory-efficient way to store duplicate strings by maintaining a thread-local pool of unique string blobs. The implementation includes lifecycle management, string API compatibility with jsoncons, and comprehensive test coverage.

Changes:

  • Introduces InternedString class that manages reference counting and lifetimes for interned string blobs
  • Enhances InternedBlobHandle with comparison operators and explicit bool conversion for null checks
  • Removes the raw SetRefCount() method to enforce proper reference count management through the public API
  • Updates tests to use the new API and adds comprehensive tests for InternedString functionality

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.

Show a summary per file
File Description
src/core/json/detail/interned_string.h Defines the InternedString class with RAII semantics for reference-counted string interning, including constructors required by jsoncons
src/core/json/detail/interned_string.cc Implements reference counting logic (Acquire/Release), string interning with deduplication, and thread-local pool management
src/core/json/detail/interned_blob.h Adds comparison operators and explicit bool conversion; removes SetRefCount to enforce encapsulation
src/core/json/detail/interned_blob.cc Updates blob creation and accessors to handle empty strings/null pointers; removes SetRefCount implementation
src/core/json/interned_blob_test.cc Updates existing tests to use IncrRefCount instead of removed SetRefCount; adds comprehensive tests for InternedString including pool behavior, string API, and constructor edge cases
src/core/json/CMakeLists.txt Adds interned_string.cc to the build configuration

[[nodiscard]] static InternedBlobHandle Create(std::string_view sv);

uint32_t RefCount() const;
[[nodiscard]] uint32_t Size() const;
Copy link
Collaborator

Choose a reason for hiding this comment

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

I do not see value in adding [[nodiscard]] to all the accessors. the only function that matters imho is Create as it can potentially cause a leak

Copy link
Contributor Author

Choose a reason for hiding this comment

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

removed

}

int InternedString::compare(const InternedString& other) const {
return std::string_view{*this}.compare(other);
Copy link
Collaborator

Choose a reason for hiding this comment

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

move one-liners to header files

Copy link
Contributor Author

Choose a reason for hiding this comment

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

moved

@abhijat abhijat requested a review from romange January 29, 2026 10:47
@@ -0,0 +1,78 @@
// Copyright 2025, DragonflyDB authors. All rights reserved.
Copy link
Collaborator

Choose a reason for hiding this comment

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

btw, please change the date to 2026 in all the touched files.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

romange
romange previously approved these changes Jan 29, 2026
Copy link
Collaborator

@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

dranikpg
dranikpg previously approved these changes Jan 29, 2026
Copy link
Contributor

@dranikpg dranikpg left a comment

Choose a reason for hiding this comment

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

Seems fine from a design perspective

@abhijat abhijat enabled auto-merge (squash) January 30, 2026 06:43
@abhijat
Copy link
Contributor Author

abhijat commented Jan 30, 2026

Merging is blocked
Commits must have verified signatures.

I missed signature in a commit. Will have to rebase

A null blob is now allowed. The reason is that sometimes jsoncons will
create an empty string. We want to allow for empty strings without
carrying the "" and its metadata around, and without adding "" to the
pool.

For this reason a null blob and associated handle is allowed. It
provides short circuited accessors, but refcount on such a blob is a
programming error.

In addition comparison operators are added to be able to compare the
InternedString objects trivially.

Signed-off-by: Abhijat Malviya <[email protected]>
The string class wraps the previously added blob class. It decides at
creation time whether to create a new blob, or bump up the refcount of
an existing blob in the pool.

The pool is thread local, and is used by the strings on that thread to
store pointers to allocated blobs.

When a string is destroyed it decrements the ref-count, dropping the
blob if necessary (ie refcount == 0).

A lot of boilerplate in this class is from trial and error, adding
methods that jsoncons required to be able to use this class as a key.

Signed-off-by: Abhijat Malviya <[email protected]>
Signed-off-by: Abhijat Malviya <[email protected]>
Signed-off-by: Abhijat Malviya <[email protected]>
Signed-off-by: Abhijat Malviya <[email protected]>
Copilot AI review requested due to automatic review settings January 30, 2026 06:54
@abhijat abhijat dismissed stale reviews from dranikpg and romange via 99cf817 January 30, 2026 06:54
@abhijat abhijat force-pushed the abhijat/feat/ref-cnt-str branch from 95793e5 to 99cf817 Compare January 30, 2026 06:54
@abhijat abhijat disabled auto-merge January 30, 2026 07:01
@abhijat abhijat requested review from dranikpg and romange January 30, 2026 07:02
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 6 out of 6 changed files in this pull request and generated no new comments.

InternedBlobHandle::operator std::string_view() const {
DCHECK(blob_) << "Attempt to convert empty blob to string_view";
return {blob_, Size()};
return blob_ ? std::string_view{blob_, Size()} : "";
Copy link
Collaborator

Choose a reason for hiding this comment

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

i could be in header file.

@abhijat abhijat enabled auto-merge (squash) January 30, 2026 07:15
@abhijat abhijat merged commit 5ef19ff into main Jan 30, 2026
16 checks passed
@abhijat abhijat deleted the abhijat/feat/ref-cnt-str branch January 30, 2026 08:07
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