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

marking function as mutating delivers a speedup

    XMLWordPrintable

    Details

    • Type: Improvement
    • Status: Open
    • Priority: Medium
    • Resolution: Unresolved
    • Component/s: Compiler
    • Labels:

      Description

      In the code below, the mutating function call is faster by almost 2x than the non-mutating version. To see this, just call both functions about 25 million times.  If you'd like an Xcode project which packages all this up as library and executable which runs the timing, please do " git clone git@github.com:davidbaraff/ptrTest2.git".

      public struct RT {
          public init() {
              data = CxxData(x: 0, y: 0,
                             m: (0, 0, 0, 0,
                                 0, 0, 0, 0,
                                 0, 0, 0, 0,
                                 0, 0, 0, 0))
          }
      
          public func call_that_copies() -> Double {
              var data_copy = data
              return analyze(&data_copy)
          }
      
          // exactly the same as above, just that "mutating" was added:
          public mutating func mutating_call_that_copies() -> Double {
              var data_copy = data
              return analyze(&data_copy)
          }
          var data: CxxData
      }
      

       
      The structure CxxData is a struct defined in C (i.e. in a bridging header file), as follows:

      typedef struct CxxData {
          double x, y;
          double m[16];
       } CxxData;

      while the function "analyze()" is written in C, and returns a constant value:

       double analyze(CxxData const* data) {
           return 37.0;
       }

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              davidbaraff David Baraff
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated: