Skip to content

Commit

Permalink
Merge pull request #1729 from twitter/oscar/opt-counters
Browse files Browse the repository at this point in the history
Use a null check rather than foreach
  • Loading branch information
ianoc authored Sep 27, 2017
2 parents 4b6c927 + 6de1884 commit 19c8881
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions scalding-core/src/main/scala/com/twitter/scalding/Stats.scala
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,14 @@ private[scalding] final case class GenericFlowPCounterImpl(fp: FlowProcess[_], s
}

private[scalding] final case class HadoopFlowPCounterImpl(fp: HadoopFlowProcess, statKey: StatKey) extends CounterImpl {
private[this] val counter: Option[Counter] = (for {
// we use a nullable type here for efficiency
private[this] val counter: Counter = (for {
r <- Option(fp.getReporter)
c <- Option(r.getCounter(statKey.group, statKey.counter))
} yield c)
} yield c).orNull

override def increment(amount: Long): Unit = counter.foreach(_.increment(amount))
override def increment(amount: Long): Unit =
if (counter != null) counter.increment(amount) else ()
}

object Stat {
Expand Down

0 comments on commit 19c8881

Please sign in to comment.