Skip to content

Commit b95ce5e

Browse files
dmitry-workerkapke
authored andcommitted
Introduced TrieMap to simplify the code and to reduce cpu/memory usage (#775)
1 parent 873cdac commit b95ce5e

File tree

1 file changed

+11
-18
lines changed

1 file changed

+11
-18
lines changed

src/main/scala/io/iohk/ethereum/jsonrpc/EthService.scala

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import io.iohk.ethereum.jsonrpc.{FilterManager => FM}
3434
import monix.eval.Task
3535
import org.bouncycastle.util.encoders.Hex
3636

37+
import scala.collection.concurrent.{ Map => ConcurrentMap, TrieMap }
3738
import scala.concurrent.duration.FiniteDuration
3839
import scala.language.existentials
3940
import scala.reflect.ClassTag
@@ -226,8 +227,7 @@ class EthService(
226227

227228
import EthService._
228229

229-
val hashRate: AtomicReference[Map[ByteString, (BigInt, Date)]] =
230-
new AtomicReference[Map[ByteString, (BigInt, Date)]](Map())
230+
val hashRate: ConcurrentMap[ByteString, (BigInt, Date)] = new TrieMap[ByteString, (BigInt, Date)]()
231231
val lastActive = new AtomicReference[Option[Date]](None)
232232

233233
private[this] def consensus = ledger.consensus
@@ -481,11 +481,9 @@ class EthService(
481481
def submitHashRate(req: SubmitHashRateRequest): ServiceResponse[SubmitHashRateResponse] =
482482
ifEthash(req) { req =>
483483
reportActive()
484-
hashRate.updateAndGet((t: Map[ByteString, (BigInt, Date)]) => {
485-
val now = new Date
486-
removeObsoleteHashrates(now, t + (req.id -> (req.hashRate, now)))
487-
})
488-
484+
val now = new Date
485+
removeObsoleteHashrates(now)
486+
hashRate.put(req.id, (req.hashRate -> now))
489487
SubmitHashRateResponse(true)
490488
}
491489

@@ -527,21 +525,16 @@ class EthService(
527525

528526
def getHashRate(req: GetHashRateRequest): ServiceResponse[GetHashRateResponse] =
529527
ifEthash(req) { _ =>
530-
val hashRates: Map[ByteString, (BigInt, Date)] = hashRate.updateAndGet((t: Map[ByteString, (BigInt, Date)]) => {
531-
removeObsoleteHashrates(new Date, t)
532-
})
533-
528+
removeObsoleteHashrates(new Date)
534529
//sum all reported hashRates
535-
GetHashRateResponse(hashRates.mapValues { case (hr, _) => hr }.values.sum)
530+
GetHashRateResponse(hashRate.map { case (_, (hr, _)) => hr }.sum)
536531
}
537532

538533
// NOTE This is called from places that guarantee we are running Ethash consensus.
539-
private def removeObsoleteHashrates(
540-
now: Date,
541-
rates: Map[ByteString, (BigInt, Date)]
542-
): Map[ByteString, (BigInt, Date)] = {
543-
rates.filter { case (_, (_, reported)) =>
544-
Duration.between(reported.toInstant, now.toInstant).toMillis < jsonRpcConfig.minerActiveTimeout.toMillis
534+
private def removeObsoleteHashrates(now: Date): Unit = {
535+
hashRate.retain {
536+
case (_, (_, reported)) =>
537+
Duration.between(reported.toInstant, now.toInstant).toMillis < jsonRpcConfig.minerActiveTimeout.toMillis
545538
}
546539
}
547540

0 commit comments

Comments
 (0)