Cache document validation until the accessible tree changes
This commit is contained in:
1 parent
8cc293113e
commit
eb9dddbc2b
7 files changed
+94
-10
No files matched your search
@@ -124,9 +124,15 @@ func (tree *AccessibleTree) DocumentView(ctx context.Context, reference ObjectRe
|
||||
if err != nil {
|
||||
return DocumentSnapshot{}, err
|
||||
}
|
||||
if view.data != nil && view.data.validated && view.data.validRevision == tree.revision {
|
||||
return view, nil
|
||||
}
|
||||
if err := tree.validateDocumentRangeLocked(ctx, view, 0, len(view.Nodes)-1); err != nil {
|
||||
return DocumentSnapshot{}, err
|
||||
}
|
||||
if view.data != nil {
|
||||
view.data.validated, view.data.validRevision = true, tree.revision
|
||||
}
|
||||
return view, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -41,16 +41,13 @@ func (tree *AccessibleTree) DocumentLineAtOffset(ctx context.Context, reference
|
||||
|
||||
caretIndex := -1
|
||||
if reference.Valid() {
|
||||
for index, node := range document.Nodes {
|
||||
if node.Reference == reference {
|
||||
caretIndex = index
|
||||
break
|
||||
}
|
||||
if index, ok := document.Index(reference.ID()); ok && document.Nodes[index].Reference == reference {
|
||||
caretIndex = index
|
||||
}
|
||||
}
|
||||
if caretIndex < 0 {
|
||||
for index, node := range document.Nodes {
|
||||
if node.Text.CaretKnown {
|
||||
for index := range document.Nodes {
|
||||
if document.Nodes[index].Text.CaretKnown {
|
||||
caretIndex = index
|
||||
break
|
||||
}
|
||||
@@ -78,7 +75,8 @@ func (tree *AccessibleTree) DocumentLineAtOffset(ctx context.Context, reference
|
||||
ancestors = ancestors[1:]
|
||||
}
|
||||
line := DocumentLine{Document: document.Context.TopLevel, Context: ancestors}
|
||||
for _, node := range document.Nodes {
|
||||
for index := range document.Nodes {
|
||||
node := &document.Nodes[index]
|
||||
if node.Hidden {
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -14,6 +14,9 @@ type documentData struct {
|
||||
indices map[ObjectID]int
|
||||
ends []int
|
||||
rows map[ObjectID][]ObjectReference
|
||||
// validRevision is the tree revision at the last full validation.
|
||||
validRevision uint64
|
||||
validated bool
|
||||
}
|
||||
|
||||
func newDocumentData(nodes []DocumentNode) *documentData {
|
||||
@@ -136,6 +139,13 @@ func (tree *AccessibleTree) validateDocumentPathLocked(ctx context.Context, docu
|
||||
if from < 0 || to < 0 || from >= len(document.Nodes) || to >= len(document.Nodes) {
|
||||
return ErrStaleObject
|
||||
}
|
||||
// Ancestor checks depend only on the ancestor and this call's flags, and
|
||||
// any failure returns immediately. Once an ancestor passes, its own
|
||||
// ancestors have passed too, so later nodes sharing it can stop there.
|
||||
var validatedAncestors map[ObjectID]struct{}
|
||||
if from != to {
|
||||
validatedAncestors = make(map[ObjectID]struct{})
|
||||
}
|
||||
for index := min(from, to); index <= max(from, to); index++ {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return err
|
||||
@@ -152,7 +162,11 @@ func (tree *AccessibleTree) validateDocumentPathLocked(ctx context.Context, docu
|
||||
return err
|
||||
}
|
||||
for entry.hasParent {
|
||||
entry = tree.nodes[entry.parent]
|
||||
if _, done := validatedAncestors[entry.parent]; done {
|
||||
break
|
||||
}
|
||||
ancestorID := entry.parent
|
||||
entry = tree.nodes[ancestorID]
|
||||
if entry.snapshotInvalid {
|
||||
return ErrStaleSnapshot
|
||||
}
|
||||
@@ -175,6 +189,9 @@ func (tree *AccessibleTree) validateDocumentPathLocked(ctx context.Context, docu
|
||||
if err := tree.validateDocumentRowLocked(document, entry.snapshot.ID(), tolerateText); err != nil {
|
||||
return err
|
||||
}
|
||||
if validatedAncestors != nil {
|
||||
validatedAncestors[ancestorID] = struct{}{}
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
||||
@@ -132,6 +132,7 @@ func (tree *AccessibleTree) InvalidateText(id ObjectID) int {
|
||||
entry.textInvalid = true
|
||||
entry.text = VisibleText{}
|
||||
tree.nodes[id] = entry
|
||||
tree.revision++
|
||||
return 1
|
||||
}
|
||||
|
||||
|
||||
@@ -89,6 +89,8 @@ type AccessibleTree struct {
|
||||
nodes map[ObjectID]treeEntry
|
||||
nextGeneration uint64
|
||||
documents map[ObjectID]*documentData
|
||||
// revision changes whenever an entry is written or removed.
|
||||
revision uint64
|
||||
}
|
||||
|
||||
func NewAccessibleTree(root ObjectID, nodes []TreeNode) (*AccessibleTree, error) {
|
||||
@@ -127,6 +129,7 @@ func (tree *AccessibleTree) Replace(root ObjectID, nodes []TreeNode) error {
|
||||
}
|
||||
tree.root = root
|
||||
tree.nodes = next
|
||||
tree.revision++
|
||||
tree.documents = nil
|
||||
return nil
|
||||
}
|
||||
@@ -199,6 +202,7 @@ func (tree *AccessibleTree) InvalidateSnapshot(id ObjectID) int {
|
||||
}
|
||||
entry.snapshotInvalid = true
|
||||
tree.nodes[id] = entry
|
||||
tree.revision++
|
||||
return 1
|
||||
}
|
||||
|
||||
@@ -292,6 +296,7 @@ func (tree *AccessibleTree) InvalidateGeometry(id ObjectID) int {
|
||||
invalidated++
|
||||
entry.geometryInvalid = true
|
||||
tree.nodes[currentID] = entry
|
||||
tree.revision++
|
||||
}
|
||||
return invalidated
|
||||
}
|
||||
@@ -388,6 +393,7 @@ func (tree *AccessibleTree) Invalidate(id ObjectID) int {
|
||||
if childID == id {
|
||||
parent.children = append(parent.children[:index:index], parent.children[index+1:]...)
|
||||
tree.nodes[entry.parent] = parent
|
||||
tree.revision++
|
||||
break
|
||||
}
|
||||
}
|
||||
@@ -405,6 +411,7 @@ func (tree *AccessibleTree) Invalidate(id ObjectID) int {
|
||||
}
|
||||
pending = append(pending, current.children...)
|
||||
delete(tree.nodes, currentID)
|
||||
tree.revision++
|
||||
removed++
|
||||
}
|
||||
return removed
|
||||
@@ -427,6 +434,7 @@ func (tree *AccessibleTree) InvalidateChildren(parentID ObjectID) int {
|
||||
parent.children = nil
|
||||
parent.childrenInvalid = true
|
||||
tree.nodes[parentID] = parent
|
||||
tree.revision++
|
||||
removed := 0
|
||||
for len(pending) != 0 {
|
||||
last := len(pending) - 1
|
||||
@@ -438,6 +446,7 @@ func (tree *AccessibleTree) InvalidateChildren(parentID ObjectID) int {
|
||||
}
|
||||
pending = append(pending, current.children...)
|
||||
delete(tree.nodes, currentID)
|
||||
tree.revision++
|
||||
removed++
|
||||
}
|
||||
return removed
|
||||
@@ -456,6 +465,7 @@ func (tree *AccessibleTree) MarkChildrenChanged(parentID ObjectID) int {
|
||||
}
|
||||
parent.childrenInvalid = true
|
||||
tree.nodes[parentID] = parent
|
||||
tree.revision++
|
||||
return 1
|
||||
}
|
||||
|
||||
@@ -475,6 +485,7 @@ func (tree *AccessibleTree) markAncestorChildrenChanged(id ObjectID) int {
|
||||
if !entry.childrenInvalid {
|
||||
entry.childrenInvalid = true
|
||||
tree.nodes[id] = entry
|
||||
tree.revision++
|
||||
invalidated++
|
||||
}
|
||||
if !entry.hasParent {
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
package review
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"git.stormux.org/storm/cthulhu/internal/atspi"
|
||||
)
|
||||
|
||||
func TestDocumentViewRejectsChangesAfterCachedValidation(t *testing.T) {
|
||||
invalidations := map[string]func(*atspi.AccessibleTree, atspi.ObjectID) int{
|
||||
"text": (*atspi.AccessibleTree).InvalidateText,
|
||||
"snapshot": (*atspi.AccessibleTree).InvalidateSnapshot,
|
||||
"geometry": (*atspi.AccessibleTree).InvalidateGeometry,
|
||||
"children": (*atspi.AccessibleTree).InvalidateChildren,
|
||||
"marked": (*atspi.AccessibleTree).MarkChildrenChanged,
|
||||
}
|
||||
for name, invalidate := range invalidations {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
tree, contentIDs := benchmarkDocumentTree(&testing.B{}, 40)
|
||||
reference, err := tree.Reference(contentIDs[1])
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ctx := context.Background()
|
||||
for range 2 {
|
||||
if _, err := tree.DocumentView(ctx, reference); err != nil {
|
||||
t.Fatalf("initial view: %v", err)
|
||||
}
|
||||
}
|
||||
target, err := tree.Reference(contentIDs[30])
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
section, _, err := tree.Parent(target)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
targetID := contentIDs[30]
|
||||
if name == "children" {
|
||||
targetID = section.ID()
|
||||
}
|
||||
if invalidate(tree, targetID) == 0 {
|
||||
t.Fatal("invalidation changed nothing")
|
||||
}
|
||||
if _, err := tree.DocumentView(ctx, reference); err == nil {
|
||||
t.Fatal("view after invalidation was accepted")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -21,7 +21,7 @@ import "strings"
|
||||
|
||||
const Name = "Cthulhu"
|
||||
|
||||
const Version = "2026.11.17"
|
||||
const Version = "2026.11.19"
|
||||
const Channel = "testing"
|
||||
|
||||
var Build = Version
|
||||
|
||||
Reference in new issue
Block a user