Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimise scheduler.get_comm_cost set difference #6931

Merged
merged 1 commit into from
Aug 23, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion distributed/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -2621,7 +2621,17 @@ def get_comm_cost(self, ts: TaskState, ws: WorkerState) -> float:
on the given worker.
"""
dts: TaskState
deps: set = ts.dependencies.difference(ws.has_what)
deps: set
if 10 * len(ts.dependencies) < len(ws.has_what):
# In the common case where the number of dependencies is
# much less than the number of tasks that we have,
# construct the set of deps that require communication in
# O(len(dependencies)) rather than O(len(has_what)) time.
# Factor of 10 is a guess at the overhead of explicit
# iteration as opposed to just calling set.difference
deps = {dep for dep in ts.dependencies if dep not in ws.has_what}
else:
deps = ts.dependencies.difference(ws.has_what)
Comment on lines +2625 to +2634
Copy link
Member

@fjetter fjetter Aug 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Micro benchmarking this, I get a factor of ~2 rather than 10

for dict_size in [100, 1_000, 10_000, 100_000, 1_000_000]:
    a_large_dict = {
        f"{ix}-{uuid.uuid4()}": "foo"
        for ix in range(dict_size)
    }    
    def timing(func):
        start = time.time_ns()
        iterations = 10
        for iteration in range(iterations):
            func()
        end = time.time_ns()
        return (end-start)/iterations
        
    for factor in [0.1, 0.4, 0.45, 0.5]:
        small_set = set(sample(a_large_dict.keys(), int(factor * dict_size)))
        intersect = timing(lambda: small_set.intersection(a_large_dict))
        iterate = timing(lambda: {k for k in small_set if k in a_large_dict})
        if iterate < intersect:
            print(f"Iterating faster for {dict_size=} and {factor=}")
Iterating faster for dict_size=100 and factor=0.1
Iterating faster for dict_size=1000 and factor=0.1
Iterating faster for dict_size=1000 and factor=0.4
Iterating faster for dict_size=1000 and factor=0.5
Iterating faster for dict_size=10000 and factor=0.1
Iterating faster for dict_size=10000 and factor=0.4
Iterating faster for dict_size=10000 and factor=0.45
Iterating faster for dict_size=100000 and factor=0.1
Iterating faster for dict_size=100000 and factor=0.4
Iterating faster for dict_size=100000 and factor=0.45
Iterating faster for dict_size=100000 and factor=0.5
Iterating faster for dict_size=1000000 and factor=0.1

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Conversely, on my (admittedly slightly antediluvian) Broadwell box, Python 3.9.13

Iterating faster for dict_size=100 and factor=0.1
Iterating faster for dict_size=1000 and factor=0.1
Iterating faster for dict_size=1000 and factor=0.4
Iterating faster for dict_size=10000 and factor=0.1
Iterating faster for dict_size=100000 and factor=0.1
Iterating faster for dict_size=1000000 and factor=0.1

nbytes: int = 0
for dts in deps:
nbytes += dts.nbytes
Expand Down