Skip to content

Commit 0ad5fe3

Browse files
committed
O(1) access to keys in HighLevelGraph
1 parent 405205a commit 0ad5fe3

File tree

3 files changed

+144
-3
lines changed

3 files changed

+144
-3
lines changed

Untitled.ipynb

Lines changed: 123 additions & 0 deletions
Large diffs are not rendered by default.

dask/blockwise.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ def blockwise(
5959
):
6060
"""Create a Blockwise symbolic mutable mapping
6161
62-
This is like the ``make_blockwise_graph`` function, but rather than construct a dict, it
63-
returns a symbolic Blockwise object.
62+
This is like the ``make_blockwise_graph`` function, but rather than construct a
63+
dict, it returns a symbolic Blockwise object.
6464
6565
See Also
6666
--------

dask/highlevelgraph.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,9 +476,27 @@ def from_collections(cls, name, layer, dependencies=()):
476476
return cls(layers, deps)
477477

478478
def __getitem__(self, key):
479+
# Attempt O(1) direct access first, under the assumption that layer names match
480+
# either the keys (Delayed, in most cases) or the first element of the key
481+
# tuples (Array, Bag, DataFrame, Series). This assumption could be wrong for
482+
# third-party collections and/or transforms, and is sometimes wrong for Delayed
483+
# objects too.
484+
try:
485+
return self.layers[key][key]
486+
except KeyError:
487+
pass
488+
try:
489+
return self.layers[key[0]][key]
490+
except (KeyError, IndexError, TypeError):
491+
pass
492+
493+
# Fall back to O(n) access
479494
for d in self.layers.values():
480-
if key in d:
495+
try:
481496
return d[key]
497+
except KeyError:
498+
pass
499+
482500
raise KeyError(key)
483501

484502
def __len__(self):

0 commit comments

Comments
 (0)