Skip to content

Commit 316dcf9

Browse files
authored
Merge pull request #407 from codacy/fix-parallel
fix: Make `--parallel` run effectively in parallel CY-4739 :breaking:
2 parents e73d41b + ce620cc commit 316dcf9

3 files changed

Lines changed: 29 additions & 3 deletions

File tree

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,9 @@ sbt codacyCoverage
334334

335335
## Breaking Changes
336336

337-
- 4.0.0 - Rename `analyse` command to `analyze`. This is a breaking change if you are running the CLI by using the
337+
- `7.0.0`: Fix `--parallel` that wasn't making the tools run actually in parallel. To restore the previous behaviour use `--parallel 1`
338+
339+
- `4.0.0`: Rename `analyse` command to `analyze`. This is a breaking change if you are running the CLI by using the
338340
[jar](#java) or [`sbt`](#local), but not if you are using the [provided script](#script).
339341

340342
## What is Codacy

core/src/main/scala/com/codacy/analysis/core/utils/SetOps.scala

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@ object SetOps {
99

1010
def mapInParallel[A, B](set: Set[A], nrParallel: Option[Int] = Option.empty[Int])(f: A => B): Seq[B] = {
1111
val setPar: ParSet[A] = set.par
12-
setPar.tasksupport = new ForkJoinTaskSupport(new ForkJoinPool(nrParallel.getOrElse(2)))
13-
setPar.map(f)(collection.breakOut)
12+
val forkJoinPool = new ForkJoinPool(nrParallel.getOrElse(2))
13+
setPar.tasksupport = new ForkJoinTaskSupport(forkJoinPool)
14+
val result = setPar.map(f)
15+
forkJoinPool.shutdown()
16+
result.seq.toSeq
1417
}
1518

1619
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.codacy.analysis.core.utils
2+
3+
import org.specs2.mutable.Specification
4+
5+
class SetOpsSpec extends Specification {
6+
7+
"SetOps.mapInParallel" should {
8+
"actually run in parallel" in {
9+
val parallelism = 10
10+
11+
val threadsIds = SetOps.mapInParallel(1.to(parallelism).toSet, Some(parallelism)) { _ =>
12+
// If the function terminates too fast the thread pool reuses the same threads
13+
Thread.sleep(100)
14+
15+
Thread.currentThread().getId()
16+
}
17+
18+
threadsIds.distinct should haveSize(parallelism)
19+
}
20+
}
21+
}

0 commit comments

Comments
 (0)