-
Type:
Improvement
-
Resolution: Unresolved
-
Priority:
Unknown
-
None
-
Affects Version/s: 2.5.7
-
Component/s: libmongoc
-
None
-
Not Needed
-
None
-
C Drivers
-
None
-
None
-
None
-
None
-
None
-
None
I run an NGSI-LD context broker (coraine) on libmongoc, and it issues many
small commands: single-document finds and updates, through
mongoc_collection_* and bulk operations. Profiling it against a local server
showed four places in libmongoc 2.5.x that do per-command work whose result
is not used on the common path. Fixing them keeps the behaviour of the
release. Together they cut the user-space instructions per request of my
application by 9.5 % for an update and 7.4 % for a find (perf stat, MongoDB
8.2.12, Linux x86-64).
1. Retry state created for every command. _mongoc_retryable_cmd_run()
creates a deprioritized-servers set and a retry-backoff generator (whose
constructor calls log() twice) for every command, and destroys them
afterwards. A command that succeeds at once, which is nearly every command,
uses neither. Creating them just before the first retry, after the
"attempt > allowed_retries" check, gives a retried command the same set and
the same backoff sequence.
Saves about 1,900 instructions per update, 750 per find.
2. retryReads / retryWrites looked up per command. They are read from the
client's URI (mongoc_uri_get_option_as_bool, a case-insensitive search of
the options) at about ten places in cluster, cmd, cursor and client code,
several times per command. A client's URI does not change after the client
is created, so reading both once into mongoc_client_t, as
enable_overload_retargeting already is, gives the same values.
Saves about 5,700 per update, 2,050 per find.
3. Four passes over every reply. _parse_error_reply() finds code, errmsg,
$err and writeConcernError with four bson_iter_init_find calls. Each one
walks a successful reply to its end, since a successful reply has none of
them. One walk recording the first occurrence of each, as bson_iter_find
does, gives the same result.
Saves about 7,700 per update, 3,200 per find.
4. Implicit sessions built like explicit ones. An implicit session
(mongoc_cmd_parts_assemble, _mongoc_cursor_run_command) is created through
mongoc_client_start_session(), which copies the client's default
transaction options and creates a jitter source. An implicit session never
starts a transaction, so it reads neither. A private
_mongoc_client_start_implicit_session() skips both, and the public function
is unchanged.
Saves about 1,800 per update, 770 per find.
Testing: test-libmongoc, the groups these changes touch (retryable_reads,
retryable_writes, retry_backoff_generator, Session, transactions, Client,
Cursor, Collection, BulkOperation, crud, command, Cluster, rpc_message):
909 tests, with the same result for every test with and without the
changes.
The changes are against 2.5.7 today. I will rebase them on master and open
one PR per change, unless you prefer another split.