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

Simplify variable #2647

Closed

Conversation

reyoung
Copy link
Collaborator

@reyoung reyoung commented Jun 28, 2017

It seems that Placeholder::Ptr and Placeholder::Type does not need. We can use dynamic_cast to achieve the same goal and make implementation simpler.

@reyoung reyoung requested a review from wangkuiyi June 28, 2017 09:37
It seems using dynamic_cast is simpler.
Copy link
Collaborator

@wangkuiyi wangkuiyi left a comment

Choose a reason for hiding this comment

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

This PR obviously complicated Variable; instead of simplifying it.

dynamic_cast is not necessary here, because we don't implement polymorphism using virtual functions and class derivation. Instead, we are using template. This allows us to know type T at compile time and to use static_cast.

https://stackoverflow.com/questions/579887/how-expensive-is-rtti shows how much more expensive dynamic_cast is, compared with static_cast. That is why I designed Variable's Placeholder to use template and static_cast.

@@ -25,42 +25,37 @@ class Variable {
public:
template <typename T>
const T& Get() const {
PADDLE_ASSERT(IsType<T>());
return *static_cast<const T*>(holder_->Ptr());
auto holder = dynamic_cast<PlaceholderImpl<T>*>(holder_.get());
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why use dynamic_cast?

@wangkuiyi wangkuiyi closed this Jun 29, 2017
@wangkuiyi
Copy link
Collaborator

Please don't be frustrated because I closed this PR. It is always a great trial to shave something with the Occam's Razor! @reyoung

@reyoung reyoung deleted the feature/simplify_variable branch October 28, 2017 22:19
heavengate pushed a commit to heavengate/Paddle that referenced this pull request Aug 16, 2021
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.

2 participants