Skip to content

Commit

Permalink
Merge pull request #2052 from subspace/gemini-3f-backport-reduce-memo…
Browse files Browse the repository at this point in the history
…ry-usage-during-plotting

Gemini 3f backport: Reduce memory usage during plotting/replotting by deferring memory allocation
  • Loading branch information
nazar-pc authored Oct 4, 2023
2 parents 914ca84 + d539e3a commit 7b120fe
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 26 deletions.
6 changes: 2 additions & 4 deletions crates/pallet-subspace/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ use subspace_core_primitives::{
use subspace_erasure_coding::ErasureCoding;
use subspace_farmer_components::auditing::audit_sector;
use subspace_farmer_components::plotting::{plot_sector, PieceGetterRetryPolicy};
use subspace_farmer_components::sector::{sector_size, SectorMetadataChecksummed};
use subspace_farmer_components::FarmerProtocolInfo;
use subspace_proof_of_space::shim::ShimTable;
use subspace_proof_of_space::{Table, TableGenerator};
Expand Down Expand Up @@ -416,13 +415,12 @@ pub fn create_signed_vote(
min_sector_lifetime: HistorySize::from(NonZeroU64::new(4).unwrap()),
};
let pieces_in_sector = farmer_protocol_info.max_pieces_in_sector;
let sector_size = sector_size(pieces_in_sector);

let mut table_generator = PosTable::generator();

for sector_index in iter::from_fn(|| Some(rand::random())) {
let mut plotted_sector_bytes = vec![0; sector_size];
let mut plotted_sector_metadata_bytes = vec![0; SectorMetadataChecksummed::encoded_size()];
let mut plotted_sector_bytes = Vec::new();
let mut plotted_sector_metadata_bytes = Vec::new();

let plotted_sector = block_on(plot_sector::<_, PosTable>(
&public_key,
Expand Down
6 changes: 2 additions & 4 deletions crates/sp-lightclient/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ use subspace_core_primitives::{
use subspace_erasure_coding::ErasureCoding;
use subspace_farmer_components::auditing::audit_sector;
use subspace_farmer_components::plotting::{plot_sector, PieceGetterRetryPolicy};
use subspace_farmer_components::sector::{sector_size, SectorMetadataChecksummed};
use subspace_farmer_components::FarmerProtocolInfo;
use subspace_proof_of_space::{Table, TableGenerator};
use subspace_solving::REWARD_SIGNING_CONTEXT;
Expand Down Expand Up @@ -153,13 +152,12 @@ fn valid_header(
let public_key = PublicKey::from(keypair.public.to_bytes());

let pieces_in_sector = farmer_parameters.farmer_protocol_info.max_pieces_in_sector;
let sector_size = sector_size(pieces_in_sector);

let mut table_generator = PosTable::generator();

for sector_index in iter::from_fn(|| Some(rand::random())) {
let mut plotted_sector_bytes = vec![0; sector_size];
let mut plotted_sector_metadata_bytes = vec![0; SectorMetadataChecksummed::encoded_size()];
let mut plotted_sector_bytes = Vec::new();
let mut plotted_sector_metadata_bytes = Vec::new();

let plotted_sector = block_on(plot_sector::<_, PosTable>(
&public_key,
Expand Down
4 changes: 2 additions & 2 deletions crates/subspace-farmer-components/benches/auditing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ pub fn criterion_benchmark(c: &mut Criterion) {
} else {
println!("Plotting one sector...");

let mut plotted_sector_bytes = vec![0; sector_size];
let mut plotted_sector_metadata_bytes = vec![0; SectorMetadataChecksummed::encoded_size()];
let mut plotted_sector_bytes = Vec::new();
let mut plotted_sector_metadata_bytes = Vec::new();

let plotted_sector = block_on(plot_sector::<_, PosTable>(
&public_key,
Expand Down
6 changes: 3 additions & 3 deletions crates/subspace-farmer-components/benches/plotting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use subspace_core_primitives::crypto::kzg::Kzg;
use subspace_core_primitives::{HistorySize, PublicKey, Record, RecordedHistorySegment};
use subspace_erasure_coding::ErasureCoding;
use subspace_farmer_components::plotting::{plot_sector, PieceGetterRetryPolicy};
use subspace_farmer_components::sector::{sector_size, SectorMetadataChecksummed};
use subspace_farmer_components::sector::sector_size;
use subspace_farmer_components::FarmerProtocolInfo;
use subspace_proof_of_space::chia::ChiaTable;
use subspace_proof_of_space::Table;
Expand Down Expand Up @@ -58,8 +58,8 @@ fn criterion_benchmark(c: &mut Criterion) {
};

let sector_size = sector_size(pieces_in_sector);
let mut sector_bytes = vec![0; sector_size];
let mut sector_metadata_bytes = vec![0; SectorMetadataChecksummed::encoded_size()];
let mut sector_bytes = Vec::new();
let mut sector_metadata_bytes = Vec::new();

let mut group = c.benchmark_group("plotting");
group.throughput(Throughput::Bytes(sector_size as u64));
Expand Down
4 changes: 2 additions & 2 deletions crates/subspace-farmer-components/benches/proving.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ pub fn criterion_benchmark(c: &mut Criterion) {
} else {
println!("Plotting one sector...");

let mut plotted_sector_bytes = vec![0; sector_size];
let mut plotted_sector_metadata_bytes = vec![0; SectorMetadataChecksummed::encoded_size()];
let mut plotted_sector_bytes = Vec::new();
let mut plotted_sector_metadata_bytes = Vec::new();

let plotted_sector = block_on(plot_sector::<_, PosTable>(
&public_key,
Expand Down
4 changes: 2 additions & 2 deletions crates/subspace-farmer-components/benches/reading.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ pub fn criterion_benchmark(c: &mut Criterion) {
} else {
println!("Plotting one sector...");

let mut plotted_sector_bytes = vec![0; sector_size];
let mut plotted_sector_metadata_bytes = vec![0; SectorMetadataChecksummed::encoded_size()];
let mut plotted_sector_bytes = Vec::new();
let mut plotted_sector_metadata_bytes = Vec::new();

let plotted_sector = block_on(plot_sector::<_, PosTable>(
&public_key,
Expand Down
17 changes: 13 additions & 4 deletions crates/subspace-farmer-components/src/plotting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ pub enum PlottingError {
/// beginning of the sector (seek to desired offset before calling this function and seek back
/// afterwards if necessary).
///
/// Sector output and sector metadata output should be either empty (in which case they'll be
/// resized to correct size automatically) or correctly sized from the beginning or else error will
/// be returned.
///
/// NOTE: Even though this function is async, it has blocking code inside and must be running in a
/// separate thread in order to prevent blocking an executor.
#[allow(clippy::too_many_arguments)]
Expand All @@ -167,8 +171,8 @@ pub async fn plot_sector<PG, PosTable>(
kzg: &Kzg,
erasure_coding: &ErasureCoding,
pieces_in_sector: u16,
sector_output: &mut [u8],
sector_metadata_output: &mut [u8],
sector_output: &mut Vec<u8>,
sector_metadata_output: &mut Vec<u8>,
table_generator: &mut PosTable::Generator,
) -> Result<PlottedSector, PlottingError>
where
Expand All @@ -181,14 +185,16 @@ where

let sector_size = sector_size(pieces_in_sector);

if sector_output.len() != sector_size {
if !sector_output.is_empty() && sector_output.len() != sector_size {
return Err(PlottingError::BadSectorOutputSize {
provided: sector_output.len(),
expected: sector_size,
});
}

if sector_metadata_output.len() < SectorMetadataChecksummed::encoded_size() {
if !sector_metadata_output.is_empty()
&& sector_metadata_output.len() != SectorMetadataChecksummed::encoded_size()
{
return Err(PlottingError::BadSectorMetadataOutputSize {
provided: sector_metadata_output.len(),
expected: SectorMetadataChecksummed::encoded_size(),
Expand Down Expand Up @@ -341,6 +347,9 @@ where
});
});

sector_output.resize(sector_size, 0);
sector_metadata_output.resize(SectorMetadataChecksummed::encoded_size(), 0);

// Write sector to disk in form of following regions:
// * sector contents map
// * record chunks as s-buckets
Expand Down
4 changes: 2 additions & 2 deletions crates/subspace-farmer/src/single_disk_farm/plotting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,8 @@ where
let plotted_sector;

(sector, sector_metadata, table_generator, plotted_sector) = {
let mut sector = vec![0; sector_size];
let mut sector_metadata = vec![0; sector_metadata_size];
let mut sector = Vec::new();
let mut sector_metadata = Vec::new();

let piece_getter = piece_getter.clone();
let kzg = kzg.clone();
Expand Down
5 changes: 2 additions & 3 deletions test/subspace-test-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ use subspace_core_primitives::{HistorySize, PosSeed, PublicKey, Record, SegmentI
use subspace_erasure_coding::ErasureCoding;
use subspace_farmer_components::auditing::audit_sector;
use subspace_farmer_components::plotting::{plot_sector, PieceGetterRetryPolicy, PlottedSector};
use subspace_farmer_components::sector::{sector_size, SectorMetadataChecksummed};
use subspace_farmer_components::FarmerProtocolInfo;
use subspace_proof_of_space::{Table, TableGenerator};
use subspace_runtime_primitives::opaque::Block;
Expand Down Expand Up @@ -240,8 +239,8 @@ where
.next()
.expect("First block is always producing one segment; qed");
let history_size = HistorySize::from(SegmentIndex::ZERO);
let mut sector = vec![0u8; sector_size(pieces_in_sector)];
let mut sector_metadata = vec![0u8; SectorMetadataChecksummed::encoded_size()];
let mut sector = Vec::new();
let mut sector_metadata = Vec::new();
let sector_index = 0;
let public_key = PublicKey::from(keypair.public.to_bytes());
let farmer_protocol_info = FarmerProtocolInfo {
Expand Down

0 comments on commit 7b120fe

Please sign in to comment.