[SERVER-1270] BSON library uses strlen to get the length of std::string objects Created: 21/Jun/10  Updated: 12/Jul/16  Resolved: 20/Jul/10

Status: Closed
Project: Core Server
Component/s: Internal Client
Affects Version/s: 1.5.3
Fix Version/s: 1.5.6

Type: Improvement Priority: Major - P3
Reporter: Andrew Morrow (Inactive) Assignee: Alberto Lerner
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

All


Participants:

 Description   

std::string objects have known lengths. In the current implementation of the BSON library, std::string objects used as keys or values are converted to const char*'s with std::string::c_str(), and then passed to the const char* overload:

/** add a subobject as a member */
BSONObjBuilder& append(const string& fieldName , BSONObj subObj) {
return append( fieldName.c_str() , subObj );
}

That eventually makes its way to BufBuilder::append, where it calls strlen, unnecessarily recomputing the length:

void append(const char *str) {
append((void*) str, strlen(str)+1);
}

Given that std::string objects have known lengths, the call to strlen is a complete waste of time.



 Comments   
Comment by auto [ 20/Jul/10 ]

Author:

{'login': 'alerner', 'name': 'Alberto Lerner', 'email': 'alerner@10gen.com'}

Message: SERVER-1270 Elim remaining c_str()'s
http://github.com/mongodb/mongo/commit/4ab45012de3f54ca09e4bf6738295d8f9279eef8

Comment by auto [ 20/Jul/10 ]

Author:

{'login': 'alerner', 'name': 'Alberto Lerner', 'email': 'alerner@10gen.com'}

Message: SERVER-1270 Elim c_str() /util
http://github.com/mongodb/mongo/commit/5a576ddbe0c79eda812d40b8b80c1344bacb3096

Comment by auto [ 20/Jul/10 ]

Author:

{'login': 'alerner', 'name': 'Alberto Lerner', 'email': 'alerner@10gen.com'}

Message: SERVER-1270 Elim c_str() /tools
http://github.com/mongodb/mongo/commit/37c9c403de1013c47d3aab5a75abc4b3323d66d9

Comment by auto [ 20/Jul/10 ]

Author:

{'login': 'alerner', 'name': 'Alberto Lerner', 'email': 'alerner@10gen.com'}

Message: SERVER-1270 Elim c_str() /s
http://github.com/mongodb/mongo/commit/4b2de232c9cf5bf0f227d97fe03c39d17a2e849b

Comment by auto [ 20/Jul/10 ]

Author:

{'login': 'alerner', 'name': 'Alberto Lerner', 'email': 'alerner@10gen.com'}

Message: SERVER-1270 Elim c_str() /client
http://github.com/mongodb/mongo/commit/ed0081deed460e5733681af5b12c7729dc464412

Comment by auto [ 20/Jul/10 ]

Author:

{'login': 'alerner', 'name': 'Alberto Lerner', 'email': 'alerner@10gen.com'}

Message: SERVER-1270 Elim c_str() /scripting
http://github.com/mongodb/mongo/commit/5227b6777f27acfac30006e4193033e2b33735bd

Comment by auto [ 20/Jul/10 ]

Author:

{'login': 'alerner', 'name': 'Alberto Lerner', 'email': 'alerner@10gen.com'}

Message: SERVER-1270 Elim c_str() /db
http://github.com/mongodb/mongo/commit/2b14da5f103a3d4e1a95c8d8bfa10a8470839bc1

Comment by auto [ 20/Jul/10 ]

Author:

{'login': 'alerner', 'name': 'Alberto Lerner', 'email': 'alerner@10gen.com'}

Message: SERVER-1270 Missed one call in BSONObjBuilder
http://github.com/mongodb/mongo/commit/67710484c107d2aac4533e87f1376f5e4638f713

Comment by auto [ 20/Jul/10 ]

Author:

{'login': 'alerner', 'name': 'Alberto Lerner', 'email': 'alerner@10gen.com'}

Message: SERVER-1270 Elim c_str() /db
http://github.com/mongodb/mongo/commit/67097550ed52d2536eb9902eda2a15e7b7366841

Comment by auto [ 20/Jul/10 ]

Author:

{'login': 'alerner', 'name': 'Alberto Lerner', 'email': 'alerner@10gen.com'}

Message: SERVER-1270 Hook StringData to BSONObjBuilder
http://github.com/mongodb/mongo/commit/27dde175e09492bb6230eadbb0e099d3c3a240e8

Comment by auto [ 19/Jul/10 ]

Author:

{'login': 'alerner', 'name': 'Alberto Lerner', 'email': 'alerner@10gen.com'}

Message: SERVER-1270 Fix example code
http://github.com/mongodb/mongo/commit/62c0c861b2a00633d9e5952b1ecb35c75c3c2bf1

Comment by auto [ 19/Jul/10 ]

Author:

{'login': 'alerner', 'name': 'Alberto Lerner', 'email': 'alerner@10gen.com'}

Message: SERVER-1270 Hook StringData to BufBuilder
http://github.com/mongodb/mongo/commit/f51a4d0669bf413d34e93c9a1c792f94edfdd757

Comment by auto [ 17/Jul/10 ]

Author:

{'login': 'alerner', 'name': 'Alberto Lerner', 'email': 'alerner@10gen.com'}

Message: Revert "SERVER-1270 Prep for hooking StringData"

This reverts commit b7c9a9fc576758ef6e7095ec0fbd2def0deb9b5b.
http://github.com/mongodb/mongo/commit/daf8c0fbd09f5730adb7511b882c1a5ba671eb08

Comment by auto [ 17/Jul/10 ]

Author:

{'login': 'alerner', 'name': 'Alberto Lerner', 'email': 'alerner@10gen.com'}

Message: SERVER-1270 Prep for hooking StringData
http://github.com/mongodb/mongo/commit/b7c9a9fc576758ef6e7095ec0fbd2def0deb9b5b

Comment by auto [ 14/Jul/10 ]

Author:

{'login': 'alerner', 'name': 'Alberto Lerner', 'email': 'alerner@10gen.com'}

Message: SERVER-1270 StringData unit test
http://github.com/mongodb/mongo/commit/16e0a0b65664b63991b0b8d01ecf9bebe449c39b

Comment by auto [ 07/Jul/10 ]

Author:

{'login': 'alerner', 'name': 'Alberto Lerner', 'email': 'alerner@10gen.com'}

Message: SERVER-1270 Go all the way const
http://github.com/mongodb/mongo/commit/7755d602c439050b519daf3a8012ab1411ccd9e5

Comment by auto [ 07/Jul/10 ]

Author:

{'login': 'alerner', 'name': 'Alberto Lerner', 'email': 'alerner@10gen.com'}

Message: SERVER-1270 Length uniform in all ctors (thanks Mathias)
http://github.com/mongodb/mongo/commit/13a235bcf39a2610f0314a701ceb75dd6db2aeb8

Comment by auto [ 07/Jul/10 ]

Author:

{'login': 'alerner', 'name': 'Alberto Lerner', 'email': 'alerner@10gen.com'}

Message: SERVER-1270 Improve StringData (on behalf of Andrew Morrow)
http://github.com/mongodb/mongo/commit/4bccb4792c8649b965fde46bb2c41a825526729f

Comment by Andrew Morrow (Inactive) [ 07/Jul/10 ]

Hi Alberto -

I like the string class. A couple comments:

  • Maybe make StringData immutable by making the data pointer and size const. That requires that the members be set up in initializer lists.
  • Add a templated ctor and an overload resolution tag so that you can construct a StringData from a compile time string literal without a call to strlen.

You end up with:

struct StringData {

const char* const data;
const unsigned size;

StringData( const char * c )
: data(c)
, size(strlen(c)) {}

StringData( const std::string& s )
: data(s.c_str())
, size(s.size()) {}

struct literal_tag {};
template<size_t N>
StringData( const char (&val)[N], literal_tag )
: data(&val[0])
, size(N) {}
};

Comment by auto [ 07/Jul/10 ]

Author:

{'login': 'alerner', 'name': 'Alberto Lerner', 'email': 'alerner@10gen.com'}

Message: SERVER-1270 A string class that can take both a strig and a char *. We'll apply next
http://github.com/mongodb/mongo/commit/e7cb7ecb5c8d17c622ca1f701a3ba31d04ff5c1a

Comment by Eliot Horowitz (Inactive) [ 06/Jul/10 ]

I think there are a lot more places we do this same thing in bsonobjbuilder.

We should probably add a helper

_appendElementStart( type , const string& name ){
...
}

almost everywhere we call .c_str() in bsonobjbuilder.h is inneficient

Comment by auto [ 06/Jul/10 ]

Author:

{'login': 'alerner', 'name': 'Alberto Lerner', 'email': 'alerner@10gen.com'}

Message: SERVER-1270 Make it a bit faster for std::string's
http://github.com/mongodb/mongo/commit/cc37c07bb561c5ad30e7b1a4fb68e88822dd13ec

Generated at Thu Feb 08 02:56:34 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.