Commit 6f9094e2 authored by Nick Mathewson's avatar Nick Mathewson 🦀
Browse files

Merge branch 'intern-more' into 'main'

tor-netdoc: use InternCache to save memory for families and protover lists

Closes #384 and #385

See merge request !398
parents c0baf86e 8f430fd5
Loading
Loading
Loading
Loading
+8 −0
Original line number Diff line number Diff line
@@ -29,6 +29,8 @@
//! Finally, there are the voting documents themselves that authorities
//! use in order to calculate the consensus.

use crate::util::intern::InternCache;

pub mod authcert;
pub mod microdesc;
pub mod netstatus;
@@ -42,3 +44,9 @@ pub mod routerdesc {
    /// The digest of a RouterDesc document, as reported in a NS consensus.
    pub type RdDigest = [u8; 20];
}

/// Cache of Protocols objects, for saving memory.
//
/// This only holds weak references to the objects, so we don't
/// need to worry about running out of space because of stale entries.
static PROTOVERS_CACHE: InternCache<tor_protover::Protocols> = InternCache::new();
+7 −3
Original line number Diff line number Diff line
@@ -63,7 +63,7 @@ pub struct Microdesc {
    /// Public key used for the ntor circuit extension protocol.
    ntor_onion_key: curve25519::PublicKey,
    /// Declared family for this relay.
    family: RelayFamily,
    family: Arc<RelayFamily>,
    /// List of IPv4 ports to which this relay will exit
    ipv4_policy: Arc<PortPolicy>,
    /// List of IPv6 ports to which this relay will exit
@@ -111,7 +111,7 @@ impl Microdesc {
    }
    /// Return the relay family for this microdesc
    pub fn family(&self) -> &RelayFamily {
        &self.family
        self.family.as_ref()
    }
    /// Return the ed25519 identity for this microdesc, if its
    /// Ed25519 identity is well-formed.
@@ -277,10 +277,14 @@ impl Microdesc {
            .into();

        // family
        //
        // (We don't need to add the relay's own ID to this family, as we do in
        // RouterDescs: the authorities already took care of that for us.)
        let family = body
            .maybe(FAMILY)
            .parse_args_as_str::<RelayFamily>()?
            .unwrap_or_else(RelayFamily::new);
            .unwrap_or_else(RelayFamily::new)
            .intern();

        // exit policies.
        let ipv4_policy = body
+1 −1
Original line number Diff line number Diff line
@@ -136,7 +136,7 @@ impl MicrodescBuilder {
        Ok(Microdesc {
            sha256,
            ntor_onion_key,
            family: self.family.clone(),
            family: self.family.clone().intern(),
            ipv4_policy: self.ipv4_policy.clone().intern(),
            ipv6_policy: self.ipv6_policy.clone().intern(),
            ed25519_id,
+7 −4
Original line number Diff line number Diff line
@@ -10,6 +10,7 @@ mod md;
mod ns;

use super::{NetstatusKwd, RelayFlags, RelayWeight};
use crate::doc;
use crate::parse::parser::Section;
use crate::types::misc::*;
use crate::types::version::TorVersion;
@@ -44,7 +45,7 @@ struct GenericRouterStatus<D> {
    /// Version of the software that this relay is running.
    version: Option<Version>,
    /// List of subprotocol versions supported by this relay.
    protos: Protocols,
    protos: Arc<Protocols>,
    /// Information about how to weight this relay when choosing a
    /// relay at random.
    weight: RelayWeight,
@@ -212,9 +213,11 @@ where
        // PR line
        let protos = {
            let tok = sec.required(RS_PR)?;
            doc::PROTOVERS_CACHE.intern(
                tok.args_as_str()
                    .parse::<Protocols>()
                .map_err(|e| EK::BadArgument.at_pos(tok.pos()).with_source(e))?
                    .map_err(|e| EK::BadArgument.at_pos(tok.pos()).with_source(e))?,
            )
        };

        // W line
+2 −1
Original line number Diff line number Diff line
//! Provide builder functionality for routerstatuses.

use super::{GenericRouterStatus, MdConsensusRouterStatus};
use crate::doc;
use crate::doc::microdesc::MdDigest;
use crate::doc::netstatus::{ConsensusBuilder, RelayFlags, RelayWeight};
use crate::{BuildError as Error, BuildResult as Result};
@@ -143,7 +144,7 @@ impl<D: Clone> RouterStatusBuilder<D> {
            addrs: self.addrs.clone(),
            doc_digest,
            version,
            protos,
            protos: doc::PROTOVERS_CACHE.intern(protos),
            flags: self.flags,
            weight,
        })
Loading