Skip to content
Snippets Groups Projects
Commit fa5c2bc0 authored by Richard van der Hoff's avatar Richard van der Hoff
Browse files

Reduce set building in get_entities_changed

This line shows up as about 5% of cpu time on a synchrotron:

    not_known_entities = set(entities) - set(self._entity_to_key)

Presumably the problem here is that _entity_to_key can be largeish, and
building a set for its keys every time this function is called is slow.

Here we rewrite the logic to avoid building so many sets.
parent 0456e059
No related branches found
No related tags found
No related merge requests found
......@@ -74,14 +74,18 @@ class StreamChangeCache(object):
assert type(stream_pos) is int
if stream_pos >= self._earliest_known_stream_pos:
not_known_entities = set(entities) - set(self._entity_to_key)
result = (
{self._cache[k] for k in self._cache.islice(
start=self._cache.bisect_right(stream_pos))}
.intersection(entities)
.union(not_known_entities)
)
changed_entities = {
self._cache[k] for k in self._cache.islice(
start=self._cache.bisect_right(stream_pos),
)
}
# we need to include entities which we don't know about, as well as
# those which are known to have changed since the stream pos.
result = {
e for e in entities
if e in changed_entities or e not in self._entity_to_key
}
self.metrics.inc_hits()
else:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment