Skip to content

rgw/dbstore: use string_view for string constants - #45109

Merged
cbodley merged 5 commits into
ceph:masterfrom
cbodley:wip-rgw-dbstore-strings
Feb 24, 2022
Merged

cbodley merged 5 commits into
ceph:masterfrom
cbodley:wip-rgw-dbstore-strings

Conversation

@cbodley

@cbodley cbodley commented Feb 21, 2022

Copy link
Copy Markdown
Contributor

taking this example:

class DeleteObjectOp: virtual public DBOp {
  private:
    const std::string Query =
      "DELETE from '{}' where BucketName = {} and ObjName = {} and ObjInstance = {}";

here, the string literal "DELETE from ..." gets stored in the data segment of the executable. but each time a RemoveUserOp is constructed, the std::string has to allocate memory and make a copy of that string literal

making this a static const std::string is a slight improvement, because this allocation/copy only happens once when the process starts up. but when there are a lot of these static strings, it takes time to initialize them all. and because this is linked into radosgw/radosgw-admin, it would initialize them even when we aren't using dbstore

what we really want is a pointer directly to the string literal. so either a raw pointer:

static const char* const Query =
  "DELETE from '{}' where BucketName = {} and ObjName = {} and ObjInstance = {}";

or a std::string_view, which is a pointer and length:

static constexpr std::string_view Query =
  "DELETE from '{}' where BucketName = {} and ObjName = {} and ObjInstance = {}";

there are several tradeoffs to consider here. the biggest one is that std::string_view (as a pointer+length) does not represent a null-terminated string, so has no c_str() function for use with c APIs that take const char*. that means it's mostly for use in pure c++

in this PR, i've only replaced the constant std::strings that are passed to fmt::format(), which understands std::string_view. there are still a lot of static strings, especially in the *PrepareInfo structs, which are better suited to const char* for use in the sqlite3 interfaces

p.s. incidentially, this also resolves lots of compilation failures with c++20 enabled

Show available Jenkins commands
  • jenkins retest this please
  • jenkins test classic perf
  • jenkins test crimson perf
  • jenkins test signed
  • jenkins test make check
  • jenkins test make check arm64
  • jenkins test submodules
  • jenkins test dashboard
  • jenkins test dashboard cephadm
  • jenkins test api
  • jenkins test docs
  • jenkins render docs
  • jenkins test ceph-volume all
  • jenkins test ceph-volume tox

@cbodley
cbodley requested a review from soumyakoduri February 21, 2022 16:29
@github-actions github-actions Bot added the rgw label Feb 21, 2022
@cbodley
cbodley force-pushed the wip-rgw-dbstore-strings branch from f175467 to efad4cc Compare February 21, 2022 16:43
@cbodley

cbodley commented Feb 21, 2022

Copy link
Copy Markdown
Contributor Author

added a couple extra commits, one addressing this part:

there are still a lot of static strings, especially in the *PrepareInfo structs, which are better suited to const char* for use in the sqlite3 interfaces

@cbodley
cbodley force-pushed the wip-rgw-dbstore-strings branch from f8faf99 to 6839b5f Compare February 21, 2022 17:21
these functions only refer to static member variables, so can be static
themselves

Signed-off-by: Casey Bodley <[email protected]>
all string constants passed to sqlite3_bind_parameter_index() are now
`static constexpr const char*` instead of `std::string`

Signed-off-by: Casey Bodley <[email protected]>
in `SQLiteDB::InitPrepareParams()`, `DBOpInfo::query_str` is assigned to
`DBOpPrepareInfo::query_str`:

  p_params.op.query_str = params->op.query_str;

by making `DBOpPrepareInfo::query_str` a view, we avoid allocating a
copy of the `std::string`

Signed-off-by: Casey Bodley <[email protected]>
@cbodley
cbodley force-pushed the wip-rgw-dbstore-strings branch from 6839b5f to 146a926 Compare February 21, 2022 18:21
@cbodley cbodley mentioned this pull request Feb 23, 2022
1 task done
@cbodley

cbodley commented Feb 24, 2022

Copy link
Copy Markdown
Contributor Author

@soumyakoduri do you think this needs a teuthology run? the unit tests are passing in 'make check', and i confirmed that the packages built successfully in https://shaman.ceph.com/builds/ceph/wip-rgw-dbstore-strings/

@soumyakoduri

Copy link
Copy Markdown
Contributor

@soumyakoduri do you think this needs a teuthology run? the unit tests are passing in 'make check', and i confirmed that the packages built successfully in https://shaman.ceph.com/builds/ceph/wip-rgw-dbstore-strings/

Thats seems enough. Anyways there are no teuthology tests added for DBStore yet. I think this PR can be merged.

@cbodley
cbodley merged commit c209b50 into ceph:master Feb 24, 2022
@cbodley
cbodley deleted the wip-rgw-dbstore-strings branch February 24, 2022 19:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants