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

Reduce the use of C++ exceptions #11282

Merged
merged 3 commits into from May 22, 2018

Conversation

mdboom
Copy link
Member

@mdboom mdboom commented May 21, 2018

PR Summary

This is hopefully the last of the pyodide-related patches for a while.

Emscripten has only limited support for C++ exceptions. The support that is there doesn't seem to work fully correctly over the matplotlib code base, and even if it did work, it has a large performance penalty which will be hard to resolve. The best available option at the moment is to have C++ exception support turned off, which means a throw from C++ aborts the current WebAssembly frame, exiting back to Javascript without a chance for any C++ catch blocks to handle the exception. (This is weaker than an unhandled exception in "native" code which would terminate the application altogether). In most cases, this behavior is "good enough" when the exceptions are truly "exceptional". It's not ideal, since there isn't an opportunity to cleanup or provide friendly error messages from C++.

However there is a very small handful of places in matplotlib where exception-handling is used for cases that are the normal course of business, such as dealing with an input differently based on the number of its dimensions. This grew out of the fact that a constructor can not return an error code, so must raise an exception to indicate a dimension mismatch. I think it's worth fixing these three places to just use the C-level Numpy API instead of the numpy_cpp.h abstraction, even though that's not strictly necessary for most platforms. There should be performance benefits for everyone, however, since it avoids interpreting the array-like input twice.

PR Checklist

  • Has Pytest style unit tests
  • Code is PEP 8 compliant
  • New features are documented, with examples if plot related
  • Documentation is sphinx and numpydoc compliant
  • Added an entry to doc/users/next_whats_new/ if major new feature (follow instructions in README.rst there)
  • Documented in doc/api/api_changes.rst if API changed in a backward-incompatible way

@mdboom mdboom requested a review from jkseppan May 21, 2018 19:32
Copy link
Member

@jkseppan jkseppan left a comment

Choose a reason for hiding this comment

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

Looks correct to me. I didn't check if we have full branch coverage in the tests of these functions, but that would seem like a good idea.

npy_intp dims[] = { (npy_intp)vertices.size(), 2 };
numpy::array_view<double, 2> result(dims);
CALL_CPP("affine_transform", (affine_transform_2d(vertices, trans, result)));
Py_DECREF(vertices_arr);
return result.pyobj();
} else { // PyArray_NDIM(vertices_arr) == 1
Copy link
Member

Choose a reason for hiding this comment

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

Do both of these branches get executed in tests?

}
catch (py::exception &)
{
} else { // PyArray_NDIM(rect_arr) == 1
Copy link
Member

Choose a reason for hiding this comment

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

Same question about test coverage.

@tacaswell tacaswell added this to the v3.0 milestone May 22, 2018
@mdboom
Copy link
Member Author

mdboom commented May 22, 2018

That's a good question about coverage. I just confirm that all of those branches are exercised by the existing tests (just crudely by adding printf statements). Longer term, we should probably do coverage testing on the C++ which I don't think we currently do.

@anntzer anntzer merged commit 5619354 into matplotlib:master May 22, 2018
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.

None yet

4 participants