mirror of
https://codeberg.org/demostf/parser.git
synced 2026-06-03 18:24:05 +02:00
remove the need to clone text entries
This commit is contained in:
parent
65d55c9a26
commit
1b9befd165
3 changed files with 66 additions and 43 deletions
|
|
@ -139,16 +139,49 @@ impl ParseBitSkip for UpdateStringTableMessage {
|
|||
}
|
||||
}
|
||||
|
||||
struct TableEntries {
|
||||
entries: Vec<(u16, StringTableEntry)>,
|
||||
history: Vec<u16>,
|
||||
}
|
||||
|
||||
impl TableEntries {
|
||||
pub fn new(count: usize) -> Self {
|
||||
TableEntries {
|
||||
entries: Vec::with_capacity(count),
|
||||
history: Vec::with_capacity(32),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn push(&mut self, entry: (u16, StringTableEntry)) {
|
||||
if self.history.len() > 31 {
|
||||
self.history.remove(0);
|
||||
}
|
||||
let entry_index = self.entries.len();
|
||||
self.entries.push(entry);
|
||||
self.history.push(entry_index as u16);
|
||||
}
|
||||
|
||||
pub fn get_history(&self, index: usize) -> Option<&StringTableEntry> {
|
||||
self.history
|
||||
.get(index)
|
||||
.and_then(|entry_index| self.entries.get(*entry_index as usize))
|
||||
.map(|entry| &entry.1)
|
||||
}
|
||||
|
||||
pub fn into_entries(self) -> Vec<(u16, StringTableEntry)> {
|
||||
self.entries
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_string_table_update(
|
||||
stream: &mut Stream,
|
||||
table_meta: &StringTableMeta,
|
||||
entry_count: u16,
|
||||
) -> ReadResult<Vec<(u16, StringTableEntry)>> {
|
||||
let entry_bits = log_base2(table_meta.max_entries);
|
||||
let mut entries = Vec::with_capacity(entry_count as usize);
|
||||
let mut entries = TableEntries::new(entry_count as usize);
|
||||
|
||||
let mut last_entry: i16 = -1;
|
||||
let mut history: Vec<Option<String>> = Vec::new();
|
||||
|
||||
for _ in 0..entry_count {
|
||||
let index = if stream.read()? {
|
||||
|
|
@ -159,58 +192,38 @@ fn parse_string_table_update(
|
|||
|
||||
last_entry = index as i16;
|
||||
|
||||
let entry = read_table_entry(stream, table_meta, &history)?;
|
||||
// optimize: any way to get rid of the clone here?
|
||||
// `entries` always outlives `history` without reallocation
|
||||
let text = entry.text.clone();
|
||||
let entry = read_table_entry(stream, table_meta, &entries)?;
|
||||
entries.push((index, entry));
|
||||
// not 100% sure we should be pushing front here, and not appending
|
||||
history.push(text);
|
||||
|
||||
if history.len() > 32 {
|
||||
history.remove(0);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(entries)
|
||||
Ok(entries.into_entries())
|
||||
}
|
||||
|
||||
fn parse_string_table_list(
|
||||
stream: &mut Stream,
|
||||
table_meta: &StringTableMeta,
|
||||
entry_count: u16,
|
||||
) -> Result<Vec<StringTableEntry>> {
|
||||
let mut entries = Vec::with_capacity(entry_count as usize);
|
||||
) -> Result<Vec<(u16, StringTableEntry)>> {
|
||||
let mut entries = TableEntries::new(entry_count as usize);
|
||||
|
||||
let mut history: Vec<Option<String>> = Vec::new();
|
||||
|
||||
for _ in 0..entry_count {
|
||||
for index in 0..entry_count {
|
||||
if !stream.read::<bool>()? {
|
||||
return Err(ParseError::InvalidDemo(
|
||||
"there should be no holes when reading CreateStringTable message",
|
||||
));
|
||||
};
|
||||
|
||||
let entry = read_table_entry(stream, table_meta, &history)?;
|
||||
// optimize: any way to get rid of the clone here?
|
||||
// `entries` always outlives `history` without reallocation
|
||||
let text = entry.text.clone();
|
||||
entries.push(entry);
|
||||
// not 100% sure we should be pushing front here, and not appending
|
||||
history.push(text);
|
||||
|
||||
if history.len() > 32 {
|
||||
history.remove(0);
|
||||
}
|
||||
let entry = read_table_entry(stream, table_meta, &entries)?;
|
||||
entries.push((index, entry));
|
||||
}
|
||||
|
||||
Ok(entries)
|
||||
Ok(entries.into_entries())
|
||||
}
|
||||
|
||||
fn read_table_entry(
|
||||
stream: &mut Stream,
|
||||
table_meta: &StringTableMeta,
|
||||
history: &Vec<Option<String>>,
|
||||
history: &TableEntries,
|
||||
) -> ReadResult<StringTableEntry> {
|
||||
let text = if stream.read()? {
|
||||
// set value
|
||||
|
|
@ -220,7 +233,11 @@ fn read_table_entry(
|
|||
let bytes_to_copy: u32 = stream.read_sized(5)?;
|
||||
let rest_of_string: String = stream.read()?;
|
||||
|
||||
Some(match history.get(index as usize).and_then(|h| h.as_ref()) {
|
||||
Some(
|
||||
match history
|
||||
.get_history(index as usize)
|
||||
.and_then(|entry| entry.text.as_ref())
|
||||
{
|
||||
Some(text) => String::from_utf8({
|
||||
text.bytes()
|
||||
.take(bytes_to_copy as usize)
|
||||
|
|
@ -228,7 +245,8 @@ fn read_table_entry(
|
|||
.collect()
|
||||
})?,
|
||||
None => rest_of_string, // best guess, happens in some pov demos but only for unimportant tables it seems
|
||||
})
|
||||
},
|
||||
)
|
||||
} else {
|
||||
Some(stream.read()?)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@ pub struct FixedUserdataSize {
|
|||
#[derive(Debug)]
|
||||
pub struct StringTable {
|
||||
pub name: String,
|
||||
pub entries: Vec<StringTableEntry>,
|
||||
pub entries: Vec<(u16, StringTableEntry)>,
|
||||
pub max_entries: u16,
|
||||
pub fixed_userdata_size: Option<FixedUserdataSize>,
|
||||
pub client_entries: Option<Vec<StringTableEntry>>,
|
||||
|
|
@ -36,7 +36,11 @@ impl BitRead<LittleEndian> for StringTable {
|
|||
fn read(stream: &mut Stream) -> ReadResult<Self> {
|
||||
let name = stream.read()?;
|
||||
let entry_count = stream.read_int(16)?;
|
||||
let entries = stream.read_sized(entry_count as usize)?;
|
||||
let mut entries = Vec::with_capacity(entry_count as usize);
|
||||
|
||||
for index in 0..entry_count {
|
||||
entries.push((index, stream.read()?))
|
||||
}
|
||||
|
||||
let client_entries = if stream.read_bool()? {
|
||||
let count = stream.read_int(16)?;
|
||||
|
|
|
|||
|
|
@ -75,7 +75,8 @@ impl<T: MessageHandler> DemoHandler<T> {
|
|||
|
||||
self.state_handler
|
||||
.handle_string_table_meta(table.get_table_meta());
|
||||
for (entry_index, entry) in table.entries.into_iter().enumerate() {
|
||||
for (entry_index, entry) in table.entries.into_iter() {
|
||||
let entry_index = entry_index as usize;
|
||||
self.state_handler
|
||||
.handle_string_entry(&table.name, entry_index, &entry);
|
||||
self.analyser
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue