Skip to content

Commit

Permalink
add threading enum
Browse files Browse the repository at this point in the history
  • Loading branch information
LaihoE committed Aug 21, 2024
1 parent 11be03d commit f25621f
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 50 deletions.
19 changes: 9 additions & 10 deletions src/node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ use std::collections::HashMap;
use std::fs::File;
use std::hash::RandomState;
use std::result::Result;
use std::time::Instant;

fn parse_demo(bytes: BytesVariant, parser: &mut Parser) -> Result<DemoOutput, Error> {
match bytes {
Expand Down Expand Up @@ -52,7 +51,7 @@ pub fn parse_voice(path_or_buf: Either<String, Buffer>) -> napi::Result<HashMap<
huffman_lookup_table: &vec![],
order_by_steamid: false,
};
let mut parser = Parser::new(settings, false);
let mut parser = Parser::new(settings, parser::parse_demo::ParsingMode::Normal);
let output = parse_demo(bytes, &mut parser)?;
let out = match convert_voice_data_to_wav(output.voice_data) {
Ok(out) => out,
Expand Down Expand Up @@ -85,7 +84,7 @@ pub fn list_game_events(path_or_buf: Either<String, Buffer>) -> napi::Result<Val
huffman_lookup_table: &huf,
order_by_steamid: false,
};
let mut parser = Parser::new(settings, false);
let mut parser = Parser::new(settings, parser::parse_demo::ParsingMode::Normal);
let output = parse_demo(bytes, &mut parser)?;

let v = Vec::from_iter(output.game_events_counter.iter());
Expand Down Expand Up @@ -116,7 +115,7 @@ pub fn parse_grenades(path_or_buf: Either<String, Buffer>) -> napi::Result<Value
huffman_lookup_table: &huf,
order_by_steamid: false,
};
let mut parser = Parser::new(settings, false);
let mut parser = Parser::new(settings, parser::parse_demo::ParsingMode::Normal);
let output = parse_demo(bytes, &mut parser)?;

let s = match serde_json::to_value(&output.projectiles) {
Expand Down Expand Up @@ -145,7 +144,7 @@ pub fn parse_header(path_or_buf: Either<String, Buffer>) -> napi::Result<Value>
huffman_lookup_table: &huf,
order_by_steamid: false,
};
let mut parser = Parser::new(settings, false);
let mut parser = Parser::new(settings, parser::parse_demo::ParsingMode::Normal);
let output = parse_demo(bytes, &mut parser)?;
let mut hm: HashMap<String, String> = HashMap::default();

Expand Down Expand Up @@ -209,7 +208,7 @@ pub fn parse_event(
huffman_lookup_table: &huf,
order_by_steamid: false,
};
let mut parser = Parser::new(settings, false);
let mut parser = Parser::new(settings, parser::parse_demo::ParsingMode::Normal);
let output = parse_demo(bytes, &mut parser)?;
let s = match serde_json::to_value(&output.game_events) {
Ok(s) => s,
Expand Down Expand Up @@ -271,7 +270,7 @@ pub fn parse_events(
huffman_lookup_table: &huf,
order_by_steamid: false,
};
let mut parser = Parser::new(settings, false);
let mut parser = Parser::new(settings, parser::parse_demo::ParsingMode::Normal);
let output = parse_demo(bytes, &mut parser)?;
let s = match serde_json::to_value(&output.game_events) {
Ok(s) => s,
Expand Down Expand Up @@ -331,7 +330,7 @@ pub fn parse_ticks(
order_by_steamid: order_by_steamid,
};

let mut parser = Parser::new(settings, false);
let mut parser = Parser::new(settings, parser::parse_demo::ParsingMode::Normal);
let output = parse_demo(bytes, &mut parser)?;
real_names.push("tick".to_owned());
real_names.push("steamid".to_owned());
Expand Down Expand Up @@ -402,7 +401,7 @@ pub fn parse_player_info(path_or_buf: Either<String, Buffer>) -> napi::Result<Va
huffman_lookup_table: &huf,
order_by_steamid: false,
};
let mut parser = Parser::new(settings, false);
let mut parser = Parser::new(settings, parser::parse_demo::ParsingMode::Normal);
let output = parse_demo(bytes, &mut parser)?;
let s = match serde_json::to_value(&output.player_md) {
Ok(s) => s,
Expand Down Expand Up @@ -431,7 +430,7 @@ pub fn parse_player_skins(path_or_buf: Either<String, Buffer>) -> napi::Result<V
huffman_lookup_table: &huf,
order_by_steamid: false,
};
let mut parser = Parser::new(settings, false);
let mut parser = Parser::new(settings, parser::parse_demo::ParsingMode::Normal);
let output = parse_demo(bytes, &mut parser)?;
let s = match serde_json::to_value(&output.skins) {
Ok(s) => s,
Expand Down
11 changes: 6 additions & 5 deletions src/parser/src/e2e_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ pub fn _create_ge_tests() {
order_by_steamid: false,
};

let mut ds = Parser::new(settings, true);
let mut ds = Parser::new(settings, crate::parse_demo::ParsingMode::ForceMultiThreaded);
// ds.is_multithreadable = false;
let file = File::open("test_demo.dem".to_string()).unwrap();
let mmap = unsafe { MmapOptions::new().map(&file).unwrap() };
Expand Down Expand Up @@ -688,7 +688,7 @@ pub fn _create_tests() {
order_by_steamid: false,
};

let mut ds = Parser::new(settings, true);
let mut ds = Parser::new(settings, crate::parse_demo::ParsingMode::ForceMultiThreaded);
let file = File::open("test_demo.dem".to_string()).unwrap();
let mmap = unsafe { MmapOptions::new().map(&file).unwrap() };
let d = ds.parse_demo(&mmap).unwrap();
Expand Down Expand Up @@ -1060,7 +1060,7 @@ fn create_data() -> (DemoOutput, PropController, BTreeMap<String, Vec<GameEvent>
order_by_steamid: false,
};

let mut ds = Parser::new(settings, false);
let mut ds = Parser::new(settings, crate::parse_demo::ParsingMode::ForceMultiThreaded);
let file = File::open("test_demo.dem".to_string()).unwrap();
let mmap = unsafe { MmapOptions::new().map(&file).unwrap() };
let out1 = ds.parse_demo(&mmap).unwrap();
Expand All @@ -1083,7 +1083,7 @@ fn create_data() -> (DemoOutput, PropController, BTreeMap<String, Vec<GameEvent>
huffman_lookup_table: &huf,
order_by_steamid: false,
};
let mut ds = Parser::new(settings, false);
let mut ds = Parser::new(settings, crate::parse_demo::ParsingMode::ForceMultiThreaded);
let file = File::open("test_demo.dem".to_string()).unwrap();
let mmap = unsafe { MmapOptions::new().map(&file).unwrap() };
let out2 = ds.parse_demo(&mmap).unwrap();
Expand Down Expand Up @@ -1198,7 +1198,8 @@ mod tests {
huffman_lookup_table: &huf,
order_by_steamid: false,
};
let mut ds = Parser::new(settings, true);

let mut ds = Parser::new(settings, crate::parse_demo::ParsingMode::ForceMultiThreaded);
let file = File::open("test_demo.dem").unwrap();
let mmap = unsafe { MmapOptions::new().map(&file).unwrap() };
let output = ds.parse_demo(&mmap).unwrap();
Expand Down
45 changes: 27 additions & 18 deletions src/parser/src/parse_demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,34 +41,43 @@ pub struct DemoOutput {

pub struct Parser<'a> {
input: ParserInputs<'a>,
pub force_singlethread: bool,
pub parsing_mode: ParsingMode,
}
#[derive(PartialEq)]
pub enum ParsingMode {
ForceSingleThreaded,
ForceMultiThreaded,
Normal,
}

impl<'a> Parser<'a> {
pub fn new(input: ParserInputs<'a>, force_singlethread: bool) -> Self {
pub fn new(input: ParserInputs<'a>, parsing_mode: ParsingMode) -> Self {
Parser {
input: input,
force_singlethread: force_singlethread,
parsing_mode: parsing_mode,
}
}

pub fn parse_demo(&mut self, demo_bytes: &[u8]) -> Result<DemoOutput, DemoParserError> {
// Single threaded second pass
if !check_multithreadability(&self.input.wanted_player_props) || self.force_singlethread {
let mut first_pass_parser = FirstPassParser::new(&self.input);
let first_pass_output = first_pass_parser.parse_demo(&demo_bytes, false)?;
return self.second_pass_single_threaded(demo_bytes, first_pass_output);
}
// Multi threaded second pass
let (sender, receiver) = channel();
let mut fp = FrameParser::new();
return thread::scope(|s| {
let _handle = s.spawn(|| fp.par_start(demo_bytes, sender));
let mut first_pass_parser = FirstPassParser::new(&self.input);
let first_pass_output = first_pass_parser.parse_demo(&demo_bytes, true).unwrap();
let out = self.second_pass_threaded_with_channels(demo_bytes, first_pass_output, receiver);
out
});
if self.parsing_mode == ParsingMode::ForceMultiThreaded
|| check_multithreadability(&self.input.wanted_player_props)
&& !(self.parsing_mode == ParsingMode::ForceSingleThreaded)
{
let (sender, receiver) = channel();
let mut fp = FrameParser::new();
return thread::scope(|s| {
let _handle = s.spawn(|| fp.par_start(demo_bytes, sender));
let mut first_pass_parser = FirstPassParser::new(&self.input);
let first_pass_output = first_pass_parser.parse_demo(&demo_bytes, true).unwrap();
let out = self.second_pass_threaded_with_channels(demo_bytes, first_pass_output, receiver);
out
});
}
// Single threaded second pass
let mut first_pass_parser = FirstPassParser::new(&self.input);
let first_pass_output = first_pass_parser.parse_demo(&demo_bytes, false)?;
return self.second_pass_single_threaded(demo_bytes, first_pass_output);
}

fn second_pass_single_threaded(
Expand Down
1 change: 0 additions & 1 deletion src/parser/src/second_pass/collect_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use crate::maps::PAINTKITS;
use crate::maps::PLAYER_COLOR;
use crate::maps::STICKER_ID_TO_NAME;
use crate::maps::WEAPINDICIES;
use crate::second_pass::entities::Entity;
use crate::second_pass::entities::EntityType;
use crate::second_pass::parser_settings::SecondPassParser;
use crate::second_pass::variants::PropColumn;
Expand Down
20 changes: 10 additions & 10 deletions src/python/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ impl DemoParser {
huffman_lookup_table: &self.huf,
order_by_steamid: false,
};
let mut parser = Parser::new(settings, false);
let mut parser = Parser::new(settings, parser::parse_demo::ParsingMode::Normal);
let output = match parser.parse_demo(&self.mmap) {
Ok(output) => output,
Err(e) => return Err(Exception::new_err(format!("{e}"))),
Expand All @@ -97,7 +97,7 @@ impl DemoParser {
huffman_lookup_table: &self.huf,
order_by_steamid: false,
};
let mut parser = Parser::new(settings, false);
let mut parser = Parser::new(settings, parser::parse_demo::ParsingMode::Normal);
let output = match parser.parse_demo(&self.mmap) {
Ok(output) => output,
Err(e) => return Err(Exception::new_err(format!("{e}"))),
Expand Down Expand Up @@ -131,7 +131,7 @@ impl DemoParser {
huffman_lookup_table: &self.huf,
order_by_steamid: false,
};
let mut parser = Parser::new(settings, false);
let mut parser = Parser::new(settings, parser::parse_demo::ParsingMode::Normal);
let output = match parser.parse_demo(&self.mmap) {
Ok(output) => output,
Err(e) => return Err(Exception::new_err(format!("{e}"))),
Expand Down Expand Up @@ -201,7 +201,7 @@ impl DemoParser {
huffman_lookup_table: &self.huf,
order_by_steamid: false,
};
let mut parser = Parser::new(settings, false);
let mut parser = Parser::new(settings, parser::parse_demo::ParsingMode::Normal);
let output = match parser.parse_demo(&self.mmap) {
Ok(output) => output,
Err(e) => return Err(Exception::new_err(format!("{e}"))),
Expand Down Expand Up @@ -245,7 +245,7 @@ impl DemoParser {
huffman_lookup_table: &self.huf,
order_by_steamid: false,
};
let mut parser = Parser::new(settings, false);
let mut parser = Parser::new(settings, parser::parse_demo::ParsingMode::Normal);
let output = match parser.parse_demo(&self.mmap) {
Ok(output) => output,
Err(e) => return Err(Exception::new_err(format!("{e}"))),
Expand Down Expand Up @@ -325,7 +325,7 @@ impl DemoParser {
huffman_lookup_table: &self.huf,
order_by_steamid: false,
};
let mut parser = Parser::new(settings, false);
let mut parser = Parser::new(settings, parser::parse_demo::ParsingMode::Normal);
let output = match parser.parse_demo(&self.mmap) {
Ok(output) => output,
Err(e) => return Err(Exception::new_err(format!("{e}"))),
Expand Down Expand Up @@ -423,7 +423,7 @@ impl DemoParser {
huffman_lookup_table: &self.huf,
order_by_steamid: false,
};
let mut parser = Parser::new(settings, false);
let mut parser = Parser::new(settings, parser::parse_demo::ParsingMode::Normal);
let output = match parser.parse_demo(&self.mmap) {
Ok(output) => output,
Err(e) => return Err(Exception::new_err(format!("{e}"))),
Expand Down Expand Up @@ -479,7 +479,7 @@ impl DemoParser {
huffman_lookup_table: &self.huf,
order_by_steamid: false,
};
let mut parser = Parser::new(settings, false);
let mut parser = Parser::new(settings, parser::parse_demo::ParsingMode::Normal);
let output = match parser.parse_demo(&self.mmap) {
Ok(output) => output,
Err(e) => return Err(Exception::new_err(format!("{e}"))),
Expand Down Expand Up @@ -507,7 +507,7 @@ impl DemoParser {
huffman_lookup_table: &vec![],
order_by_steamid: false,
};
let mut parser = Parser::new(settings, false);
let mut parser = Parser::new(settings, parser::parse_demo::ParsingMode::Normal);
let output = match parser.parse_demo(&self.mmap) {
Ok(output) => output,
Err(e) => return Err(PyValueError::new_err(format!("{e}"))),
Expand Down Expand Up @@ -558,7 +558,7 @@ impl DemoParser {
huffman_lookup_table: &arc_huf,
order_by_steamid: false,
};
let mut parser = Parser::new(settings, false);
let mut parser = Parser::new(settings, parser::parse_demo::ParsingMode::Normal);
let output = match parser.parse_demo(&self.mmap) {
Ok(output) => output,
Err(e) => return Err(Exception::new_err(format!("{e}"))),
Expand Down
13 changes: 7 additions & 6 deletions src/wasm/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use parser::first_pass::parser_settings::rm_user_friendly_names;
use parser::first_pass::parser_settings::ParserInputs;
use parser::parse_demo::Parser;
use parser::parse_demo::ParsingMode::ForceSingleThreaded;
use parser::second_pass::parser_settings::create_huffman_lookup_table;
use parser::second_pass::variants::soa_to_aos;
use parser::second_pass::variants::OutputSerdeHelperStruct;
Expand Down Expand Up @@ -57,7 +58,7 @@ pub fn parseEvent(
huffman_lookup_table: &arc_huf,
order_by_steamid: false,
};
let mut parser = Parser::new(settings, false);
let mut parser = Parser::new(settings, ForceSingleThreaded);

let output = match parser.parse_demo(&file) {
Ok(output) => output,
Expand Down Expand Up @@ -119,7 +120,7 @@ pub fn parseEvents(
huffman_lookup_table: &arc_huf,
order_by_steamid: false,
};
let mut parser = Parser::new(settings, false);
let mut parser = Parser::new(settings, ForceSingleThreaded);

let output = match parser.parse_demo(&file) {
Ok(output) => output,
Expand Down Expand Up @@ -149,7 +150,7 @@ pub fn listGameEvents(fileBytes: Vec<u8>) -> Result<JsValue, JsError> {
huffman_lookup_table: &arc_huf.clone(),
order_by_steamid: false,
};
let mut parser = Parser::new(settings, false);
let mut parser = Parser::new(settings, ForceSingleThreaded);

let output = match parser.parse_demo(&fileBytes) {
Ok(output) => output,
Expand Down Expand Up @@ -209,7 +210,7 @@ pub fn parseTicks(
huffman_lookup_table: &arc_huf.clone(),
order_by_steamid: false,
};
let mut parser = Parser::new(settings, false);
let mut parser = Parser::new(settings, ForceSingleThreaded);

let output = match parser.parse_demo(&file) {
Ok(output) => output,
Expand Down Expand Up @@ -263,7 +264,7 @@ pub fn parseGrenades(file: Vec<u8>) -> Result<JsValue, JsError> {
huffman_lookup_table: &arc_huf.clone(),
order_by_steamid: false,
};
let mut parser = Parser::new(settings, false);
let mut parser = Parser::new(settings, ForceSingleThreaded);

let output = match parser.parse_demo(&file) {
Ok(output) => output,
Expand Down Expand Up @@ -295,7 +296,7 @@ pub fn parseHeader(file: Vec<u8>) -> Result<JsValue, JsError> {
huffman_lookup_table: &arc_huf.clone(),
order_by_steamid: false,
};
let mut parser = Parser::new(settings, false);
let mut parser = Parser::new(settings, ForceSingleThreaded);
let output = match parser.parse_demo(&file) {
Ok(output) => output,
Err(e) => return Err(JsError::new(&format!("{}", e))),
Expand Down

0 comments on commit f25621f

Please sign in to comment.