Skip to content

Commit

Permalink
Improve rendering time by rewriting `STD140BufferFormat._write_padded…
Browse files Browse the repository at this point in the history
…()` (#4058)

The original implementation of `STD140BufferFormat._write_padded()` used `np.pad` which is slow.
This new implementation avoids that by creating a new array of zeroes instead. In this way, this method now takes 60x less time, and the total rendering time is almost halved.
  • Loading branch information
chopan050 authored Dec 13, 2024
1 parent 9360129 commit 9f72377
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions manim/renderer/buffers/buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def __init__(
self.name = name
self.binding = binding
self.dtype = []
self._paddings = {} # LUT for future writes
self._paddings: dict[str, int] = {} # LUT for future writes
byte_offset = 0 # Track the offset so we can calculate padding for alignment -- NOTE: use RenderDoc to debug
for data_type, var_name in struct:
_base_char, base_bytesize, shape = self._GL_DTYPES[data_type]
Expand Down Expand Up @@ -97,11 +97,16 @@ def _write_padded(self, data: tuple | np.ndarray, var: str) -> np.ndarray:
np.ndarray
the same data with 0 or 1 columns of 0s appended
"""
try:
# This fails for 1D data (python or np.array)
return np.pad(data, ((0, 0), (0, self._paddings[var])), mode="constant")
except Exception:
return np.pad(data, ((0, self._paddings[var])), mode="constant")
data = np.asarray(data)
if self._paddings[var] == 0 or data.ndim == 0:
return data

# Make a new array with extra columns of 0s
new_shape = list(data.shape)
new_shape[-1] += self._paddings[var]
padded_data = np.zeros(new_shape)
padded_data[..., : data.shape[-1]] = data
return padded_data

def write(self, data: dict) -> None:
"""Write a dictionary of key value pairs to the STD140BufferFormat's data attribute
Expand Down

0 comments on commit 9f72377

Please sign in to comment.