Skip to content

Stackoverflow exception when adding a query in a loop #974

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

Closed
goalie7960 opened this issue Oct 6, 2014 · 5 comments
Closed

Stackoverflow exception when adding a query in a loop #974

goalie7960 opened this issue Oct 6, 2014 · 5 comments
Assignees

Comments

@goalie7960
Copy link

I can consistently create a stackoverflow exception when I run this code. Maybe it's not the most efficient, but it did cause an issue in our code, especially when the maximum value of i is generated at runtime.

class Program
{
    static void Main(string[] args)
    {
        QueryContainer query = null;

        for (int i = 0; i < 10000; i++)
        {
            query |= Query<Thing>.Term(f => f.ID, i);
        }
    }
}

class Thing
{
    public int ID { get; set; }
}
@djnelson9715
Copy link

The better way to do this would be with a terms query

var ids = new List().

//Add you id to ids list

//Then run your query
Query.Terms(f=> f.ID, ids)

Hope this helps

On Mon, Oct 6, 2014 at 8:53 AM, Chris Heckathorne [email protected]
wrote:

I can consistently create a stackoverflow exception when I run this code.
Maybe it's not the most efficient, but it did cause an issue in our code,
especially when the maximum value of i is generated at runtime.

class Program
{
static void Main(string[] args)
{
QueryContainer query = null;

    for (int i = 0; i < 10000; i++)
    {
        query |= Query<Thing>.Term(f => f.ID, i);
    }
}

}

class Thing
{
public int ID { get; set; }
}


Reply to this email directly or view it on GitHub
#974.

Doug Nelson

@goalie7960
Copy link
Author

That is fine for this example, thanks. But the issue still exists.

@Mpdreamz
Copy link
Member

Mpdreamz commented Oct 7, 2014

While I could not get it to stackoverflow on my machine the performance starts to degrade dramatically

These bitwise ops have been implemented mainly for DSL purposes and queries generally don't exceed more then a 100 individual queries pieces but what is very evident is that when it grows into the 1000s performance plummets.

1000 ops = 250ms
4000 ops = 4seconds

Which appears to a form of O(nX)

Whats going on is that this part of the code stacks a whole bunch of lazy concats to be evaluated:

https://github.com/elasticsearch/elasticsearch-net/blob/develop/src/Nest/DSL/Query/BoolQueryExtensions.cs#L35

which is then hit by this issue in the BCL:

https://connect.microsoft.com/VisualStudio/feedback/details/322004/poor-performance-of-multiple-chained-concat-commands

I'm working a fix now as well as refactoring the bitwise routines further.

@Mpdreamz Mpdreamz self-assigned this Oct 7, 2014
@Mpdreamz
Copy link
Member

Mpdreamz commented Oct 8, 2014

This is now fixed in develop and doing 10000 iterations takes 150ms on my machine. In the process all the bits of code that deal with bitwise operators (&&, ||, !) are heavily refactored and simplified.

Thanks for reporting this one @goalie7960 !

@Mpdreamz Mpdreamz closed this as completed Oct 8, 2014
@gmarz gmarz added Bug labels Oct 8, 2014
@goalie7960
Copy link
Author

Awesome! Thanks.

@gmarz gmarz added v1.2.0 and removed v1.1.3 labels Oct 20, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants