skip to Main Content

I have a data structure in my Xcode project similar to this:

struct Group: {
  let name: String
  var items: [Thing]
}

struct Thing: {
  let description: String
  let variants: [Thing]?
}

var data: [Group] = 
    [Group(name: "Group 1",
          items: [Thing(description: "Thing 1", variants: nil),
                  Thing(description: "Thing 2", variants: nil),
                  ... ]),
     Group(name: "Group 2",
          items: [Thing(description: "AnotherThing 1", variants: [Thing(description: "Variant Thing1)",
                                                                  Thing(description: "Variant Thing2)",
                                                                  ... ]),
                  Thing(description: "AnotherThing 2", variants: [Thing(description: "Variant Thing 101"),
                                                                  Thing(description: "Variant Thing 102"),
                                                                  ... ]),
      ... ])]

In total there are about 10 Group’s containing about 3200 Thing’s; the Thing’s are spread about equally in "items" arrays and "variants" arrays.

Xcode takes from 3 to 5 minutes to compile this one file. What can I do to reduce this to a reasonable time?

Aside: In addition to being slow to compile, this data structure is making the Xcode profiler go bonkers. Fairly often, the profiler will eat up all of the swap space and crash the machine.

2

Answers


  1. Chosen as BEST ANSWER

    I didn't do it exactly like matt suggested but his advice put me onto a way to do it and not be too ugly.

    struct Thing: {
      let description: String
      var variants: [Thing]?
    }
                
    struct Group: {
      let name: String
      var items: [Thing]
    }
            
    let group1Things: [Thing] = [
      Thing(description: "Thing 1", variants: nil),
      Thing(description: "Thing 2", variants: nil),
      ... ]
            
    let group1: Group = Group(name: "Group 1", items: group1Things)
            
    let group2Things: [Thing] = [
      Thing(description: "AnotherThing 1", variants: [Thing(description: "Variant Thing1)",
                                                      Thing(description: "Variant Thing2)",
                                                      ... ]),
      Thing(description: "AnotherThing 2", variants: [Thing(description: "Variant Thing 101"),
                                                      Thing(description: "Variant Thing 102"),
                                                      ... ]
    
    let group2: Group = Group(name: "Group 2", items: group2Things)
            
    let data: [Group] = [group1, group2, ...]
    

    This got the compile time down from 3-5 minutes to 30 seconds. I can live with that. I could probably get it down further by breaking out the "variants" arrays.


  2. Try changing the assignment to

    var data: [Group] = [Group(name: "Group 1", ....
    

    In my experience, these slowdowns are mostly due to type inference, and specifying the type explicitly helps a lot.

    If that doesn’t work, follow the advice given by Joakim Danielson.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search