Skip to content

Commit a28aa5e

Browse files
committed
try this
1 parent 39aa14c commit a28aa5e

File tree

2 files changed

+48
-44
lines changed

2 files changed

+48
-44
lines changed

cli/cache/module_info.rs

Lines changed: 46 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,16 @@ impl From<ModuleInfoCacheSourceHash> for String {
6868
/// deno_graph.
6969
pub struct ModuleInfoCache {
7070
conn: CacheDB,
71-
pending_inserts: Mutex<Vec<(String, &'static str, String, String)>>,
71+
sender: Mutex<
72+
Option<
73+
tokio::sync::mpsc::UnboundedSender<(
74+
String,
75+
&'static str,
76+
String,
77+
String,
78+
)>,
79+
>,
80+
>,
7281
}
7382

7483
impl ModuleInfoCache {
@@ -80,7 +89,7 @@ impl ModuleInfoCache {
8089
pub fn new(conn: CacheDB) -> Self {
8190
Self {
8291
conn,
83-
pending_inserts: Mutex::new(Vec::new()),
92+
sender: Default::default(),
8493
}
8594
}
8695

@@ -89,7 +98,7 @@ impl ModuleInfoCache {
8998
pub(crate) fn recreate_with_version(self, version: &'static str) -> Self {
9099
Self {
91100
conn: self.conn.recreate_with_version(version),
92-
pending_inserts: Mutex::new(Vec::new()),
101+
sender: Default::default(),
93102
}
94103
}
95104

@@ -140,50 +149,47 @@ impl ModuleInfoCache {
140149
source_hash: &ModuleInfoCacheSourceHash,
141150
module_info: &ModuleInfo,
142151
) -> Result<(), AnyError> {
143-
self.pending_inserts.lock().push((
152+
let data = (
144153
specifier.to_string(),
145154
serialize_media_type(media_type),
146155
source_hash.as_str().to_string(),
147156
serde_json::to_string(&module_info).unwrap(),
148-
));
149-
Ok(())
150-
}
151-
152-
pub fn flush_pending_inserts(&self) {
153-
let pending_inserts = {
154-
let mut items = self.pending_inserts.lock();
155-
let items = &mut *items;
156-
std::mem::take(items)
157-
};
157+
);
158158

159-
if pending_inserts.is_empty() {
160-
return;
159+
let mut sender = self.sender.lock();
160+
if let Some(sender) = &mut *sender {
161+
let _ = sender.send(data);
162+
} else {
163+
let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel();
164+
let conn = self.conn.clone();
165+
let _ = deno_core::unsync::spawn(async move {
166+
while let Some((specifier, media_type, source_hash, module_info)) =
167+
rx.recv().await
168+
{
169+
let conn = conn.clone();
170+
deno_core::unsync::spawn_blocking(move || {
171+
let sql = "
172+
INSERT OR REPLACE INTO
173+
moduleinfocache (specifier, media_type, source_hash, module_info)
174+
VALUES
175+
(?1, ?2, ?3, ?4)";
176+
let result = conn.execute(
177+
sql,
178+
params![specifier, media_type, source_hash, module_info],
179+
);
180+
if let Err(err) = result {
181+
log::debug!("Error saving module cache info. {:#}", err);
182+
}
183+
})
184+
.await
185+
.unwrap();
186+
}
187+
});
188+
tx.send(data).unwrap();
189+
*sender = Some(tx);
161190
}
162191

163-
let conn = self.conn.clone();
164-
let _ignore = deno_core::unsync::spawn_blocking(move || {
165-
let sql = "
166-
INSERT OR REPLACE INTO
167-
moduleinfocache (specifier, media_type, source_hash, module_info)
168-
VALUES
169-
(?1, ?2, ?3, ?4)";
170-
let result = conn.execute_bulk(
171-
sql,
172-
pending_inserts.iter(),
173-
|(specifier, media_type, source_hash, module_info)| {
174-
use deno_runtime::deno_webstorage::rusqlite::ToSql;
175-
[
176-
specifier as &dyn ToSql,
177-
media_type as &dyn ToSql,
178-
source_hash as &dyn ToSql,
179-
module_info as &dyn ToSql,
180-
]
181-
},
182-
);
183-
if let Err(err) = result {
184-
log::debug!("Error saving module cache info. {:#}", err);
185-
}
186-
});
192+
Ok(())
187193
}
188194

189195
pub fn as_module_analyzer<'a>(

cli/graph_util.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ impl ModuleGraphBuilder {
442442
.map(|r| r.as_reporter());
443443
let workspace_members =
444444
self.options.resolve_deno_graph_workspace_members()?;
445-
let result = self
445+
self
446446
.build_graph_with_npm_resolution_and_build_options(
447447
graph,
448448
options.roots,
@@ -461,9 +461,7 @@ impl ModuleGraphBuilder {
461461
resolver: Some(graph_resolver),
462462
},
463463
)
464-
.await;
465-
self.module_info_cache.flush_pending_inserts(); // flush regardless of sucess
466-
result
464+
.await
467465
}
468466

469467
async fn build_graph_with_npm_resolution_and_build_options<'a>(

0 commit comments

Comments
 (0)