Skip to content

Commit f794632

Browse files
Implement Allocative for the protocol views. (#4687)
## Motivation Tracking the size of the objects being used is important for considering the memory usage and resolving the OOM. ## Proposal The `Allocative` trait is implemented for the views, and the code is followed from there. Several design decisions were made: * For the Weighted distribution, the entries are dropped. * In the views, the context is not taken into account of memory usage. * For the keys of Ed25519, Secp256k1, EvmSecp256k1, the sizes are written explicitly. ## Test Plan The CI. Adding in `process_confirmed_block` the following ```rust tracing::info!("process_confirmed_block, size_of_unique={}", allocative::size_of_unique(&self.chain)); ``` works as expected. How to implement more systematically is open to discussion. ## Release Plan The goal is to add it to the TestNet Conway. ## Links None.
1 parent 6c87f81 commit f794632

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+484
-93
lines changed

Cargo.lock

Lines changed: 36 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ license = "Apache-2.0"
7272
edition = "2021"
7373

7474
[workspace.dependencies]
75+
allocative = "0.3.4"
7576
alloy = { version = "1.0.5", default-features = false }
7677
alloy-primitives = { version = "1.1.0", default-features = false, features = [
7778
"serde",

examples/Cargo.lock

Lines changed: 35 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

linera-base/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ web = [
3535
]
3636

3737
[dependencies]
38+
allocative.workspace = true
3839
alloy-primitives.workspace = true
3940
anyhow.workspace = true
4041
async-graphql.workspace = true

linera-base/src/crypto/hash.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use std::ops::RangeInclusive;
99
use std::{borrow::Cow, fmt, io, str::FromStr};
1010

11+
use allocative::{Allocative, Visitor};
1112
#[cfg(with_testing)]
1213
use alloy_primitives::FixedBytes;
1314
use alloy_primitives::{Keccak256, B256};
@@ -33,6 +34,12 @@ use crate::{
3334
#[cfg_attr(with_testing, derive(Default))]
3435
pub struct CryptoHash(B256);
3536

37+
impl Allocative for CryptoHash {
38+
fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>) {
39+
visitor.visit_simple_sized::<Self>();
40+
}
41+
}
42+
3643
impl CryptoHash {
3744
/// Computes a hash.
3845
pub fn new<'de, T: BcsHashable<'de>>(value: &T) -> Self {

linera-base/src/crypto/mod.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ mod secp256k1;
1111
pub mod signer;
1212
use std::{fmt::Display, io, num::ParseIntError, str::FromStr};
1313

14+
use allocative::Allocative;
1415
use alloy_primitives::FixedBytes;
1516
use custom_debug_derive::Debug;
1617
pub use ed25519::{Ed25519PublicKey, Ed25519SecretKey, Ed25519Signature};
@@ -24,7 +25,7 @@ use serde::{Deserialize, Serialize};
2425
pub use signer::*;
2526
use thiserror::Error;
2627

27-
use crate::{hex_debug, identifiers::AccountOwner};
28+
use crate::{hex_debug, identifiers::AccountOwner, visit_allocative_simple};
2829

2930
/// The public key of a validator.
3031
pub type ValidatorPublicKey = secp256k1::Secp256k1PublicKey;
@@ -63,14 +64,15 @@ pub enum SignatureScheme {
6364
WitType,
6465
WitLoad,
6566
WitStore,
67+
Allocative,
6668
)]
6769
pub enum AccountPublicKey {
6870
/// Ed25519 public key.
69-
Ed25519(ed25519::Ed25519PublicKey),
71+
Ed25519(#[allocative(visit = visit_allocative_simple)] ed25519::Ed25519PublicKey),
7072
/// secp256k1 public key.
71-
Secp256k1(secp256k1::Secp256k1PublicKey),
73+
Secp256k1(#[allocative(visit = visit_allocative_simple)] secp256k1::Secp256k1PublicKey),
7274
/// EVM secp256k1 public key.
73-
EvmSecp256k1(secp256k1::evm::EvmPublicKey),
75+
EvmSecp256k1(#[allocative(visit = visit_allocative_simple)] secp256k1::evm::EvmPublicKey),
7476
}
7577

7678
/// The private key of a chain owner.
@@ -85,28 +87,34 @@ pub enum AccountSecretKey {
8587
}
8688

8789
/// The signature of a chain owner.
88-
#[derive(Eq, PartialEq, Copy, Clone, Debug, Serialize, Deserialize)]
90+
#[derive(Eq, PartialEq, Copy, Clone, Debug, Serialize, Deserialize, Allocative)]
8991
pub enum AccountSignature {
9092
/// Ed25519 signature.
9193
Ed25519 {
9294
/// Signature of the value.
95+
#[allocative(visit = visit_allocative_simple)]
9396
signature: ed25519::Ed25519Signature,
9497
/// Public key of the signer.
98+
#[allocative(visit = visit_allocative_simple)]
9599
public_key: ed25519::Ed25519PublicKey,
96100
},
97101
/// secp256k1 signature.
98102
Secp256k1 {
99103
/// Signature of the value.
104+
#[allocative(visit = visit_allocative_simple)]
100105
signature: secp256k1::Secp256k1Signature,
101106
/// Public key of the signer.
107+
#[allocative(visit = visit_allocative_simple)]
102108
public_key: secp256k1::Secp256k1PublicKey,
103109
},
104110
/// EVM secp256k1 signature.
105111
EvmSecp256k1 {
106112
/// Signature of the value.
113+
#[allocative(visit = visit_allocative_simple)]
107114
signature: secp256k1::evm::EvmSignature,
108115
/// EVM address of the signer.
109116
#[debug(with = "hex_debug")]
117+
#[allocative(visit = visit_allocative_simple)]
110118
address: [u8; 20],
111119
},
112120
}

linera-base/src/crypto/secp256k1/mod.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use std::{
1313
str::FromStr,
1414
};
1515

16+
use allocative::{Allocative, Visitor};
1617
use k256::{
1718
ecdsa::{Signature, SigningKey, VerifyingKey},
1819
elliptic_curve::sec1::FromEncodedPoint,
@@ -44,6 +45,12 @@ pub struct Secp256k1SecretKey(pub SigningKey);
4445
#[derive(Eq, PartialEq, Ord, PartialOrd, Copy, Clone)]
4546
pub struct Secp256k1PublicKey(pub VerifyingKey);
4647

48+
impl Allocative for Secp256k1PublicKey {
49+
fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>) {
50+
visitor.visit_simple_sized::<Self>();
51+
}
52+
}
53+
4754
impl Hash for Secp256k1PublicKey {
4855
fn hash<H: Hasher>(&self, state: &mut H) {
4956
self.0.to_encoded_point(true).as_bytes().hash(state);
@@ -63,6 +70,12 @@ pub struct Secp256k1KeyPair {
6370
#[derive(Eq, PartialEq, Copy, Clone)]
6471
pub struct Secp256k1Signature(pub Signature);
6572

73+
impl Allocative for Secp256k1Signature {
74+
fn visit<'a, 'b: 'a>(&self, visitor: &'a mut Visitor<'b>) {
75+
visitor.visit_simple_sized::<Self>();
76+
}
77+
}
78+
6679
impl Secp256k1PublicKey {
6780
/// A fake public key used for testing.
6881
#[cfg(all(with_testing, not(target_arch = "wasm32")))]

0 commit comments

Comments
 (0)