Skip to content

Commit

Permalink
builder: delay free data structure to reduce image build time
Browse files Browse the repository at this point in the history
According to perf flame graph, it takes a long time to free objects
used by image builder. In most common cases, the builder will only
run once and exit, so it's unnecessary to free those used objects.

Signed-off-by: Jiang Liu <[email protected]>
  • Loading branch information
jiangliu committed May 23, 2023
1 parent 809f8d9 commit c8d460a
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 6 deletions.
6 changes: 4 additions & 2 deletions rafs/src/builder/core/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex, MutexGuard};

use anyhow::{bail, Result};
use nydus_utils::{root_tracer, timing_tracer};
use nydus_utils::{lazy_drop, root_tracer, timing_tracer};

use super::node::{ChunkSource, Node, NodeChunk, NodeInfo};
use super::overlay::{Overlay, WhiteoutType};
Expand Down Expand Up @@ -180,8 +180,10 @@ impl Tree {
// Handle the root node.
upper.lock_node().overlay = Overlay::UpperModification;
self.node = upper.node.clone();
self.merge_children(ctx, &upper)?;
lazy_drop(upper);

self.merge_children(ctx, &upper)
Ok(())
}

fn merge_children(&mut self, ctx: &BuildContext, upper: &Tree) -> Result<()> {
Expand Down
4 changes: 3 additions & 1 deletion rafs/src/builder/directory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::fs;
use std::fs::DirEntry;

use anyhow::{anyhow, Context, Result};
use nydus_utils::{event_tracer, root_tracer, timing_tracer};
use nydus_utils::{event_tracer, lazy_drop, root_tracer, timing_tracer};

use super::core::blob::Blob;
use super::core::context::{
Expand Down Expand Up @@ -190,6 +190,8 @@ impl Builder for DirectoryBuilder {
)?;
}

lazy_drop(bootstrap_ctx);

BuildOutput::new(blob_mgr, &bootstrap_mgr.bootstrap_storage)
}
}
4 changes: 3 additions & 1 deletion rafs/src/builder/stargz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use nydus_storage::{RAFS_MAX_CHUNKS_PER_BLOB, RAFS_MAX_CHUNK_SIZE};
use nydus_utils::compact::makedev;
use nydus_utils::compress::{self, compute_compressed_gzip_size};
use nydus_utils::digest::{self, DigestData, RafsDigest};
use nydus_utils::{root_tracer, timing_tracer, try_round_up_4k, ByteSize};
use nydus_utils::{lazy_drop, root_tracer, timing_tracer, try_round_up_4k, ByteSize};
use serde::{Deserialize, Serialize};

use super::core::blob::Blob;
Expand Down Expand Up @@ -897,6 +897,8 @@ impl Builder for StargzBuilder {
)?;
}

lazy_drop(bootstrap_ctx);

BuildOutput::new(blob_mgr, &bootstrap_mgr.bootstrap_storage)
}
}
Expand Down
4 changes: 3 additions & 1 deletion rafs/src/builder/tarball.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use nydus_utils::compact::makedev;
use nydus_utils::compress::zlib_random::{ZranReader, ZRAN_READER_BUF_SIZE};
use nydus_utils::compress::ZlibDecoder;
use nydus_utils::digest::RafsDigest;
use nydus_utils::{div_round_up, root_tracer, timing_tracer, BufReaderInfo, ByteSize};
use nydus_utils::{div_round_up, lazy_drop, root_tracer, timing_tracer, BufReaderInfo, ByteSize};

use super::core::blob::Blob;
use super::core::context::{
Expand Down Expand Up @@ -585,6 +585,8 @@ impl Builder for TarballBuilder {
)?;
}

lazy_drop(bootstrap_ctx);

BuildOutput::new(blob_mgr, &bootstrap_mgr.bootstrap_storage)
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/bin/nydus-image/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ use nydus_storage::factory::BlobFactory;
use nydus_storage::meta::{format_blob_features, BatchContextGenerator};
use nydus_storage::{RAFS_DEFAULT_CHUNK_SIZE, RAFS_MAX_CHUNK_SIZE};
use nydus_utils::trace::{EventTracerClass, TimingTracerClass, TraceClass};
use nydus_utils::{compress, digest, event_tracer, register_tracer, root_tracer, timing_tracer};
use nydus_utils::{
compress, digest, event_tracer, lazy_drop, register_tracer, root_tracer, timing_tracer,
};
use serde::{Deserialize, Serialize};

use crate::unpack::{OCIUnpacker, Unpacker};
Expand Down Expand Up @@ -1026,6 +1028,8 @@ impl Command {
"total_build"
)?;

lazy_drop(build_ctx);

// Some operations like listing xattr pairs of certain namespace need the process
// to be privileged. Therefore, trace what euid and egid are.
event_tracer!("euid", "{}", geteuid());
Expand Down
15 changes: 15 additions & 0 deletions utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,21 @@ impl Delayer {
}
}

struct LazyDrop<T> {
v: T,
}

unsafe impl<T> Send for LazyDrop<T> {}

/// Lazy drop of object.
pub fn lazy_drop<T: 'static>(v: T) {
let v = LazyDrop { v };
std::thread::spawn(move || {
std::thread::sleep(Duration::from_secs(600));
let _ = v.v;
});
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit c8d460a

Please sign in to comment.