Optimized DocumentElem (#2777)

This commit is contained in:
Sébastien d'Herbais de Thun 2023-11-27 12:13:46 +01:00 committed by GitHub
parent 34862b7b27
commit c1ed55f555
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 6 deletions

View File

@ -724,7 +724,7 @@ impl<T> StyleVec<T> {
} }
impl<'a> StyleVec<Cow<'a, Content>> { impl<'a> StyleVec<Cow<'a, Content>> {
pub fn to_vec(self) -> Vec<Prehashed<Content>> { pub fn to_vec<F: From<Content>>(self) -> Vec<F> {
self.items self.items
.into_iter() .into_iter()
.zip( .zip(
@ -733,7 +733,7 @@ impl<'a> StyleVec<Cow<'a, Content>> {
.flat_map(|(map, count)| iter::repeat(map).take(*count)), .flat_map(|(map, count)| iter::repeat(map).take(*count)),
) )
.map(|(content, styles)| content.into_owned().styled_with_map(styles.clone())) .map(|(content, styles)| content.into_owned().styled_with_map(styles.clone()))
.map(Prehashed::new) .map(F::from)
.collect() .collect()
} }
} }

View File

@ -1,4 +1,3 @@
use comemo::Prehashed;
use ecow::EcoString; use ecow::EcoString;
use crate::diag::{bail, SourceResult, StrResult}; use crate::diag::{bail, SourceResult, StrResult};
@ -32,12 +31,15 @@ pub struct DocumentElem {
/// ///
/// While this can be arbitrary content, PDF viewers only support plain text /// While this can be arbitrary content, PDF viewers only support plain text
/// titles, so the conversion might be lossy. /// titles, so the conversion might be lossy.
#[ghost]
pub title: Option<Content>, pub title: Option<Content>,
/// The document's authors. /// The document's authors.
#[ghost]
pub author: Author, pub author: Author,
/// The document's keywords. /// The document's keywords.
#[ghost]
pub keywords: Keywords, pub keywords: Keywords,
/// The document's creation date. /// The document's creation date.
@ -48,11 +50,12 @@ pub struct DocumentElem {
/// ///
/// The year component must be at least zero in order to be embedded into a /// The year component must be at least zero in order to be embedded into a
/// PDF. /// PDF.
#[ghost]
pub date: Smart<Option<Datetime>>, pub date: Smart<Option<Datetime>>,
/// The page runs. /// The page runs.
#[variadic] #[variadic]
pub children: Vec<Prehashed<Content>>, pub children: Vec<Content>,
} }
impl Construct for DocumentElem { impl Construct for DocumentElem {
@ -71,11 +74,11 @@ impl LayoutRoot for DocumentElem {
) -> SourceResult<Document> { ) -> SourceResult<Document> {
tracing::info!("Document layout"); tracing::info!("Document layout");
let mut pages = vec![]; let mut pages = Vec::with_capacity(self.children().len());
let mut page_counter = ManualPageCounter::new(); let mut page_counter = ManualPageCounter::new();
let children = self.children(); let children = self.children();
let mut iter = children.iter().map(|c| &**c).peekable(); let mut iter = children.iter().peekable();
while let Some(mut child) = iter.next() { while let Some(mut child) = iter.next() {
let outer = styles; let outer = styles;