diff --git a/src/hb-face.hh b/src/hb-face.hh index 77c243721..baa1a2e57 100644 --- a/src/hb-face.hh +++ b/src/hb-face.hh @@ -71,6 +71,10 @@ struct hb_face_t }; #ifndef HB_NO_SHAPER hb_atomic_t shape_plans; + /* One-entry cache of the last plan looked up, so repeated shaping with the + * same key hits without walking the list. Nodes are never freed before the + * face, so a cached pointer stays valid. */ + hb_atomic_t last_shape_plan; #endif hb_blob_t *reference_table (hb_tag_t tag) const diff --git a/src/hb-shape-plan.cc b/src/hb-shape-plan.cc index 8f5287918..e6a79f9b2 100644 --- a/src/hb-shape-plan.cc +++ b/src/hb-shape-plan.cc @@ -567,10 +567,18 @@ retry: shaper_list)) return hb_shape_plan_get_empty (); + hb_face_t::plan_node_t *cached = face->last_shape_plan.get_acquire (); + if (cached && cached->shape_plan->key.equal (&key)) + { + DEBUG_MSG_FUNC (SHAPE_PLAN, cached->shape_plan, "fulfilled from last-plan cache"); + return hb_shape_plan_reference (cached->shape_plan); + } + for (hb_face_t::plan_node_t *node = cached_plan_nodes; node; node = node->next) if (node->shape_plan->key.equal (&key)) { DEBUG_MSG_FUNC (SHAPE_PLAN, node->shape_plan, "fulfilled from cache"); + face->last_shape_plan.set_release (node); return hb_shape_plan_reference (node->shape_plan); } } @@ -597,6 +605,7 @@ retry: goto retry; } DEBUG_MSG_FUNC (SHAPE_PLAN, shape_plan, "inserted into cache"); + face->last_shape_plan.set_release (node); return hb_shape_plan_reference (shape_plan); }