Repository: pyvista/pyvista

Issue #3971: vtkCell wrappings slow down
Created: 2023-02-06T21:57:48Z
@bjlittle is experiencing some slow downs from the new wrapping of vtkCell in 0.38+

performance bottleneck that I was hitting when using pyvista 0.38+. Basically it's a result of the new wrapping of vtkCell and calls to mesh.cell[cid] instead of mesh.get_cell(cid)... Which makes a massive difference, particularly for large/huge meshes.

We should look into this

Duplicate issue #4013: Iterating over `dataset.cells` much more expensive than `cells = dataset.cells`
Created: 2023-02-13T22:58:23Z
Describe the bug, what's wrong, and what you expected.
I'm not sure exactly what causes this, but I have a script that's going through each cell in the data set and noticed it was taking a long time (~10 minutes) when iterating through elements like this
connectivity_cursor = 0
for elemNum, etypeNum in enumerate(dataset.celltypes):
    n_nodes = dataset.cells[connectivity_cursor]
    connectivity_cursor += 1
    elemList = dataset.cells[connectivity_cursor:connectivity_cursor + n_nodes]
    connectivity_cursor += n_nodes

However by simply changing to this the time comes down to (~2 seconds)
cells = dataset.cells
connectivity_cursor = 0
for elemNum, etypeNum in enumerate(dataset.celltypes):
    n_nodes = cells[connectivity_cursor]
    connectivity_cursor += 1
    elemList = cells[connectivity_cursor:connectivity_cursor + n_nodes]

Steps to reproduce the bug.
import pyvista as pv
mfile = 'aorta_mesh_with_wall.vtk'
dataset = pv.read(mfile)

cells = dataset.cells
connectivity_cursor = 0
for elemNum, etypeNum in enumerate(dataset.celltypes):
    n_nodes = cells[connectivity_cursor]
    connectivity_cursor += 1
    elemList = cells[connectivity_cursor:connectivity_cursor + n_nodes]

# Compare with
connectivity_cursor = 0
for elemNum, etypeNum in enumerate(dataset.celltypes):
    n_nodes = dataset.cells[connectivity_cursor]
    connectivity_cursor += 1
    elemList = dataset.cells[connectivity_cursor:connectivity_cursor + n_nodes]
    connectivity_cursor += n_nodes
aorta_mesh_with_wall.zip
System Information
--------------------------------------------------------------------------------
  Date: Mon Feb 13 23:57:13 2023 CET
                OS : Linux
            CPU(s) : 8
           Machine : x86_64
      Architecture : 64bit
               RAM : 15.2 GiB
       Environment : IPython
       File system : btrfs
        GPU Vendor : Intel
      GPU Renderer : Mesa Intel(R) Iris(R) Plus Graphics (ICL GT2)
       GPU Version : 4.6 (Core Profile) Mesa 22.3.4 (git-a5ffb70f86)
  Python 3.10.8 | packaged by conda-forge | (main, Nov 22 2022, 08:26:04) [GCC
  10.4.0]
           pyvista : 0.37.0
               vtk : 9.1.0
             numpy : 1.24.1
           imageio : 2.25.0
            scooby : 0.7.1
             pooch : v1.6.0
        matplotlib : 3.6.3
             PyQt5 : 5.15.7
           IPython : 8.10.0
             scipy : 1.10.0
--------------------------------------------------------------------------------
Screenshots
No response

Closing comment (2023-02-14T14:49:29Z): I don't think that this is a duplicate of #3971 as you are using pyvista 0.37.0.  You will find that your code will no longer work in 0.38.0+ without changes as cells will be different.  The problem is very similar however.  In version 0.38.0+, I'd recommend using the get_cell method instead.  This may now require a list comprehension, or similar, in this line cells[connectivity_cursor:connectivity_cursor + n_nodes].