From 48b28351edd7ddc7a8b83424dd9c613e0e43f601 Mon Sep 17 00:00:00 2001 From: Jan Petykiewicz Date: Wed, 8 Jun 2022 20:55:25 -0700 Subject: [PATCH] speed up GDS writing by preallocating xy array and directly rounding to target dtype --- masque/file/gdsii.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/masque/file/gdsii.py b/masque/file/gdsii.py index 6bea759..df5cf99 100644 --- a/masque/file/gdsii.py +++ b/masque/file/gdsii.py @@ -462,16 +462,18 @@ def _shapes_to_elements( elements.append(path) elif isinstance(shape, Polygon): polygon = shape - xy_open = numpy.round(polygon.vertices + polygon.offset).astype(int) - xy_closed = numpy.vstack((xy_open, xy_open[0, :])) + xy_closed = numpy.empty((polygon.vertices.shape[0] + 1, 2), dtype=numpy.int32) + numpy.rint(polygon.vertices + polygon.offset, out=xy_closed[:-1], casting='unsafe') + xy_closed[-1] = xy_closed[0] boundary = klamath.elements.Boundary(layer=(layer, data_type), xy=xy_closed, properties=properties) elements.append(boundary) else: for polygon in shape.to_polygons(): - xy_open = numpy.round(polygon.vertices + polygon.offset).astype(int) - xy_closed = numpy.vstack((xy_open, xy_open[0, :])) + xy_closed = numpy.empty((polygon.vertices.shape[0] + 1, 2), dtype=numpy.int32) + numpy.rint(polygon.vertices + polygon.offset, out=xy_closed[:-1], casting='unsafe') + xy_closed[-1] = xy_closed[0] boundary = klamath.elements.Boundary(layer=(layer, data_type), xy=xy_closed, properties=properties)