cleanup(ext/web/BlobStore): avoid redundant Arc<Box<T>> alloc (#11693)

This commit is contained in:
Aaron O'Mullan 2021-08-14 10:27:27 +02:00 committed by GitHub
parent b8cfc95470
commit 1d1507384b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 13 deletions

View file

@ -968,7 +968,7 @@ mod tests {
let specifier = blob_store.insert_object_url(
Blob {
media_type: "application/typescript".to_string(),
parts: vec![Arc::new(Box::new(InMemoryBlobPart::from(bytes)))],
parts: vec![Arc::new(InMemoryBlobPart::from(bytes))],
},
None,
);

View file

@ -15,7 +15,7 @@ use uuid::Uuid;
use crate::Location;
pub type PartMap = HashMap<Uuid, Arc<Box<dyn BlobPart + Send + Sync>>>;
pub type PartMap = HashMap<Uuid, Arc<dyn BlobPart + Send + Sync>>;
#[derive(Clone, Default, Debug)]
pub struct BlobStore {
@ -24,17 +24,14 @@ pub struct BlobStore {
}
impl BlobStore {
pub fn insert_part(&self, part: Box<dyn BlobPart + Send + Sync>) -> Uuid {
pub fn insert_part(&self, part: Arc<dyn BlobPart + Send + Sync>) -> Uuid {
let id = Uuid::new_v4();
let mut parts = self.parts.lock();
parts.insert(id, Arc::new(part));
parts.insert(id, part);
id
}
pub fn get_part(
&self,
id: &Uuid,
) -> Option<Arc<Box<dyn BlobPart + Send + Sync>>> {
pub fn get_part(&self, id: &Uuid) -> Option<Arc<dyn BlobPart + Send + Sync>> {
let parts = self.parts.lock();
let part = parts.get(id);
part.cloned()
@ -43,7 +40,7 @@ impl BlobStore {
pub fn remove_part(
&self,
id: &Uuid,
) -> Option<Arc<Box<dyn BlobPart + Send + Sync>>> {
) -> Option<Arc<dyn BlobPart + Send + Sync>> {
let mut parts = self.parts.lock();
parts.remove(id)
}
@ -86,7 +83,7 @@ impl BlobStore {
pub struct Blob {
pub media_type: String,
pub parts: Vec<Arc<Box<dyn BlobPart + Send + Sync>>>,
pub parts: Vec<Arc<dyn BlobPart + Send + Sync>>,
}
impl Blob {
@ -143,7 +140,7 @@ impl BlobPart for InMemoryBlobPart {
#[derive(Debug)]
pub struct SlicedBlobPart {
part: Arc<Box<dyn BlobPart + Send + Sync>>,
part: Arc<dyn BlobPart + Send + Sync>,
start: usize,
len: usize,
}
@ -167,7 +164,7 @@ pub fn op_blob_create_part(
) -> Result<Uuid, AnyError> {
let blob_store = state.borrow::<BlobStore>();
let part = InMemoryBlobPart(data.to_vec());
let id = blob_store.insert_part(Box::new(part));
let id = blob_store.insert_part(Arc::new(part));
Ok(id)
}
@ -198,7 +195,7 @@ pub fn op_blob_slice_part(
}
let sliced_part = SlicedBlobPart { part, start, len };
let id = blob_store.insert_part(Box::new(sliced_part));
let id = blob_store.insert_part(Arc::new(sliced_part));
Ok(id)
}