PYTHON-4779 Use slots for Selection and TopologyDescription#2414
Closed
blink1073 wants to merge 3 commits intomongodb:masterfrom
Closed
PYTHON-4779 Use slots for Selection and TopologyDescription#2414blink1073 wants to merge 3 commits intomongodb:masterfrom
blink1073 wants to merge 3 commits intomongodb:masterfrom
Conversation
ShaneHarvey
reviewed
Jul 1, 2025
Member
ShaneHarvey
left a comment
There was a problem hiding this comment.
Could you add something like this to the changelog?
- Classes :class:`~bson.int64.Int64`, :class:`~bson.min_key.MinKey`,
:class:`~bson.max_key.MaxKey`, :class:`~bson.timestamp.Timestamp`,
:class:`~bson.regex.Regex`, and :class:`~bson.dbref.DBRef` all implement
``__slots__`` now. This means that their attributes are fixed, and new
attributes cannot be added to them at runtime.
Also is there any measurable benefit at all to this change? Like what if we have 1 thread just doing client._select_server? Or 100 threads?
Member
Author
|
I see no significant differences with this script between this PR and from pymongo import MongoClient
from pymongo.read_preferences import ReadPreference
from concurrent.futures import ThreadPoolExecutor, wait
client = MongoClient()
session = client.start_session()
read_preference = ReadPreference.PRIMARY
def test():
for i in range(100):
client._select_server(read_preference, session, "testOperation")
def test2_target():
client._select_server(read_preference, session, "testOperation")
def test2():
futures = []
with ThreadPoolExecutor(max_workers=100) as pool:
for i in range(100):
futures.append(pool.submit(test2_target))
wait(futures)
if __name__ == '__main__':
import timeit
print(timeit.timeit("test()", setup="from __main__ import test, client, read_preference, session", number=1000))
print(timeit.timeit("test2()", setup="from __main__ import test2, client, read_preference, session", number=1000)) |
Member
Author
|
Given that there is no measurable improvement, and Selection isn't even represented in our API docs, I think we should close this PR and ticket. |
Member
|
Closing sounds good to me. We should only do this if we can show it leads to a measurable improvement. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
I ran a perf build: https://spruce.mongodb.com/task/mongo_python_driver_performance_benchmarks_perf_8.0_standalone_patch_578c6c2ad2559c4c939c682151eeeaf3b847ab3e_68643055c8c88400072d3e1b_25_07_01_19_00_56/trend-charts?execution=0
I did not see an appreciable change in FindOneByID8Threads or SmallDocInsertOne, unfortunately.