Skip to content

Ref._to_buffer does not cascade the moving of elements #126

@freddieknets

Description

@freddieknets

When moving a Ref (that itself points to an array of Refs) to another buffer, the original elements are not copied to that buffer.

I don't know if that is intentional, but as it is now I do not have a way to move the original elements to the new buffer.

This is blocking the development of xboinc.

MWE:

import xobjects as xo
import xtrack as xt

class ElementRefClass(xo.UnionRef):
    _reftypes = [xt.Drift._XoStruct, xt.Multipole._XoStruct]

class ElementRefData(xo.Struct):
    elements = ElementRefClass[:]

class MyStruct(xo.Struct):
    refdata = xo.Ref(ElementRefData)

thisref = ElementRefData(elements=[xt.Drift(length=1.7)._xobject, xt.Multipole(knl=[0,1.2])._xobject])

newstruct = MyStruct()
newstruct.refdata = thisref

newstruct.refdata.elements[0]
newstruct.refdata.elements[1]

will throw an error. What happens is that the MyStruct is created on a different buffer, and when assigning the field newstruct.refdata = thisref the ElementRefData is moved to the new buffer, but the Drift and Multipole elements themselves are not moved (this, or they are moved but their offsets are not correctly updated - I don't know which is the underlying issue).

I know this can be avoided by creating the ElementRefData in the same buffer as the MyStruct, but this cannot be done in my use case, because in practice the creation of ElementRefData is rather wastefull and creates a buffer that is several times larger than needed (which is an issue for xboinc).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions