Skip to content

Implement copy-on-write for all backends #157

@mratsim

Description

@mratsim

While I love value semantics and still thinks it is best to avoid this type of questions:
image

I think:

  • easy syntax,
  • behaviour consistency between backends (Cpu, Cuda, OpenCL),
  • efficient memory allocation
  • and performance
    cannot be ignored.

Most Arraymancer performance issues come from memory allocation and are due to value semantics. The alternative, using unsafeView, unsafeSlice, unsafeSqueeze, etc is clunky as it litters the codebase with low-level details.

With "copy-on-write", it is possible to be:

  • safe as with value semantics
  • and fast like reference semantics

by only copying when necessary, i.e. when a value is mutable.

Benefits expected:

  • Copies involving non-mutable variables will be shallow by default
  • Slicing of non-mutable variables will be shallow by default
  • Everything involving mutable variables will be deep by default to prevent "unexpected surprises"
  • Keep the behaviour predictable:
    • let assignment from let ==> shallow
    • var assignment from let ==> deep
    • let assignment from var ==> deep
    • There will be no under the hood if refcount = 1 optimization for var assignment .
      That would make Arraymancer depends on the refcounting GC and it would be much better to use destructors and the future =move and =sink in Implement =move and =sink operators (destructors) #150 for that.
  • Copy-on-write will work well with unsafe proc chaining unsafe proc chaining -> first proc unsafe = all procs unsafe #151

Limitations:

  • Implicit result will still require unsafeView and unsafeUnsqueeze for shallow-copies. (Obviously slice mutation does not copy but overwrite in-place).
    This is perfectly reasonable in my opinion.

This superseeds #19

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions