Skip to content

BUG: Sum involving tempStore has a COW type of bug #12

Description

@kechan

I ran into a problem while working with sum(...) in a separate module that depend on this one. I found that the base address returned by tempStore is different than what I expect.

extension FloatVector {
  @usableFromInline var storeKey:String { get { return "FloatVector\(Element.self)" } }
  @usableFromInline var tempStore:Self { get {
    if let r = Thread.current.threadDictionary[storeKey] as? Self {
      if (r.count == count) { return r }
    }
    return Thread.setToTLS(Self(count), storeKey)
  }}

I think the line:

if let r = Thread.current.threadDictionary[storeKey] as? Self {

may be at fault. Since self is Array which is a value type, the "if let" may actually create a copy. My prelim debug seemed to confirm that r.p has a different address than whats created previously. And if your downstream logic work by pointer, you may be on an entirely different Array than the cache you expect.

NB: The tempStore will work 1st time you invoke it, but if the dictionary is not clear, the next time when it gets reused, this issue will rear its head.

Apparent simplest fix:

extension FloatVector {
  @usableFromInline var storeKey:String { get { return "FloatVector\(Element.self)" } }
  @usableFromInline var tempStore:Self { get {
    if let r = Thread.current.threadDictionary[storeKey] as? Self {
        if (r.count == count) {
//            return r
            return Thread.current.threadDictionary[storeKey] as! Self    // avoid if let, and COW
        }
    }
    return Thread.setToTLS(Self(count), storeKey)
  }}

Activity

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

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions