-
Notifications
You must be signed in to change notification settings - Fork 265
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
base: master
Are you sure you want to change the base?
Conversation
44cb87c
to
3017a8f
Compare
…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>
@@ -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) { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
sonic-swss-common/common/c-api/util.h
Lines 15 to 19 in 3017a8f
// 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)); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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) {
| ~~~~~~~~~~~~~~^
@erer1243 , can you help to resolve the conflict? |
d7056cb
to
3e49737
Compare
@lguohan should be good now |
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