diff options
author | Alex Auvolat <alex@adnab.me> | 2024-03-15 17:36:55 +0100 |
---|---|---|
committer | Alex Auvolat <alex@adnab.me> | 2024-03-15 17:36:55 +0100 |
commit | 4cd9081dc3a41594174480c1565fd2427550c50a (patch) | |
tree | 8a09db13b11477eaba40fa1a12347015738a5f8d /src/server.rs | |
parent | f6a778698b8f0b709a7afa693ab42f8a0c314fe7 (diff) | |
download | datagengo-4cd9081dc3a41594174480c1565fd2427550c50a.tar.gz datagengo-4cd9081dc3a41594174480c1565fd2427550c50a.zip |
reduce memory usage & fix scroll to top
Diffstat (limited to 'src/server.rs')
-rw-r--r-- | src/server.rs | 39 |
1 files changed, 16 insertions, 23 deletions
diff --git a/src/server.rs b/src/server.rs index 5e5d61b..14368e0 100644 --- a/src/server.rs +++ b/src/server.rs @@ -36,13 +36,14 @@ pub async fn server_main() -> tide::Result<()> { eprintln!("Loading furigana overrides..."); let furigana_overrides = read_furigana_overrides().expect("read_furigana_overrides"); + eprintln!("RAM: {}", ALLOCATOR.allocated() / 1024); eprintln!("Loading JMdict_e.xml..."); let jmdict_raw = fs::read_to_string("data/JMdict_e.xml").expect("read_jmdict"); - let jmdict_raw: &'static str = String::leak(jmdict_raw); + eprintln!("RAM: {}", ALLOCATOR.allocated() / 1024); eprintln!("Parsing JMdict_e.xml..."); - let jmdict = roxmltree::Document::parse_with_options( + let jmdict_xml = roxmltree::Document::parse_with_options( &jmdict_raw, roxmltree::ParsingOptions { allow_dtd: true, @@ -50,10 +51,14 @@ pub async fn server_main() -> tide::Result<()> { }, ) .expect("parse_jmdict"); - let jmdict_xml = Box::leak(Box::new(jmdict)); + eprintln!("RAM: {}", ALLOCATOR.allocated() / 1024); eprintln!("Indexing JMdict_e.xml..."); - let jmdict_idx = index_jmdict(jmdict_xml); + let jmdict_idx = index_jmdict(&jmdict_xml); + eprintln!("RAM: {}", ALLOCATOR.allocated() / 1024); + drop(jmdict_xml); + drop(jmdict_raw); + eprintln!("RAM: {}", ALLOCATOR.allocated() / 1024); eprintln!("Loading batches.json..."); let batches = read_batches().expect("read/parse"); @@ -66,8 +71,6 @@ pub async fn server_main() -> tide::Result<()> { // ---- setup http server ---- let state: State = Box::leak(Box::new(StateStruct { - jmdict_raw, - jmdict_xml, jmdict_idx, batches, index, @@ -99,9 +102,7 @@ pub async fn server_main() -> tide::Result<()> { type State = &'static StateStruct; #[allow(dead_code)] struct StateStruct { - jmdict_raw: &'static str, - jmdict_xml: &'static roxmltree::Document<'static>, - jmdict_idx: DictIndex<'static>, + jmdict_idx: DictIndex, batches: &'static [Batch], index: &'static str, examples: &'static [Example], @@ -233,13 +234,7 @@ async fn gen_examples_page(mut req: Request<State>) -> tide::Result { } if let Some(ents) = state.jmdict_idx.get(keb) { for ent in ents.iter() { - let ent_r_ele = ent.children().find(|x| x.has_tag_name("r_ele")).unwrap(); - let ent_reb = ent_r_ele - .children() - .find(|x| x.has_tag_name("reb")) - .unwrap(); - let ent_reb = ent_reb.text().unwrap().trim(); - if reb.map(|x| x != ent_reb).unwrap_or(false) { + if reb.map(|x| x != ent.reb).unwrap_or(false) { continue; } expl += &format!( @@ -247,17 +242,15 @@ async fn gen_examples_page(mut req: Request<State>) -> tide::Result { keb ); - for sense in ent.children().filter(|x| x.has_tag_name("sense")) { - if let Some(s) = sense.children().find(|x| x.has_tag_name("gloss")) { - if !expl.ends_with('>') { - expl += "; "; - } - expl += s.text().unwrap().trim(); + for sense in ent.sense.iter() { + if !expl.ends_with('>') { + expl += "; "; } + expl += sense; } expl += &format!( r#"</td><td style="word-break: keep-all" class="tab_large font_ja">{}</td></tr>"#, - ent_reb + ent.reb ); } } |