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(
|
fn parse_string_table_update(
|
||||||
stream: &mut Stream,
|
stream: &mut Stream,
|
||||||
table_meta: &StringTableMeta,
|
table_meta: &StringTableMeta,
|
||||||
entry_count: u16,
|
entry_count: u16,
|
||||||
) -> ReadResult<Vec<(u16, StringTableEntry)>> {
|
) -> ReadResult<Vec<(u16, StringTableEntry)>> {
|
||||||
let entry_bits = log_base2(table_meta.max_entries);
|
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 last_entry: i16 = -1;
|
||||||
let mut history: Vec<Option<String>> = Vec::new();
|
|
||||||
|
|
||||||
for _ in 0..entry_count {
|
for _ in 0..entry_count {
|
||||||
let index = if stream.read()? {
|
let index = if stream.read()? {
|
||||||
|
|
@ -159,58 +192,38 @@ fn parse_string_table_update(
|
||||||
|
|
||||||
last_entry = index as i16;
|
last_entry = index as i16;
|
||||||
|
|
||||||
let entry = read_table_entry(stream, table_meta, &history)?;
|
let entry = read_table_entry(stream, table_meta, &entries)?;
|
||||||
// optimize: any way to get rid of the clone here?
|
|
||||||
// `entries` always outlives `history` without reallocation
|
|
||||||
let text = entry.text.clone();
|
|
||||||
entries.push((index, entry));
|
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(
|
fn parse_string_table_list(
|
||||||
stream: &mut Stream,
|
stream: &mut Stream,
|
||||||
table_meta: &StringTableMeta,
|
table_meta: &StringTableMeta,
|
||||||
entry_count: u16,
|
entry_count: u16,
|
||||||
) -> Result<Vec<StringTableEntry>> {
|
) -> Result<Vec<(u16, StringTableEntry)>> {
|
||||||
let mut entries = Vec::with_capacity(entry_count as usize);
|
let mut entries = TableEntries::new(entry_count as usize);
|
||||||
|
|
||||||
let mut history: Vec<Option<String>> = Vec::new();
|
for index in 0..entry_count {
|
||||||
|
|
||||||
for _ in 0..entry_count {
|
|
||||||
if !stream.read::<bool>()? {
|
if !stream.read::<bool>()? {
|
||||||
return Err(ParseError::InvalidDemo(
|
return Err(ParseError::InvalidDemo(
|
||||||
"there should be no holes when reading CreateStringTable message",
|
"there should be no holes when reading CreateStringTable message",
|
||||||
));
|
));
|
||||||
};
|
};
|
||||||
|
|
||||||
let entry = read_table_entry(stream, table_meta, &history)?;
|
let entry = read_table_entry(stream, table_meta, &entries)?;
|
||||||
// optimize: any way to get rid of the clone here?
|
entries.push((index, entry));
|
||||||
// `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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(entries)
|
Ok(entries.into_entries())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_table_entry(
|
fn read_table_entry(
|
||||||
stream: &mut Stream,
|
stream: &mut Stream,
|
||||||
table_meta: &StringTableMeta,
|
table_meta: &StringTableMeta,
|
||||||
history: &Vec<Option<String>>,
|
history: &TableEntries,
|
||||||
) -> ReadResult<StringTableEntry> {
|
) -> ReadResult<StringTableEntry> {
|
||||||
let text = if stream.read()? {
|
let text = if stream.read()? {
|
||||||
// set value
|
// set value
|
||||||
|
|
@ -220,15 +233,20 @@ fn read_table_entry(
|
||||||
let bytes_to_copy: u32 = stream.read_sized(5)?;
|
let bytes_to_copy: u32 = stream.read_sized(5)?;
|
||||||
let rest_of_string: String = stream.read()?;
|
let rest_of_string: String = stream.read()?;
|
||||||
|
|
||||||
Some(match history.get(index as usize).and_then(|h| h.as_ref()) {
|
Some(
|
||||||
Some(text) => String::from_utf8({
|
match history
|
||||||
text.bytes()
|
.get_history(index as usize)
|
||||||
.take(bytes_to_copy as usize)
|
.and_then(|entry| entry.text.as_ref())
|
||||||
.chain(rest_of_string.bytes())
|
{
|
||||||
.collect()
|
Some(text) => String::from_utf8({
|
||||||
})?,
|
text.bytes()
|
||||||
None => rest_of_string, // best guess, happens in some pov demos but only for unimportant tables it seems
|
.take(bytes_to_copy as usize)
|
||||||
})
|
.chain(rest_of_string.bytes())
|
||||||
|
.collect()
|
||||||
|
})?,
|
||||||
|
None => rest_of_string, // best guess, happens in some pov demos but only for unimportant tables it seems
|
||||||
|
},
|
||||||
|
)
|
||||||
} else {
|
} else {
|
||||||
Some(stream.read()?)
|
Some(stream.read()?)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@ pub struct FixedUserdataSize {
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct StringTable {
|
pub struct StringTable {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
pub entries: Vec<StringTableEntry>,
|
pub entries: Vec<(u16, StringTableEntry)>,
|
||||||
pub max_entries: u16,
|
pub max_entries: u16,
|
||||||
pub fixed_userdata_size: Option<FixedUserdataSize>,
|
pub fixed_userdata_size: Option<FixedUserdataSize>,
|
||||||
pub client_entries: Option<Vec<StringTableEntry>>,
|
pub client_entries: Option<Vec<StringTableEntry>>,
|
||||||
|
|
@ -36,7 +36,11 @@ impl BitRead<LittleEndian> for StringTable {
|
||||||
fn read(stream: &mut Stream) -> ReadResult<Self> {
|
fn read(stream: &mut Stream) -> ReadResult<Self> {
|
||||||
let name = stream.read()?;
|
let name = stream.read()?;
|
||||||
let entry_count = stream.read_int(16)?;
|
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 client_entries = if stream.read_bool()? {
|
||||||
let count = stream.read_int(16)?;
|
let count = stream.read_int(16)?;
|
||||||
|
|
|
||||||
|
|
@ -75,7 +75,8 @@ impl<T: MessageHandler> DemoHandler<T> {
|
||||||
|
|
||||||
self.state_handler
|
self.state_handler
|
||||||
.handle_string_table_meta(table.get_table_meta());
|
.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
|
self.state_handler
|
||||||
.handle_string_entry(&table.name, entry_index, &entry);
|
.handle_string_entry(&table.name, entry_index, &entry);
|
||||||
self.analyser
|
self.analyser
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue