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

New accumulator semantics #472

Merged
merged 13 commits into from
Mar 31, 2021
Merged

New accumulator semantics #472

merged 13 commits into from
Mar 31, 2021

Conversation

nsmith-
Copy link
Member

@nsmith- nsmith- commented Mar 25, 2021

Keeping the old accumulators intact but allowing new ones to be used in processor output

Keeping the old accumulators intact but allowing new ones to be used in
processor output
@nsmith- nsmith- marked this pull request as draft March 25, 2021 19:00
@nsmith- nsmith- marked this pull request as ready for review March 26, 2021 14:31
@nsmith- nsmith- requested a review from lgray March 26, 2021 14:32
@nsmith- nsmith- linked an issue Mar 26, 2021 that may be closed by this pull request
They need to stay as AccumulatorABC for backwards compatibility
in user code for now.
Copy link
Collaborator

@lgray lgray left a comment

Choose a reason for hiding this comment

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

One minor performance gripe and then one more important point on behavior. Otherwise the PR looks very good.

elif isinstance(a, MutableMapping) and isinstance(b, MutableMapping):
out = copy.copy(a)
out.clear()
for key in set(a) & set(b):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Not sure if it matters that much but minimizing the number of times we iterate over keys of a and b could be important for large mappings. The set operations are all very fast but the creation of set(a) and set(b) may not be.

a_keyset = set(a)
b_keyset = set(b)
for key in a_keyset & b_keyset:
    ...

If it's possible to non-hideously get rid of the walk over a from the instantiation of out, that would be good too.

if isinstance(a, MutableSet) and isinstance(b, MutableSet):
return operator.ior(a, b)
elif isinstance(a, MutableMapping) and isinstance(b, MutableMapping):
for key in set(a) & set(b):
Copy link
Collaborator

Choose a reason for hiding this comment

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

same comment as for add about re-instantiating sets that you use over and over.

out[key] = copy.deepcopy(a[key])
for key in set(b) - set(a):
out[key] = copy.deepcopy(b[key])
return out
Copy link
Collaborator

@lgray lgray Mar 30, 2021

Choose a reason for hiding this comment

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

It makes more sense in iadd, but do we want add to always take the type of the first operand? I think the usual behavior would be to take the first common non-abstract type shared by a and b (counting back from the passed types)?

I'm fine with this being the rule so long as we are clear about it.

What about user defined accumulables which are somehow not compatible with each other? We should detect that and throw an error, to keep people from getting confusing answers.

Copy link
Member Author

Choose a reason for hiding this comment

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

The eventual call to a + b would throw if they are not compatible

Copy link
Member Author

Choose a reason for hiding this comment

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

There is a loss of the type of the mapping b here but since I figure these are almost always just dictionaries no need to work too hard to resolve the common type of a and b. It would be a challenging problem. I had originally thought I would convert everything to plain dict in the output, but it might be nice for user to have some chance of receiving what they put in (e.g. defaultdict)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah for sure the common case is covered well by this. I'm only concerned with not-to-farfetched uncommon cases from people who like tinkering.

It's also fine to simply say as a rule that for different input types the lhs operand's type is taken. That gives enough control to the user and just requires docs/comments.

Copy link
Member Author

@nsmith- nsmith- Mar 31, 2021

Choose a reason for hiding this comment

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

Another option is to require type(a) == type(b) which in this context is how we would expect things to be used regardless. For some of our executors you can't really guarantee what ends up on lhs in a reproducible way since it depends on the order the jobs finish

Copy link
Member Author

Choose a reason for hiding this comment

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

I went for a bit of a softer type check, take a look at the tests to see how it plays out

Copy link
Collaborator

Choose a reason for hiding this comment

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

That's also a fine solution. I think what we have right now is not confusing.

@nsmith- nsmith- merged commit 24f7d57 into master Mar 31, 2021
@nsmith- nsmith- deleted the newaccumulator branch March 31, 2021 20:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Accumulator refactor
2 participants