Skip to content

Commit

Permalink
lib: Remove extra alloc + memcpy from bind wrappers
Browse files Browse the repository at this point in the history
Both py_cr_mesh_bind_vertex_buf and py_cr_mesh_bind_faces allocated a
temporary buffer needlessly. These don't actually amount to much of
anything in the final runtime, the time delta before and after this
patch is dwarfed by the time we spend in Python trying to shuffle the
data into a suitable format.
  • Loading branch information
vkoskiv committed Oct 28, 2024
1 parent d6b6f7c commit ef502b5
Showing 1 changed file with 5 additions and 22 deletions.
27 changes: 5 additions & 22 deletions bindings/cray_wrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -359,27 +359,16 @@ static PyObject *py_cr_mesh_bind_vertex_buf(PyObject *self, PyObject *args) {
return NULL;
}

// TODO: add checks
struct cr_vector *vertices = calloc(vec_count, sizeof(*vertices));
struct cr_vector *normals = calloc(nor_count, sizeof(*normals));
struct cr_coord *texcoords = calloc(tex_count, sizeof(*texcoords));
memcpy(vertices, vec_view.buf, vec_count * sizeof(*vertices));
memcpy(normals, nor_view.buf, nor_count * sizeof(*normals));
memcpy(texcoords, tex_view.buf, tex_count * sizeof(*texcoords));

struct cr_scene *s = PyCapsule_GetPointer(s_ext, "cray.cr_scene");
cr_mesh_bind_vertex_buf(s, mesh, (struct cr_vertex_buf_param){
.vertices = vertices,
.vertices = vec_view.buf,
.vertex_count = vec_count,
.normals = normals,
.normals = nor_view.buf,
.normal_count = nor_count,
.tex_coords = texcoords,
.tex_coords = tex_view.buf,
.tex_coord_count = tex_count
});

free(vertices);
free(normals);
free(texcoords);
Py_RETURN_NONE;
}

Expand Down Expand Up @@ -412,15 +401,9 @@ static PyObject *py_cr_mesh_bind_faces(PyObject *self, PyObject *args) {
return NULL;
}

struct cr_face *faces = calloc(face_count, sizeof(*faces));
memcpy(faces, face_view.buf, face_count * sizeof(*faces));
// for (size_t i = 0; i < face_count; ++i) {
// dump_face(faces[i]);
// }

struct cr_scene *s = PyCapsule_GetPointer(s_ext, "cray.cr_scene");
cr_mesh_bind_faces(s, mesh, faces, face_count);
free(faces);
cr_mesh_bind_faces(s, mesh, face_view.buf, face_count);

Py_RETURN_NONE;
}

Expand Down

0 comments on commit ef502b5

Please sign in to comment.