-
-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
FullTextIndex is much slower than LINQ when infix length is < 3 #103
Comments
Hi, thanks for your detailed feedback. Highly appreciated! I guess it's kind of by design. The full text index is using a Suffixx Trie and hence, each element is added multiple times within the Trie. I.e. when you add something with "foo" as key, it is also added as "oo" and "o". If you now ask for a short string, the matching node is very close to the root and will have lots of children, often containing the same node more than once: "foo" will be in the list twice when querying with 'o'. Hence, the simple linear scan can be faster because the index is actually doing much more work in such cases. I quickly looked into the implementation and have found a few nobs I can turn without being too much to work on. However, I do not know in advance how big the improvements will be. There would also be the possibility of special casing small values or algorithmic/data structural changes such as merging long leafs or maybe switching to a DAG... Another idea is limiting the number of results, would that be sufficient for your use case? I will look into it and keep you updated. |
Excited to see the results of this.
I'm not sure how much this would help in my use-case as I have a lot of Japanese text that needs to be searched, which means thousands of unique characters. However, it might be a good idea overall, if people only have ASCII text.
Unfortunately not. My use-case requires me to search the data multiple times in different ways, concatenating the results from every call, and then ordering them at the end to be able to get the correct result. Thanks for looking into it. I might be able to provide a full reproduction of my use-case if these improvements aren't enough. |
Here are my result of looking into it: While I got some improvements, it's shown to be only somewhat effective on memory consumption (~10%). There is more potential by pooling and so on, but not really runtime wise. After that experiment, I came to the conclusion that the main problem really is the theoretical runtime complexity and no tweaking will offer that big perf improvements as LINQ is also heavily optimized. LINQ is O(n) while within the suffixx tree it will always include an This means that there needs to be an algorithmic change one way or another. So here is what I imagine could be the solution: I'm currently improving the library to support multiple keys for all kind of indexes (maybe except the range index), which might be in fact a solution for you. Common techniques are to index a document by its words (or by n-grams, ...) and that feature will allow that. This means that Results of the prototype:
As you can see, the results are improved dramatically but still heavily depend on what you index and the query length. Do you think that would be a solution for you? I'd be willing to test that on your data if you want to provide a full reproduction. However, if you have enough memory to throw around, a workaround (that works right now) can also be special casing:
This works like this: _indexedSet = _document.ToIndexedSet()
.WithFullTextIndex(x => x.Content)
// special case for a single char, O(1), z is for a different index name, you might want to use a static function
.WithIndex<char>(z => z.Content)
.WithIndex(x => NGrams(x.Content, 2)) // special case for 2-gram O(1)
.Build();
IEnumerable<Document> Query(string query)
{
return query switch
{
[char a] => _indexedSet.Where<char>(z => z.Content, a),
string { Length: 2 } twoGram => _indexedSet.Where(x => NGrams(x.Content, 2), twoGram),
string fullText => _indexedSet.Contains(x => x.Content, fullText)
};
}
private static IEnumerable<string> NGrams(string text, int nGramSize)
{
for(int i = 0; i < text.Length - nGramSize; i++)
{
yield return text.Substring(i, nGramSize);
}
} Integrating that into the FullTextIndex might also be worth a try, but that would be orthogonal to the former improvement anyway and I'd yet have to think about a good heuristic or API for this... |
I actually came across this library while looking for a C# equivalent of PostgreSQL's trigram indexes, so I think using ngrams is definitely the way to go for this. Unfortunately it looks like the improvements are not good enough for my use-case. I really need it to be faster than LINQ for 1 and 2 letter cases. The special-casing approach looks promising, but I couldn't get your example to work because I need both Contains and StartsWith queries against the data, and it looks like I'll try to create a reproduction of my use-case, but it might take a while. Thanks again for looking into this. |
Good to hear that ngrams would be useful to you. Motivates me to keep working on the feature. I may also include some full text helpers for NGrams and similar, though that would come in a second version...
Yes, that is not supported by that kind of index (dictionary-based). Chaining a normal
Thanks, might be useful for me. Especially since I'm only used to latin / germanic languages... |
Here: You may have to configure the path to the |
I just merged the "multi-key everything" PR and with that (local built version) I can get much better results on your data. It works by using an 3-grams approach for contains-queries and a normal prefix index. With your data, the 3-gram indices is not ideal for Prefix-queries. Hence the seperate prefix indices. It should be ok (though I have not measured it) memory-wise as the Suffixx Tries for the FullTextIndex should be much smaller with 3-grams. Can you work with that? I hope that I can release a new version soon.
+ #pragma warning disable AkadeIndexedSetEXP0001
public static class Autocomplete
{
[...]
+ public static IEnumerable<string> NGrams(string input, int n)
+ {
+ for (int i = 0; i < input.Length - n + 1; i++)
+ {
+ yield return input.Substring(i, n);
+ }
+ }
public static void BuildIndexedSetMst(IEnumerable<AutocompleteMst> data)
{
SetMst = data.ToIndexedSet()
- .WithFullTextIndex(x => x.MSTLatinTitle.NormalizeForAutocomplete())
- .WithFullTextIndex(x => x.MSTNonLatinTitle.NormalizeForAutocomplete())
+ .WithPrefixIndex(x => x.MSTLatinTitle.NormalizeForAutocomplete())
+ .WithPrefixIndex(x => x.MSTNonLatinTitle.NormalizeForAutocomplete())
+ .WithFullTextIndex(x => NGrams(x.MSTLatinTitle.NormalizeForAutocomplete(), 3))
+ .WithFullTextIndex(x => NGrams(x.MSTNonLatinTitle.NormalizeForAutocomplete(), 3))
.Build();
}
[...]
public static IEnumerable<string> SearchAutocompleteMstIndexed(string arg)
{
arg = arg.NormalizeForAutocomplete();
var startsWith1 = SetMst.StartsWith(x => x.MSTLatinTitle.NormalizeForAutocomplete(), arg);
var startsWith2 = SetMst.StartsWith(x => x.MSTNonLatinTitle.NormalizeForAutocomplete(), arg);
var startsWith = startsWith1.Concat(startsWith2).OrderBy(x => x.MSTLatinTitle).ToArray();
- var contains1 = SetMst.Contains(x => x.MSTLatinTitle.NormalizeForAutocomplete(), arg);
- var contains2 = SetMst.Contains(x => x.MSTNonLatinTitle.NormalizeForAutocomplete(), arg);
- var contains = contains1.Concat(contains2).OrderBy(x => x.MSTLatinTitle).ToArray();
+ IEnumerable<AutocompleteMst> containResult = [];
+
+ foreach(string ngram in NGrams(arg, 3))
+ {
+ containResult = containResult.Concat(SetMst.Contains(x => NGrams(x.MSTLatinTitle.NormalizeForAutocomplete(), 3), ngram));
+ containResult = containResult.Concat(SetMst.Contains(x => NGrams(x.MSTNonLatinTitle.NormalizeForAutocomplete(), 3), ngram));
+ }
+ var contains = containResult.Distinct()
+ .Where(d => d.MSTLatinTitleNormalized.Contains(arg) || d.MSTNonLatinTitleNormalized.Contains(arg))
+ .OrderBy(x => x.MSTLatinTitle)
+ .ToArray();
var startsWithLT = startsWith.Select(x => x.MSTLatinTitle);
var startsWithNLT = startsWith.Select(x => x.MSTNonLatinTitle);
[...] |
Looks really really good. Thanks for spending so much time on this. By the way, is there any way you can either make the index building async, or make the built set serializable/deserializable? The reason I'm asking is that I will be using this on a Blazor WebAssembly project, and it takes like 3 times more than normal (over 5 seconds on my PC) to build the index there, while locking up the UI thread (i.e. freezing the user's browser tab completely) because multi-threading for Blazor WASM is still not implemented (keeps being pushed back every release). |
Happy to help. There is nothing that prevents you to index in a separate thread in general. There even is a concurrent version for multi-threaded scenarios. However, JS & WASM do not really do multi threading (async I/O yes, but your code runs always in the same thread). The .NET effort to make it possible using web workers is still on going: dotnet/runtime#68162. So, unfortunately you are out of luck here. Serialization is something to explore, but it will probably take a significant amount of work to actually make it worthwhile, i.e. that the potential speed increase is actually worth the increased serialization payload size compared to serializing only the entities. In general though, currently not much of the optimization work time has been invested yet into making the creation faster... I'm sure there are improvements to be found there and if there's demand for that being faster, I'm happy to look into that in the future. However, I will finalize the "value-everything" version as a first step. Unfortunately, life happened in a very bad way and I'm not able to make any promises, only that I will get to it eventually. I will close this issue then, and will then create a new issue for exploring serialization. |
Understood. Hope everything is all right. |
@mertvn I'm in the process of publishing the changes :) thanks for being patient & friendly, highly appreciated! The version is marked as preview as I will evaluate if source link works and check if the "multi-key everything" feature does not break the clients I'm aware of (which I expect). Happy to hear feedback from you if the feature is an improvement for you. |
Hi, I encountered this problem while trying to use this library for a real-world scenario, but it reproduces on your benchmarks as well, so I just used those.
I tested it on commit 7da0898 and using .NET 7, but I doubt using main and .NET 8 would make a big difference.
I'm not sure if this is a design limitation or a bug with the current implementation of the FullTextIndex, but I thought that I'd let you know in any case, as this issue prevents me from using this otherwise wonderful library.
Add to FullTextIndexBenchmarks.cs:
BenchmarkDotNet v0.13.9+228a464e8be6c580ad9408e98f18813f6407fb5a, Windows 10 (10.0.19045.2486/22H2/2022Update)
AMD Ryzen 5 4500U with Radeon Graphics, 1 CPU, 6 logical and 6 physical cores
.NET SDK 7.0.404
[Host] : .NET 7.0.14 (7.0.1423.51910), X64 RyuJIT AVX2
.NET 7.0 : .NET 7.0.14 (7.0.1423.51910), X64 RyuJIT AVX2
Job=.NET 7.0 Runtime=.NET 7.0
The text was updated successfully, but these errors were encountered: