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

C API: improve performance by not memcpy-ing string DB values across ffi boundary #935

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

erer1243
Copy link
Contributor

@erer1243 erer1243 commented Oct 30, 2024

This introduces a couple of new FFI types, representing owned C++ strings and arrays. We use those owned strings for database values, so they do not need to be memcpy'd across ffi boundaries. This is in response to Riff's concerns that database values which hamgrd will be reading may be large and expensive to memcpy.

Partner pr to sonic-net/sonic-dash-ha#11

r12f pushed a commit to sonic-net/sonic-dash-ha that referenced this pull request Nov 1, 2024
…FFI (#11)

partner PR to sonic-net/sonic-swss-common#935

Do we want to reorganize the files further? lmk

---------

Co-authored-by: erer1243 <erer1243@users.noreply.github.com>
@erer1243 erer1243 changed the title C API: no copy for DB values C API: improve performance by not memcpy-ing string DB values across ffi boundary Nov 1, 2024
@@ -37,14 +38,14 @@ int8_t SWSSDBConnector_del(SWSSDBConnector db, const char *key) {
SWSSTry(return ((DBConnector *)db)->del(string(key)) ? 1 : 0);
}

void SWSSDBConnector_set(SWSSDBConnector db, const char *key, const char *value) {
SWSSTry(((DBConnector *)db)->set(string(key), string(value)));
void SWSSDBConnector_set(SWSSDBConnector db, const char *key, SWSSStrRef value) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I can't find the definition of SWSSStrRef, where is the source code?

Copy link
Contributor Author

@erer1243 erer1243 Nov 4, 2024

Choose a reason for hiding this comment

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

Near the top of util.h

// FFI version of std::string&
// This can be converted to an SWSSString with a standard cast
// Functions that take SWSSString will move data out of the underlying string,
// but functions that take SWSSStrRef will only view it.
typedef struct SWSSStrRefOpaque *SWSSStrRef;

entry.field = strdup(pair.first.c_str());
entry.value = strdup(pair.second.c_str());
entry.value = makeString(std::move(pair.second));
Copy link
Contributor

Choose a reason for hiding this comment

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

Already move inside makeString(), maybe don't need move here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You need the move call there to be able to call the function. C++ won't automatically make an rvalue ref

common/c-api/util.h:160:39: error: cannot bind rvalue reference of type 'std::string&&' {aka 'std::__cxx11::basic_string<char>&&'} to lvalue of type 'std::__cxx11::basic_string<char>'
  160 |         entry.value = makeString(pair.second);
      |                                  ~~~~~^~~~~~
common/c-api/util.h:146:51: note:   initializing argument 1 of 'SWSSStringOpaque* swss::makeString(std::string&&)'
  146 | static inline SWSSString makeString(std::string &&s) {
      |                                     ~~~~~~~~~~~~~~^

@lguohan
Copy link
Contributor

lguohan commented Nov 9, 2024

@erer1243 , can you help to resolve the conflict?

lguohan
lguohan previously approved these changes Nov 9, 2024
@erer1243 erer1243 closed this Nov 9, 2024
@erer1243 erer1243 deleted the c-interface-nocopy branch November 9, 2024 18:10
@erer1243 erer1243 restored the c-interface-nocopy branch November 9, 2024 18:11
@erer1243 erer1243 reopened this Nov 9, 2024
@erer1243
Copy link
Contributor Author

erer1243 commented Nov 9, 2024

@lguohan should be good now

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.

3 participants