Description
TM suggestions and the TM examples in Tolgee AI prompts both use the trigram similarity query in TranslationMemoryServiceEeImpl / TranslationMemoryServiceOssImpl. Its cost grows with the length of the source text and with the number of source texts in the TMs assigned to the project.
On a large database, with a shared TM of about 240k source texts, the query took:
- 12 characters: 1.9 s
- 300 characters: 6.8 s
- 1000 characters: 26 s
- 1200+ characters: more than 40 s
Tolgee AI waits for the TM examples, so for long texts neither the TM panel nor the AI translation loads. #4098 skips the TM lookup for texts over 1000 characters as a quick fix.
Proposals, from smallest to largest:
-
Length filter. With the 0.5 similarity threshold, a match can't be much shorter or longer than the source text. Adding a condition like
length(base_t.text) between length(:text) / 2 and length(:text) * 2to the candidate rows took all lengths (12 to 2700 characters) down to 0.6 - 2.2 s on the same data, with no new index. Character length is only an estimate of the trigram count (repeated trigrams count once), so the bounds should be a bit looser, and a test should check that no real match is lost. With this, the 1000 character skip from fix: skip translation memory lookup for texts over 1000 characters #4098 can be removed. -
Don't let TM examples block Tolgee AI. If the lookup is slow, the AI should translate without examples rather than wait. A timeout needs care: a failed statement aborts the caller's transaction, so the lookup would need its own transaction.
-
Ignore HTML tags when matching. In texts with HTML, tags like
<p>,<strong>andstyle="..."make up a big part of the trigrams. Replacing them with placeholders before matching would make the lookup faster and the similarity scores more accurate. -
Match by sentence instead of by whole text. CAT tools split long texts into sentences and match each one separately, so one changed sentence doesn't drop the match for the whole text. This is a product decision and a bigger change to how TM works.
Activity