Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

security(affinity): index object fields for O(1) lookup

Dylan M. Taylor5adebc5d6df4
diff --git a/crates/affinity/src/stream.rs b/crates/affinity/src/stream.rs
index a755747c4..2ffe12b7e 100644
--- a/crates/affinity/src/stream.rs
+++ b/crates/affinity/src/stream.rs
@@ -73,11 +73,40 @@ pub struct Object {
pub chain: Vec<Tag>,
/// Fields in stream order; positional fields have tag 0.
pub fields: Vec<(Tag, Value)>,
+ /// First-occurrence index of each non-positional tag, so `get` is O(1). A shared object
+ /// can be referenced by many sibling layers, and each reference used to rescan all of its
+ /// fields; indexing once keeps repeated lookups from multiplying into quadratic work.
+ index: std::collections::HashMap<Tag, usize>,
}
impl Object {
+ pub fn new(class: Tag, kind: Kind, chain: Vec<Tag>, fields: Vec<(Tag, Value)>) -> Self {
+ let mut index = std::collections::HashMap::with_capacity(fields.len());
+ for (i, (t, _)) in fields.iter().enumerate() {
+ // Positional fields share tag 0 and are not looked up by tag; keep first-match
+ // semantics for real tags.
+ if t.0 != 0 {
+ index.entry(*t).or_insert(i);
+ }
+ }
+ Object { class, kind, chain, fields, index }
+ }
+
pub fn get(&self, tag: Tag) -> Option<&Value> {
- self.fields.iter().find(|(t, _)| *t == tag).map(|(_, v)| v)
+ self.index.get(&tag).map(|&i| &self.fields[i].1)
+ }
+
+ /// Replaces the fields (the root object is filled in after it is pushed) and rebuilds the
+ /// tag index so `get` stays correct.
+ fn set_fields(&mut self, fields: Vec<(Tag, Value)>) {
+ self.index.clear();
+ self.index.reserve(fields.len());
+ for (i, (t, _)) in fields.iter().enumerate() {
+ if t.0 != 0 {
+ self.index.entry(*t).or_insert(i);
+ }
+ }
+ self.fields = fields;
}
}
@@ -124,10 +153,10 @@ pub fn parse(bytes: &[u8]) -> Result<Stream, Error> {
c.u32()?;
}
let mut p = Parser { c, objects: Vec::new(), shared: std::collections::HashMap::new(), depth: 0, values: 0 };
- let root = p.push(Object { class, kind: Kind::Root, chain: vec![class], fields: Vec::new() })?;
+ let root = p.push(Object::new(class, Kind::Root, vec![class], Vec::new()))?;
let fields = p.fields(true, class)?;
if let Some(o) = p.objects.get_mut(root) {
- o.fields = fields;
+ o.set_fields(fields);
}
Ok(Stream { root, objects: p.objects })
}
@@ -252,7 +281,7 @@ impl Parser<'_> {
}
0x30 => {
let fields = self.fields(false, class)?;
- let id = self.push(Object { class, kind: Kind::Positional, chain: Vec::new(), fields })?;
+ let id = self.push(Object::new(class, Kind::Positional, Vec::new(), fields))?;
Value::Obj(Some(id))
}
0x31 | 0x32 if !tagged => return Err(Error::Malformed("object without a field tag")),
@@ -350,7 +379,7 @@ impl Parser<'_> {
}
};
let fields = self.fields(true, class)?;
- let id = self.push(Object { class, kind: Kind::Inline, chain: vec![class], fields })?;
+ let id = self.push(Object::new(class, Kind::Inline, vec![class], fields))?;
Ok(Value::Obj(Some(id)))
}
@@ -369,7 +398,7 @@ impl Parser<'_> {
return Err(Error::Malformed("object defined twice"));
}
// Register before the fields: an object's own fields may refer back to it.
- let id = self.push(Object { class: Tag(0), kind: Kind::Shared, chain: Vec::new(), fields: Vec::new() })?;
+ let id = self.push(Object::new(Tag(0), Kind::Shared, Vec::new(), Vec::new()))?;
self.shared.insert(key, id);
let mut chain = Vec::new();
let mut fields = Vec::new();
@@ -395,7 +424,7 @@ impl Parser<'_> {
let class = chain.first().copied().unwrap_or(Tag(0));
fields.extend(self.fields(true, class)?);
if let Some(o) = self.objects.get_mut(id) {
- *o = Object { class, kind: Kind::Shared, chain, fields };
+ *o = Object::new(class, Kind::Shared, chain, fields);
}
Ok(Value::Obj(Some(id)))
}