Skip to content

Commit 0b2ebf0

Browse files
committed
get_next_shape_internal: Skip VM lock for single child case
If the shape has only one child, we check it lock-free without compromising thread safety. I haven't computed hard data as to how often that it the case, but we can assume that it's not too rare for shapes to have a single child that is often requested, typically when freezing and object.
1 parent 73f8d0a commit 0b2ebf0

1 file changed

Lines changed: 11 additions & 1 deletion

File tree

shape.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -499,13 +499,23 @@ get_next_shape_internal(rb_shape_t * shape, ID id, enum shape_type shape_type, b
499499

500500
*variation_created = false;
501501

502+
// Fast path: if the shape has a single child, we can check it without a lock
503+
struct rb_id_table *edges = shape->edges;
504+
if (edges && SINGLE_CHILD_P(edges)) {
505+
rb_shape_t *child = SINGLE_CHILD(edges);
506+
if (child->edge_name == id) {
507+
return child;
508+
}
509+
}
510+
502511
RB_VM_LOCK_ENTER();
503512
{
504513
// If the current shape has children
505514
if (shape->edges) {
506515
// Check if it only has one child
507516
if (SINGLE_CHILD_P(shape->edges)) {
508-
rb_shape_t * child = SINGLE_CHILD(shape->edges);
517+
rb_shape_t *child = SINGLE_CHILD(shape->edges);
518+
509519
// If the one child has a matching edge name, then great,
510520
// we found what we want.
511521
if (child->edge_name == id) {

0 commit comments

Comments
 (0)