Uploaded image for project: 'Swift'
  1. Swift
  2. SR-11762

Property wrappers can cause surprising value copying

    XMLWordPrintable

    Details

      Description

      I have a property wrapper on an array property. For example: 

      @propertyWrapper
      struct TestWrapper<ValueType> {
          var wrappedValue: ValueType
      }
      		
      struct State {
          @TestWrapper
          var values: [String] = []
      }
      
      var state = State()
      state.values = Array(repeating: "", count: 20000)
      		
      for i in 0..<20000 {
          state.values[i] = String(i)
      }
      

      Running this code is very slow (in both release and debug modes) as it causes something like 20,000 array copies to occur. If I remove the property wrapper, it runs very fast and has no array copying overhead. This is very unexpected behavior, IMO, and represents a rather nasty performance surprise when using property wrappers.

      I originally posted about this on the forums here: https://forums.swift.org/t/property-wrapper-causing-excessive-array-copying/30637

      Tested on Xcode 11.2.1 (11B53).

        Attachments

          Activity

            People

            Assignee:
            suyashsrijan Suyash Srijan
            Reporter:
            bigzaphod Sean Heber
            Votes:
            0 Vote for this issue
            Watchers:
            7 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: