Repository navigation
Add sparseGrams function implementation - #78176
Conversation
3def5d0 to
f5a4bc0
Compare
acce145 to
cee446a
Compare
cee446a to
65e80fe
Compare
jkartseva
left a comment
There was a problem hiding this comment.
LGTM, test failures are unrelated.
Please take a look at the comments.
| private: | ||
| Pos pos; | ||
| Pos end; | ||
| std::vector<UInt32> ngram_hashes; |
There was a problem hiding this comment.
Thinking about the problem, it can be optimized (there is no need to do it here). For example, in corner cases that produce a lot of repetitive n-grams, a hashmap can help.
There was a problem hiding this comment.
I think it doesn't make sense to store hashes in a hashmap. It means you have to calculate hash to get hash from the map :)
crc32 is a fast algo, maybe we don't need to store it at all, just left and right to avoid double calculation
|
Test failures are unrelated |
| } | ||
|
|
||
| /** Functions that finds all substrings win minimal length n | ||
| * such their border (n-1)-grams' hashes are more than hashes of every (n-1)-grams' in substring. |
There was a problem hiding this comment.
are more -> are larger
| { | ||
| if constexpr (is_utf8) | ||
| { | ||
| Poco::UTF8Encoding encoder{}; |
There was a problem hiding this comment.
This is extremely slow. We have good functions in UTF8Helpers.h
| Pos pos; | ||
| Pos end; | ||
| std::vector<UInt32> ngram_hashes; | ||
| std::vector<size_t> utf8_offsets; |
| private: | ||
| Pos pos; | ||
| Pos end; | ||
| std::vector<UInt32> ngram_hashes; |
| utf8_offsets.push_back(byte_offset); | ||
| auto len = encoder.sequenceLength(reinterpret_cast<const unsigned char *>(pos + byte_offset), end - pos - byte_offset); | ||
| if (len < 1) | ||
| throw Exception(ErrorCodes::BAD_ARGUMENTS, "Incorrect utf8 symbol"); |
There was a problem hiding this comment.
We should better use implementation-defined behavior, similarly to ngramDistanceUTF8.
| chassert(right > left); | ||
| while (left < ngram_hashes.size()) | ||
| { | ||
| while (right < ngram_hashes.size() && right <= left + max_ngram_length - min_ngram_length + 1) |
There was a problem hiding this comment.
The size is calculated in a loop, and the compiler typically cannot optimize it.
|
|
||
| /** Functions that finds all substrings win minimal length n | ||
| * such their border (n-1)-grams' hashes are more than hashes of every (n-1)-grams' in substring. | ||
| * As a hash function use zlib crc32, which is crc32-ieee with 0xffffffff as initial value |
There was a problem hiding this comment.
Should use hardware-optimized CRC32-C.
|
Hi @GrigoryPervakov @jkartseva — while reviewing this PR I found the following:
Happy to discuss — close anything that's wrong or already addressed. |
Continuation of #66341
This closes #65694
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes to CHANGELOG.md):
Functions sparseGrams and sparseGramsHashes with UTF8 versions added. Author: scanhex12.
Documentation entry for user-facing changes