Skip to content

Commit

Permalink
perf(serving): reduce patching to offsets; drop text (#5422)
Browse files Browse the repository at this point in the history
  • Loading branch information
schroederc committed Nov 2, 2022
1 parent 4ecc02a commit 69bef08
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions kythe/go/util/span/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ func InBounds(kind xpb.DecorationsRequest_SpanKind, start, end, startBoundary, e
// Patcher uses a computed diff between two texts to map spans from the original
// text to the new text.
type Patcher struct {
dmp *diffmatchpatch.DiffMatchPatch
diff []diffmatchpatch.Diff
spans []diff
}

// NewPatcher returns a Patcher based on the diff between oldText and newText.
Expand All @@ -60,7 +59,21 @@ func NewPatcher(oldText, newText []byte) (p *Patcher, err error) {
}
}()
dmp := diffmatchpatch.New()
return &Patcher{dmp, dmp.DiffCleanupEfficiency(dmp.DiffMain(string(oldText), string(newText), true))}, nil
diff := dmp.DiffCleanupEfficiency(dmp.DiffMain(string(oldText), string(newText), true))
return &Patcher{mapToOffsets(diff)}, nil
}

type diff struct {
Length int32
Type diffmatchpatch.Operation
}

func mapToOffsets(ds []diffmatchpatch.Diff) []diff {
res := make([]diff, len(ds))
for i, d := range ds {
res[i] = diff{Length: int32(len(d.Text)), Type: d.Type}
}
return res
}

// Patch returns the resulting span of mapping the given span from the Patcher's
Expand All @@ -75,8 +88,8 @@ func (p *Patcher) Patch(spanStart, spanEnd int32) (newStart, newEnd int32, exist
}

var old, new int32
for _, d := range p.diff {
l := int32(len(d.Text))
for _, d := range p.spans {
l := d.Length
if old > spanStart {
return 0, 0, false
}
Expand Down

0 comments on commit 69bef08

Please sign in to comment.