Avoid string initialization in network metrics and replace by &str where possible (#1898)

## Issue Addressed

NA

## Proposed Changes

Removes most of the temporary string initializations in network metrics and replaces them by directly using `&str`. This further improves on PR https://github.com/sigp/lighthouse/pull/1895.

For the subnet id handling the current approach uses a build script to create a static map. This has the disadvantage that the build script hardcodes the number of subnets. If we want to use more than 64 subnets we need to adjust this in the build script.

## Additional Info

We still have some string initializations for the enum `PeerKind`. To also replace that by `&str` I created a PR in the libp2p dependency: https://github.com/sigp/rust-libp2p/pull/91. Either we wait with merging until this dependency PR is merged (and all conflicts with the newest libp2p version are resolved) or we just merge as is and I will create another PR when the dependency is ready.
This commit is contained in:
blacktemplar
2020-11-18 23:31:37 +00:00
parent bcc7f6b143
commit 3408de8151
8 changed files with 107 additions and 69 deletions

View File

@@ -42,7 +42,7 @@ regex = "1.3.9"
[dependencies.libp2p]
#version = "0.23.0"
git = "https://github.com/sigp/rust-libp2p"
rev = "b6278e1ba7b6bcfad1eef300f72148705da5d8d2"
rev = "f53d02bc873fef2bf52cd31e3d5ce366a41d8a8c"
default-features = false
features = ["websocket", "identify", "mplex", "yamux", "noise", "gossipsub", "dns", "tcp-tokio"]

View File

@@ -98,9 +98,29 @@ impl std::fmt::Display for Client {
}
}
impl ClientKind {
pub fn as_static_ref(&self) -> &'static str {
use ClientKind::*;
match self {
Lighthouse => "Lighthouse",
Nimbus => "Nimbus",
Teku => "Teku",
Prysm => "Prysm",
Lodestar => "Lodestar",
Unknown => "Unknown",
}
}
}
impl AsRef<str> for ClientKind {
fn as_ref(&self) -> &str {
self.as_static_ref()
}
}
impl std::fmt::Display for ClientKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self)
f.write_str(self.as_ref())
}
}

View File

@@ -52,15 +52,25 @@ pub enum GossipKind {
AttesterSlashing,
}
impl AsRef<str> for GossipKind {
fn as_ref(&self) -> &str {
use GossipKind::*;
match self {
BeaconBlock => "beacon_block",
BeaconAggregateAndProof => "beacon_aggregate_and_proof",
Attestation(_) => "beacon_attestation",
VoluntaryExit => "voluntary_exit",
ProposerSlashing => "proposer_slashing",
AttesterSlashing => "attester_slashing",
}
}
}
impl std::fmt::Display for GossipKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
GossipKind::BeaconBlock => write!(f, "beacon_block"),
GossipKind::BeaconAggregateAndProof => write!(f, "beacon_aggregate_and_proof"),
GossipKind::Attestation(subnet_id) => write!(f, "beacon_attestation_{}", **subnet_id),
GossipKind::VoluntaryExit => write!(f, "voluntary_exit"),
GossipKind::ProposerSlashing => write!(f, "proposer_slashing"),
GossipKind::AttesterSlashing => write!(f, "attester_slashing"),
x => f.write_str(x.as_ref()),
}
}
}