title: Auto Layout performance with nested relative widths?
body: <p>To summarize my problem: when I have many child views with widths equal to the width of their parent, where the parent's width is equal to their parent's width, there is a huge performance hit when the window is horizontally resized.</p>
  
  <hr>
  
  <p>I am retrieving a large number of comments from an API and decided to create an individual <code>NSView</code> for each comment and then vertically space them all within an <code>NSScrollView</code>. Each individual comment grows to the height of the comment's text and matches the width of the container. This works fine when there aren't many loaded comments, but when the comments routinely exceed 100, there is a huge performance hit when the window is horizontally resized.</p>
  
  <p>I create each comment's view:</p>
  
  <pre><code>var lastComment:NSView = documentView // The last view to vertically position around
  for (index, comment) in commentsArray.enumerate() {
      var data = comment
      data["attributedString"] = attributedString
      let commentView = NSCommentView(data: data)
      documentView.addSubview(commentView)
      let verticalSpacing = (index == 0) ? 0 : 10 // Position the first comment without any top spacing 
      let secondAttribute = (index == 0) ? NSLayoutAttribute.Top : NSLayoutAttribute.Bottom
      let widthConstraint = NSLayoutConstraint(item: commentView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: documentView, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0)
      let topConstraint = NSLayoutConstraint(item: commentView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: lastComment, attribute: secondAttribute, multiplier: 1, constant: verticalSpacing)
      documentView.addConstraints([widthConstraint, topConstraint])
      lastComment = commentView
      if index == commentsArray.count - 1{
          let bottom = NSLayoutConstraint(item: commentView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: documentView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0)
          documentView.addConstraint(bottom)
      }
  }
  </code></pre>
  
  <p>Where <code>NSCommentView</code> is defined as:</p>
  
  <pre><code>class NSCommentView: NSView {
  
      func setupFrame(){
          self.wantsLayer = true
          self.translatesAutoresizingMaskIntoConstraints = false
          self.layer?.backgroundColor = NSColor.redColor().CGColor
          // Set height of comment, fixed for testing
          let heightConstraint = NSLayoutConstraint(item: self, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 50)
          self.addConstraint(heightConstraint)
      }
  
      func setupTextView(attributedString: NSAttributedString){
          let textView = NSTextView()
          textView.translatesAutoresizingMaskIntoConstraints = false
          textView.verticallyResizable = true
          textView.textStorage?.setAttributedString(attributedString)
          self.addSubview(textView)
          let heightConstraint = NSLayoutConstraint(item: textView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 50)
          textView.addConstraint(heightConstraint)
  
          // The performance hit occurs when I set a width equal to the parent width:
          let widthConstraint = NSLayoutConstraint(item: textView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0)
          self.addConstraint(widthConstraint)
      }
  
      convenience init(data:[String: AnyObject?]){
          self.init()
          setupFrame()
          if data["isComment"] as! Bool {
              let attributedString = data["attributedString"] as! NSAttributedString
              setupTextView(attributedString)
          }
      }
  }
  </code></pre>
  
  <p>As you can see from the code, <strong>the issue</strong> is when I attempt to set the width of the <code>NSTextView</code> to the width of the <code>NSCommentView</code>. If I simply set it as a constant, there is no real performance degradation.</p>
  
  <p>Is my view hierarchy not setup correctly, or am I overlooking something which is causing this performance issue?</p>