Skip to content

Commit 7e0627e

Browse files
committed
Use real_quick_ratio to fast-reject very dissimilar updates
1 parent ba21026 commit 7e0627e

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

jupyter_ydoc/yunicode.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010

1111
from .ybasedoc import YBaseDoc
1212

13+
# Heuristic threshold as recommended in difflib documentation
14+
SIMILARITY_THREESHOLD = 0.6
15+
1316

1417
class YUnicode(YBaseDoc):
1518
"""
@@ -74,15 +77,10 @@ def set(self, value: str) -> None:
7477
with self._ydoc.transaction():
7578
matcher = SequenceMatcher(a=old_value, b=value)
7679

77-
# for very different strings, just replace the whole content;
78-
# this avoids generating a huge number of operations
79-
if matcher.ratio() < 0.6:
80-
# clear document
81-
self._ysource.clear()
82-
# initialize document
83-
if value:
84-
self._ysource += value
85-
else:
80+
if (
81+
matcher.real_quick_ratio() >= SIMILARITY_THREESHOLD
82+
and matcher.ratio() >= SIMILARITY_THREESHOLD
83+
):
8684
operations = matcher.get_opcodes()
8785
offset = 0
8886
for tag, i1, i2, j1, j2 in operations:
@@ -99,6 +97,15 @@ def set(self, value: str) -> None:
9997
pass
10098
else:
10199
raise ValueError(f"Unknown tag '{tag}' in sequence matcher")
100+
else:
101+
# for very different strings, just replace the whole content;
102+
# this avoids generating a huge number of operations
103+
104+
# clear document
105+
self._ysource.clear()
106+
# initialize document
107+
if value:
108+
self._ysource += value
102109

103110
def observe(self, callback: Callable[[str, Any], None]) -> None:
104111
"""

0 commit comments

Comments
 (0)