summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2022-05-25 21:31:12 +0200
committerLaurenz <laurmaedje@gmail.com>2022-05-25 21:56:22 +0200
commit0170913d5413aab4c1f2bd7d56b9b7138f676012 (patch)
treefbe77af9ac3c3cfa63ffde3d861987e8d1276ac5
parentb6b6e3692404e32873892810bebeb1122f34c52e (diff)
Rebrand queries as memoization
-rw-r--r--macros/src/lib.rs1
-rw-r--r--src/eval/mod.rs1
-rw-r--r--src/export/render.rs3
-rw-r--r--src/lib.rs2
-rw-r--r--src/memo.rs (renamed from src/query.rs)34
-rw-r--r--src/model/layout.rs3
6 files changed, 23 insertions, 21 deletions
diff --git a/macros/src/lib.rs b/macros/src/lib.rs
index 49623834..029a1adb 100644
--- a/macros/src/lib.rs
+++ b/macros/src/lib.rs
@@ -8,6 +8,7 @@ use syn::punctuated::Punctuated;
use syn::spanned::Spanned;
use syn::{Error, Ident, Result};
+/// Turn a struct into a node / a function with settable properties.
#[proc_macro_attribute]
pub fn node(stream: TokenStream, item: TokenStream) -> TokenStream {
let impl_block = syn::parse_macro_input!(item as syn::ItemImpl);
diff --git a/src/eval/mod.rs b/src/eval/mod.rs
index e403c3cb..e54dfce4 100644
--- a/src/eval/mod.rs
+++ b/src/eval/mod.rs
@@ -26,6 +26,7 @@ pub use func::*;
pub use machine::*;
pub use raw::*;
pub use scope::*;
+pub use typst_macros::node;
pub use value::*;
use std::collections::BTreeMap;
diff --git a/src/export/render.rs b/src/export/render.rs
index 9c37791e..aa60e67e 100644
--- a/src/export/render.rs
+++ b/src/export/render.rs
@@ -12,7 +12,6 @@ use crate::geom::{
self, Geometry, Length, Paint, PathElement, Shape, Size, Stroke, Transform,
};
use crate::image::{Image, RasterImage, Svg};
-use crate::query::query_ref;
use crate::Context;
/// Export a frame into a rendered image.
@@ -244,7 +243,7 @@ fn render_outline_glyph(
// Rasterize the glyph with `pixglyph`.
// Try to retrieve a prepared glyph or prepare it from scratch if it
// doesn't exist, yet.
- let bitmap = query_ref(
+ let bitmap = crate::memo::memoized_ref(
(&ctx.fonts, text.face_id, id),
|(fonts, face_id, id)| pixglyph::Glyph::load(fonts.get(face_id).ttf(), id),
|glyph| glyph.as_ref().map(|g| g.rasterize(ts.tx, ts.ty, ppem)),
diff --git a/src/lib.rs b/src/lib.rs
index 7d848c53..17225b32 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -45,9 +45,9 @@ pub mod frame;
pub mod image;
pub mod library;
pub mod loading;
+pub mod memo;
pub mod model;
pub mod parse;
-pub mod query;
pub mod source;
pub mod syntax;
diff --git a/src/query.rs b/src/memo.rs
index 97af0139..6545ebcc 100644
--- a/src/query.rs
+++ b/src/memo.rs
@@ -1,4 +1,4 @@
-//! Query caching.
+//! Function memoization.
use std::any::Any;
use std::cell::RefCell;
@@ -7,7 +7,7 @@ use std::fmt::{self, Display, Formatter};
use std::hash::Hash;
thread_local! {
- /// The thread-local query cache.
+ /// The thread-local cache.
static CACHE: RefCell<Cache> = RefCell::default();
}
@@ -22,37 +22,39 @@ where
CACHE.with(|cell| f(&mut cell.borrow_mut()))
}
-/// An entry in the query cache.
+/// An entry in the cache.
struct CacheEntry {
- /// The query's results.
+ /// The memoized function's result.
data: Box<dyn Any>,
/// How many evictions have passed since the entry has been last used.
age: usize,
}
-/// Execute a query.
+/// Execute a memoized function call.
///
-/// This hashes all inputs to the query and then either returns a cached version
-/// from the thread-local query cache or executes the query and saves a copy of
-/// the results in the cache.
+/// This hashes all inputs to the function and then either returns a cached
+/// version from the thread-local cache or executes the function and saves a
+/// copy of the results in the cache.
///
/// Note that `f` must be a pure function.
-pub fn query<I, O>(input: I, f: fn(input: I) -> O) -> O
+pub fn memoized<I, O>(input: I, f: fn(input: I) -> O) -> O
where
I: Hash,
O: Clone + 'static,
{
- query_ref(input, f, Clone::clone)
+ memoized_ref(input, f, Clone::clone)
}
-/// Execute a query and then call a function with a reference to the result.
+/// Execute a function and then call another function with a reference to the
+/// result.
///
-/// This hashes all inputs to the query and then either call `g` with a cached
-/// version from the thread-local query cache or executes the query, calls `g`
-/// with the fresh version and saves the result in the cache.
+/// This hashes all inputs to the function and then either
+/// - calls `g` with a cached version from the thread-local cache,
+/// - or executes `f`, calls `g` with the fresh version and saves the result in
+/// the cache.
///
/// Note that `f` must be a pure function, while `g` does not need to be pure.
-pub fn query_ref<I, O, G, R>(input: I, f: fn(input: I) -> O, g: G) -> R
+pub fn memoized_ref<I, O, G, R>(input: I, f: fn(input: I) -> O, g: G) -> R
where
I: Hash,
O: 'static,
@@ -74,7 +76,7 @@ where
})
}
-/// Garbage-collect the thread-local query cache.
+/// Garbage-collect the thread-local cache.
///
/// This deletes elements which haven't been used in a while and returns details
/// about the eviction.
diff --git a/src/model/layout.rs b/src/model/layout.rs
index 1933fca1..6dfbcb90 100644
--- a/src/model/layout.rs
+++ b/src/model/layout.rs
@@ -14,7 +14,6 @@ use crate::geom::{
};
use crate::library::graphics::MoveNode;
use crate::library::layout::{AlignNode, PadNode};
-use crate::query::query;
use crate::util::Prehashed;
use crate::Context;
@@ -222,7 +221,7 @@ impl Layout for LayoutNode {
regions: &Regions,
styles: StyleChain,
) -> TypResult<Vec<Arc<Frame>>> {
- query(
+ crate::memo::memoized(
(self, ctx, regions, styles),
|(node, ctx, regions, styles)| {
let entry = StyleEntry::Barrier(Barrier::new(node.id()));