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

[Bind2] Refactor XMR references #1255

Merged
merged 2 commits into from
Apr 13, 2023
Merged

Conversation

rsetaluri
Copy link
Collaborator

@rsetaluri rsetaluri commented Mar 23, 2023

Avoids requiring setting instances as attributes on circuits to refer to them (similarly with self. for Generator2's). Instead getting the attribute will retrieve the instance by name.

For example,

class Foo(m.Circuit):
    my_placeholder_var = Bar(name="my_instance_name")

assert Foo.my_placeholder_var is Foo.my_instance_name

@rsetaluri rsetaluri requested a review from leonardt March 23, 2023 19:21
@rsetaluri rsetaluri force-pushed the bind2-refactor-xmr-references branch from 2a6d9f0 to 65940f6 Compare March 23, 2023 19:24
@codecov-commenter
Copy link

codecov-commenter commented Mar 23, 2023

Codecov Report

Merging #1255 (7e054d4) into master (c865dd7) will increase coverage by 0.00%.
The diff coverage is 100.00%.

❗ Current head 7e054d4 differs from pull request most recent head 65940f6. Consider uploading reports for the commit 65940f6 to get more accurate results

📣 This organization is not using Codecov’s GitHub App Integration. We recommend you install it so Codecov can continue to function properly for your repositories. Learn more

@@           Coverage Diff           @@
##           master    #1255   +/-   ##
=======================================
  Coverage   85.67%   85.67%           
=======================================
  Files         161      161           
  Lines       17410    17416    +6     
=======================================
+ Hits        14916    14922    +6     
  Misses       2494     2494           
Impacted Files Coverage Δ
magma/circuit.py 92.15% <100.00%> (+0.07%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@rsetaluri
Copy link
Collaborator Author

@leonardt my main concern is the performance cost of doing this lookup every time getattr is called. We could add an acceleration data structure (map from names to instances) to speed this up. Although, currently we don't enforce uniqueness of instance names, and that would implicitly enforce that. Open to suggestions here.

magma/circuit.py Outdated
return only(filter(lambda i: i.name == attr, self.instances))
except IterableException:
pass
return object.__getattribute__(self, attr)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could we invert this, do __getattribute__ by default, and check instances list on AttributeError? That would optimize the common case (save a level of indirection in the function call stack)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Could we invert this, do __getattribute__ by default, and check instances list on AttributeError? That would optimize the common case (save a level of indirection in the function call stack)

I like this option more. In general though, there is a problem about aliasing/colliding names, e.g.

class Foo(m.Circuit):
    Bar(name="bar")
    bar = 100

print (Foo.bar)  # prints "100"...

We should have a clear spec on what we want to do in this case, that is ideally independent of implementation concerns. Not sure I have a strong opinion on what we should do in this case. Alternatively, we could add a separate instance getter API, like Foo.get_instance(..).

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think the above behavior is expected given normal Python class/object semantics so I'd advocate for preserving that and having a get API specifically for instances when there is this issue of collisions.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good point. See #1262

@leonardt
Copy link
Collaborator

Alternatively, we could try patching the instance name attributes at close time? Assuming the .open() pattern is used for modifications, this should be safe?

@rsetaluri rsetaluri force-pushed the bind2-refactor-xmr-references branch 2 times, most recently from b72d76b to 6b060f1 Compare April 7, 2023 20:17
Updates XMR tests to avoid setting instances as attributes on top-level
circuits.
@rsetaluri rsetaluri force-pushed the bind2-refactor-xmr-references branch from 6b060f1 to 9f7d7bc Compare April 7, 2023 20:21
@rsetaluri
Copy link
Collaborator Author

@leonardt ended up going with your suggestion of defaulting to object.__getattribute__() and only searching for instances in the error case.

@rsetaluri rsetaluri requested a review from leonardt April 7, 2023 21:09
@rsetaluri rsetaluri merged commit 704d729 into master Apr 13, 2023
@rsetaluri rsetaluri deleted the bind2-refactor-xmr-references branch April 13, 2023 17:41
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.

3 participants