Skip to content

random number generators perform unnecessary copy #1351

Open

Description

@braingram

Taking generate as an example:

GalSim/galsim/random.py

Lines 265 to 271 in ee177aa

array_1d = np.ascontiguousarray(array.ravel(),dtype=float)
#assert(array_1d.strides[0] == array_1d.itemsize)
_a = array_1d.__array_interface__['data'][0]
self._rng.generate(len(array_1d), _a)
if array_1d.data != array.data:
# array_1d is not a view into the original array. Need to copy back.
np.copyto(array, array_1d.reshape(array.shape), casting='unsafe')

The result of ascontinguousarray can share memory with the input array yet the if array_1d.data != array.data: check can fail resulting in an unnecessary copy.

The issue can be illustrated in the following code:

import numpy as np
array = np.empty((10, 20, 30), dtype=float)
array_1d = np.ascontiguousarray(array.ravel(),dtype=float)

# data is not the same
assert array.data != array_1d.data

# because array_1d is a view of array
assert array.data == array_1d.base.data  # note the use of base here

# arrays share memory
assert np.shares_memory(array, array_1d)

# double check
array_1d[:] = 2
assert np.all(array == 2)

Replacing if array_1d.data != array.data: with if not np.shares_memory(array, array_1d): should fix the issue.

Activity

  1. rmjarvis commented on Mar 6, 2026

    @rmjarvis
    Member

    Sure. I'm not sure it's an "issue" exactly. But I'll admit the change would be marginally more efficient. Feel free to submit a PR.

  2. added
    optimization/performanceRelated to the speed and/or memory consumption of some aspect of the code
    on May 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    optimization/performanceRelated to the speed and/or memory consumption of some aspect of the code

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions