Skip to content

fix: initialise/deinitialise of (boxed) array data#8

Merged
tmcdonell merged 5 commits into
mainfrom
feat/semantics
Dec 16, 2025
Merged

fix: initialise/deinitialise of (boxed) array data#8
tmcdonell merged 5 commits into
mainfrom
feat/semantics

Conversation

@tmcdonell

@tmcdonell tmcdonell commented Dec 12, 2025

Copy link
Copy Markdown
Contributor

Summary

This is required for any class-based types (e.g. String) that we are storing via Box. When first writing to the raw buffer we must initialise the memory (retaining as strong reference to the value that is not owned by us) and ensure that memory is deinitialised when the containing MultiArray is deallocated (releasing said strong reference). Previously we were not doing the latter, and when modifying elements (subscript write) re-initialising that slot. Both of these would cause memory leaks.

@tmcdonell tmcdonell requested a review from JaapWijnen December 12, 2025 12:17
@tmcdonell tmcdonell force-pushed the feat/semantics branch 3 times, most recently from 6df25b1 to 318551a Compare December 15, 2025 09:12
Comment thread Sources/MultiArray/ArrayData.swift
@tmcdonell tmcdonell force-pushed the feat/semantics branch 3 times, most recently from c063740 to a3dfc67 Compare December 15, 2025 13:58
public protocol ArrayData {
associatedtype Buffer

static func initialise(_ arrayData: Buffer, at: Int, to value: Self)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add a comment here that describes what these are expected to do

Comment thread Sources/MultiArray/ArrayData.swift
Storage is variable sized (depending on the element type) so having the
count and context pointer fields at the start of the object gives a
fixed a offset to (the start of) all fields.
This also drops the inout qualifier on the Buffer parameter, which is
not needed (the buffer contents may change, but not the pointer to the
buffer itself).
This is required for any class-based types (e.g. String) that we are
storing via Box. When first writing to the raw buffer we must initialise
the memory (retaining as strong reference to the value that is not owned
by us) and ensure that memory is deinitialised when the containing
MultiArray is deallocated (releasing said strong reference). Previously
we were not doing the latter, and when modifying elements (subscript
write) re-initialising that slot. Both of these would cause memory
leaks.
@tmcdonell tmcdonell merged commit 7b09ab2 into main Dec 16, 2025
3 checks passed
@tmcdonell tmcdonell deleted the feat/semantics branch December 16, 2025 08:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants