Skip to content

Can we change MultiVector(layout, arr) to no longer copy arr? #282

@eric-wieser

Description

@eric-wieser

Almost all of our operations create an extra copy (see #280). The fix proposed there is to opt-in for copy-free construction (spelt layout.MultiVector(value, copy=False)). However, it seems that every single construction within clifford would want this optin, which would make it seem better as a default.


If we made not copying the default, there are two types of code that would start doing the wrong thing:

  • Code which modifies arrays to create multiple multivectors

    def mv_range(n):
        mvs = []
        buffer = np.zeros(32)
        for i in range(n):
            buffer[0] = i
            mvs.append(layout.MultiVector(buffer))
        return mvs  # if the above does not copy, these end up all the same!
  • Code which modifies multivectors sharing the same array

    def mv_range(n):
        mvs = []
        buffer = np.zeros(32)
        for i in range(n):
            mv = layout.MultiVector(buffer)
            mv[()] = i
            mvs.append(mv)
        return mvs  # if the above does not copy, these end up all the same!

Both of these are easy to fix by using buffer.copy() instead of buffer, but:

  • Users upgrading may not know they need to do this
  • It's still possible to write this type of code by accident.

Does the benefit of not having copy=False sprinked throughout tools justify the cost of the potential breakage here?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions