Skip to content

Conversation

@reyoung
Copy link
Collaborator

@reyoung reyoung commented Jun 26, 2017

Move paddle/utils/Error.h to paddle/framework/error.h.

Make new framework using paddle::Error as error type.

Make paddle framework use Error as Error Type.
@reyoung reyoung requested review from gangliao and wangkuiyi June 26, 2017 06:41
Copy link
Contributor

@typhoonzero typhoonzero left a comment

Choose a reason for hiding this comment

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

Is this PR fix #2430?


private:
private:
std::shared_ptr<std::string> msg_;
Copy link
Contributor

Choose a reason for hiding this comment

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

Why a shared_ptr, can ::std::string object do the job?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

No. Because if we using std:: string, Error will copy by value each time, which is very time-consuming.

Copy link
Contributor

@typhoonzero typhoonzero Jun 26, 2017

Choose a reason for hiding this comment

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

Well, Error object creation will do one memory operation. But after that, we'll pass error object using reference all the time, we can also use Error as return types of functions as we have return value optimization

This is the sample code from this PR:

Error __must_check foobar() {
  Error err;
  // do something.
  foo(&err);
  if (err) return err;
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We should not mainly use foo(Error* error) to return Error. __must_check will let developer force to check the return error, but foo(Error* error) does not have the same technic. So it is better to use Error __must_check foo();.

In another hand, RVO depends on compiler implementation.

Copy link
Contributor

@typhoonzero typhoonzero Jun 27, 2017

Choose a reason for hiding this comment

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

I'm thinking of two type of use case, which is already in this PR:

// single error return value
Error err = foo();
// multiple return values with Error
Error err;
ReturnValue ret;
foo(&err, &ret);
// more general uses, mostly use error as return value.
ReturnValue ret;
Error err = foo(&ret, "params");

@reyoung reyoung mentioned this pull request Jun 27, 2017
* removed later.
*/
void check() const { CHECK(this->isOK()) << msg(); }
bool operator==(const Error &o) const {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@typhoonzero By using msg_ a shared_ptr, the operator== will be fast for predefined Error.

Copy link
Contributor

Choose a reason for hiding this comment

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

I see.

@reyoung reyoung requested a review from helinwang June 27, 2017 09:01
* @brief isOK return True if there is no error.
* @return True if no error.
*/
bool isOK() const { return msg_ == nullptr; }
Copy link
Contributor

@helinwang helinwang Jun 27, 2017

Choose a reason for hiding this comment

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

I am not very familiar with C++ naming convention so I could be wrong: to me it seems ok() is better than isOK() (btw, maybe should be isOk rather than isOK according to Google C++ style?), "is" makes developers type two more characters, and does not provide much value in my opinion.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

My fault, It will be changed soon.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done.

@helinwang
Copy link
Contributor

Do we need error code to specify what kind of error it is? Two errors with different error message could be the same kind of error (e.g., Error("open file failed: /tmp/foo"), Error("open file failed: /tmp/bar")).

@reyoung
Copy link
Collaborator Author

reyoung commented Jun 28, 2017

Do we need error code to specify what kind of error it is? Two errors with different error message could be the same kind of error (e.g., Error("open file failed: /tmp/foo"), Error("open file failed: /tmp/bar")).

In this case, maybe a global function bool IsNotFound(const Error& err) is need.

bool IsNotFound(const Error& err) {
   auto pattern = r"file not found: .*";
   return pattern.math(err.msg());
}

IsNotFound is slower than Error Code but it may not be a performance critical path for Paddle.

And I think the most error of Paddle should not be a dynamic string. The most error should be just a predefined const string.

const Error FileNotFound = Error("file is not found.");

Then IsNotFound should just invoke error's operator ==, and just a pointer match operation.

following google name style.
@reyoung
Copy link
Collaborator Author

reyoung commented Jun 28, 2017

Closed because #2646

@reyoung reyoung closed this Jun 28, 2017
@reyoung reyoung deleted the feature/moving_error_to_framework branch October 28, 2017 22:19
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