Skip to content

[Feature] Is it possible to improve the algorithm to a GC-free version? #39

Description

@DingpingZhang

I tried to write a search application demo (WPF UI) by this library, but found that the UI was lagging on input.

Here is my code: I have dispatched the search part of the logic to a non-UI thread and, in fact, the algorithm is fast enough not to block the UI.

            Observable
                .FromEvent<string>(
                    handler => SearchTextChanged += handler,
                    handler => SearchTextChanged -= handler)
                .ObserveOn(NewThreadScheduler.Default)
                .Throttle(TimeSpan.FromMilliseconds(200))
                .Select(text => text.Replace(" ", string.Empty).ToLowerInvariant())
                .Select(text => _fullCollection
                    .Select(item => (
                        entity: item,
                        radio: new[]
                        {
                            Fuzz.PartialRatio(text, item.Name, FuzzySharp.PreProcess.PreprocessMode.Full),
                            Fuzz.PartialRatio(text, item.Alias),
                            Fuzz.Ratio(text, item.Id),
                            //item.Name.Contains(text) ? 100 : 0,
                            //item.Alias.Contains(text) ? 100 : 0,
                            //item.Id.Contains(text) ? 100 : 0,
                        }.Max()))
                    .Where(item => item.radio > 50)
                    .OrderByDescending(item => item.radio == 100 ? item.entity.Frequency : item.radio)
                    .Take(10))
                .ObserveOnDispatcher(DispatcherPriority.Background)
                .Subscribe(searchResult =>
                {
                    SearchResult.CanNotify = false;
                    SearchResult.Clear();
                    foreach ((BrandCodeEntity entity, int radio) item in searchResult)
                    {
                        item.entity.Radio = item.radio;
                        SearchResult.Add(item.entity);
                    }

                    SearchResult.CanNotify = true;
                });

So, I did a performance analysis and found that the algorithm execution cause 1 second of time in doing GC.
If I use string.Contains(), I don't have this problem.

image

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions