fxprof_processed_profile/
thread_string_table.rs

1use serde::ser::{Serialize, Serializer};
2
3use crate::fast_hash_map::FastHashMap;
4use crate::string_table::{GlobalStringIndex, GlobalStringTable, StringIndex, StringTable};
5
6#[derive(Debug, Clone, Copy, PartialOrd, Ord, PartialEq, Eq, Hash)]
7pub struct ThreadInternalStringIndex(pub StringIndex);
8
9impl Serialize for ThreadInternalStringIndex {
10    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
11        self.0.serialize(serializer)
12    }
13}
14
15#[derive(Debug, Clone, Default)]
16pub struct ThreadStringTable {
17    table: StringTable,
18    global_to_local_string: FastHashMap<GlobalStringIndex, ThreadInternalStringIndex>,
19}
20
21impl ThreadStringTable {
22    pub fn new() -> Self {
23        Default::default()
24    }
25
26    pub fn index_for_string(&mut self, s: &str) -> ThreadInternalStringIndex {
27        ThreadInternalStringIndex(self.table.index_for_string(s))
28    }
29
30    pub fn index_for_global_string(
31        &mut self,
32        global_index: GlobalStringIndex,
33        global_table: &GlobalStringTable,
34    ) -> ThreadInternalStringIndex {
35        let table = &mut self.table;
36        *self
37            .global_to_local_string
38            .entry(global_index)
39            .or_insert_with(|| {
40                let s = global_table.get_string(global_index).unwrap();
41                ThreadInternalStringIndex(table.index_for_string(s))
42            })
43    }
44}
45
46impl Serialize for ThreadStringTable {
47    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
48        self.table.serialize(serializer)
49    }
50}