Can we pull the if branch out of the inner loop? Right now it appears as if for some blocks, the flag consolidateShuffleFiles could be true, while for others, it couldn't. 

    val writers = if (consolidateShuffleFiles) {
      Array.tabulate ...
    } else {
      Array.tabulate ...
    }