@@ -13,38 +13,41 @@ Performance Considerations
1313Overview
1414--------
1515
16- In this guide, you can learn how to optimize performance of the {+driver-short+}.
17- To connect to MongoDB, you must create a ``Client`` instance. Your ``Client``
18- instance automatically handles most aspects of connection, such as discovering server topology, monitoring
19- your connection, and maintaining an internal connection pool.
20- This guide describes best practices to configure and use your ``Client`` instance.
16+ In this guide, you can learn how to optimize the performance of the
17+ {+driver-short+}. To connect to MongoDB, you must create a ``Client`` instance. Your ``Client``
18+ instance automatically handles most aspects of connection, such as
19+ discovering server topology, monitoring your connection, and maintaining
20+ an internal connection pool. This guide describes best practices for
21+ configuring and using your ``Client`` instance.
2122
2223.. _rust-performance-client-lifecycle:
2324
2425Client Lifecycle
2526----------------
2627
2728We recommend that you reuse your client across sessions and operations.
28- You can use the same ``Client`` instance to perform multiple tasks, as the ``Client`` type is safe for concurrent use by multiple threads.
29- Creating a new ``Client`` instance for each request results in slower performance.
29+ You can use the same ``Client`` instance to perform multiple tasks, as
30+ the ``Client`` type is safe for concurrent use by multiple threads.
31+ Creating a new ``Client`` instance for each request results in slower
32+ performance.
3033
31- The following code creates a method that accepts a pointer to an existing ``Client`` instance,
32- which allows you to execute many requests by using same client:
34+ The following code creates a method that accepts a pointer to an
35+ existing ``Client`` instance, which allows you to perform many requests
36+ by using the same client:
3337
3438.. literalinclude:: /includes/fundamentals/code-snippets/performance.rs
3539 :language: rust
3640 :dedent:
37- :start-after: start-perf-client-faster
38- :end-before: end-perf-client-faster
3941
4042.. _rust-performance-parallelism:
4143
4244Parallelism
4345-----------
4446
45- If you can run parallel data operations, you can optimize performance by running asynchronous, concurrent tasks.
46- The following code uses the ``task`` module from the ``tokio`` crate to create separate, concurrent
47- tasks for multiple data operations:
47+ If you can run parallel data operations, you can optimize performance by
48+ running asynchronous, concurrent tasks. The following code uses the
49+ ``spawn()`` method from the ``tokio::task`` module to create separate,
50+ concurrent tasks to perform insert operations:
4851
4952.. literalinclude:: /includes/fundamentals/code-snippets/performance-parallel.rs
5053 :language: rust
@@ -55,28 +58,31 @@ tasks for multiple data operations:
5558Runtime
5659-------
5760
58- A ``Client`` instance is bound to the instance of the ``tokio`` or ``async-std`` runtime in which you created it.
59- If you use a ``Client`` instance to perform operations on a different runtime, you might experience unexpected behavior or failures.
61+ A ``Client`` instance is bound to the instance of the ``tokio`` or
62+ ``async-std`` runtime in which you created it. If you use a ``Client``
63+ instance to perform operations on a different runtime, you might
64+ experience unexpected behavior or failures.
6065
61- If you are using the ``test`` helper macro from the ``tokio`` or ``async_std`` crate to test your application,
62- you might accidentally run operations on a different runtime than you intended.
63- This is because these helper macros create a new runtime for each test.
64- You can use one of the following strategies to avoid this issue:
66+ If use the ``test`` helper macro from the ``tokio`` or
67+ ``async_std`` crate to test your application, you might accidentally run
68+ operations on a different runtime than intended. This is because these
69+ helper macros create a new runtime for each test. However, you can use
70+ one of the following strategies to avoid this issue:
6571
6672- Attach the runtime to the ``Client`` instance without using the ``test`` helper macros.
6773- Create a new ``Client`` instance for every ``async`` test.
6874
6975This example follows the first strategy and creates a global runtime used only for testing.
70- In the following code, the ``test_list_dbs()`` method uses a client that manually connects to this runtime
71- to list databases in the deployment.
76+ In the following code, the ``test_list_dbs()`` method uses a client that
77+ manually connects to this runtime to list databases in the deployment:
7278
7379.. literalinclude:: /includes/fundamentals/code-snippets/performance-bundle-runtime.rs
7480 :language: rust
7581 :dedent:
7682
77- Implementing the second strategy,
78- the following code creates a new ``Client`` instance for each test run with ``tokio::test``,
79- ensuring that there is no unintended interaction between runtimes:
83+ Implementing the second strategy, the following code creates a new
84+ ``Client`` instance for each test run with ``tokio::test``,
85+ ensuring that there are no unintended interactions between runtimes:
8086
8187.. literalinclude:: /includes/fundamentals/code-snippets/performance-new-client.rs
8288 :language: rust
@@ -85,13 +91,15 @@ ensuring that there is no unintended interaction between runtimes:
8591Additional Information
8692----------------------
8793
88- For more information about the concepts in this guide, see the following pages:
94+ For more information about connecting to MongoDB, see the
95+ :ref:`Connection Guide <rust-connect-to-mongodb-client>`.
8996
90- - :ref:`Connection Guide <rust-connect-to-mongodb-client>`
97+ .. TODO link to runtimes page
9198
9299API Documentation
93100~~~~~~~~~~~~~~~~~
94101
95102- `Client() <{+api+}/struct.Client.html>`__
96-
97- .. TODO link to Async page when done
103+ - `spawn() <https://docs.rs/tokio/latest/tokio/task/fn.spawn.html>`__ in
104+ the ``tokio::task`` module
105+ - `tokio::runtime <https://docs.rs/tokio/latest/tokio/runtime/index.html>`__ module
0 commit comments