perf(editor): fewer kernel round trips per settle, background tessellation, vectorised edge keys
A settle lists the cells in the call that publishes the preview (one call into the kernel, not three), the boot's duplicate settle is dropped, the debounce is 0.12 s, parts are tessellated on a background thread while the next cell computes (cached_mesh releases the GIL; a failure is recorded on the shape under the GIL), and the edge position keys are computed with numpy (byte-identical). Six-axis arm, last cell to preview event: ~0.83 s -> ~0.36 s. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -222,8 +222,21 @@ void bind_mesh(py::module_& m) {
|
||||
internal.def(
|
||||
"cached_mesh",
|
||||
[](const Shape& solid, double linear_tolerance, double angular_tolerance) {
|
||||
// Meshing works on a topology copy (see meshed_copy) and touches no Python object, so it runs without the GIL:
|
||||
// the editor tessellates parts on a background thread while the next cell computes. A failure is recorded on
|
||||
// the shape here, with the GIL back.
|
||||
std::shared_ptr<const io::TriMesh> mesh;
|
||||
std::string error;
|
||||
{
|
||||
py::gil_scoped_release release;
|
||||
mesh = io::cached_mesh_detached(solid, linear_tolerance, angular_tolerance, &error);
|
||||
}
|
||||
if (!mesh && !error.empty()) {
|
||||
Shape handle = solid;
|
||||
handle.set_runtime("mesh.error", Value(error));
|
||||
}
|
||||
// The mesh is shared with the cache and exposed read-only, so handing it out un-const is safe.
|
||||
return std::const_pointer_cast<io::TriMesh>(io::cached_mesh(solid, linear_tolerance, angular_tolerance));
|
||||
return std::const_pointer_cast<io::TriMesh>(mesh);
|
||||
},
|
||||
py::arg("solid"), py::kw_only(), py::arg("linear_tolerance") = io::kDefaultLinearDeflection,
|
||||
py::arg("angular_tolerance") = io::kDefaultAngularDeflection);
|
||||
|
||||
@@ -143,6 +143,10 @@ TriMesh build_solid_trimesh(const Shape& solid, double linear_tolerance = kDefau
|
||||
std::shared_ptr<const TriMesh> cached_mesh(const Shape& solid,
|
||||
double linear_tolerance = kDefaultLinearDeflection,
|
||||
double angular_tolerance = kDefaultAngularDeflection);
|
||||
// The same without touching the shape: a failure's diagnostic is stored in `*error` instead of on the shape, so
|
||||
// the call is safe on a thread that holds no lock on the shape (the editor meshes parts in the background).
|
||||
std::shared_ptr<const TriMesh> cached_mesh_detached(const Shape& solid, double linear_tolerance,
|
||||
double angular_tolerance, std::string* error);
|
||||
// The diagnostic recorded by the last failed cached_mesh on this shape, or "".
|
||||
std::string mesh_error(const Shape& solid);
|
||||
// Drop every cached mesh (tests, memory pressure).
|
||||
|
||||
@@ -77,19 +77,29 @@ Cache& cache() {
|
||||
|
||||
} // namespace
|
||||
|
||||
std::shared_ptr<const TriMesh> cached_mesh(const Shape& solid, double linear_tolerance, double angular_tolerance) {
|
||||
std::shared_ptr<const TriMesh> cached_mesh_detached(const Shape& solid, double linear_tolerance,
|
||||
double angular_tolerance, std::string* error) {
|
||||
if (auto hit = cache().find(solid.wrapped(), linear_tolerance, angular_tolerance)) return hit;
|
||||
try {
|
||||
auto built = std::make_shared<const TriMesh>(build_solid_trimesh(solid, linear_tolerance, angular_tolerance));
|
||||
cache().insert(solid.wrapped(), linear_tolerance, angular_tolerance, built);
|
||||
return built;
|
||||
} catch (const std::exception& error) {
|
||||
Shape handle = solid; // handles share their entity: this records on the caller's shape
|
||||
handle.set_runtime(kErrorKey, std::string(error.what()));
|
||||
} catch (const std::exception& failure) {
|
||||
if (error != nullptr) *error = failure.what();
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
std::shared_ptr<const TriMesh> cached_mesh(const Shape& solid, double linear_tolerance, double angular_tolerance) {
|
||||
std::string error;
|
||||
auto mesh = cached_mesh_detached(solid, linear_tolerance, angular_tolerance, &error);
|
||||
if (!mesh && !error.empty()) {
|
||||
Shape handle = solid; // handles share their entity: this records on the caller's shape
|
||||
handle.set_runtime(kErrorKey, error);
|
||||
}
|
||||
return mesh;
|
||||
}
|
||||
|
||||
std::string mesh_error(const Shape& solid) {
|
||||
const Value value = solid.get_runtime(kErrorKey);
|
||||
return value.is_string() ? value.as_string() : std::string();
|
||||
|
||||
Reference in New Issue
Block a user