Hacker News new | ask | show | jobs
A curious case of O(N^2) behavior which should be O(N) (2023) (gist.github.com)
101 points by bssrdf 551 days ago
10 comments

More accidentally quadratic stories: https://www.tumblr.com/accidentallyquadratic
A classic. Every time I see that link I read the whole thing starting from the new beginning.

...oops

I opened the link and just started reading. I have a really dumb question that may expose common knowledge I don’t have, about this quote:

> The total amount of space needed to represent this collection of strings is O(k n^2).

I haven’t seen O-notation ever represent ram usage, just algorithm complexity. Is this common?

Yes, very common. You've seen "time complexity"; it's very common to talk about "space complexity" as well.
Fun bonus: they can be interchangeable, e.g. increasing space to reduce time.
And any operation that takes n bits as input can be trivially turned into an O(1) time and O(2^n) space algorithm through tabulation.
Yes, but total time is never going to be less than total space, when expressed in big-O notation
> Is this common?

Very. For instance if you look at sorting algorithms on wikipedia they pretty much all list performance (best, worst, average) but also worst-case space complexity, in O notation.

Is there a way to do that without signing in?
While taking a computational complexity course I wrote some python code that should have run in O(n) but was closer to O(n^2). After asking StackOverflow and some more experimentation it turns out the garbage collector turned it O(n^2) - turning it off manually yielded the correct O(n) runtime
Why was the solution not to get rid of the linked list and replace it with a red/black tree?
Or hash table, or any other kind of tree.

My guess would be because it was implemented in c, where the usual practice if you need a container type other than a fixed size array is to implement it yourself. IME, c code tends to use linked lists for a lot of things that in most other languages would be implemented using a better suited, and more performent, container type from the standard library.

One way that other languages can outperform c, is it is easier to use the right data structure.

Blender's not even in C (the snippets are clearly C++), I wonder what the logic of having a sorted doubly linked list is: unless it's a skip list it's not like you can do bisection searches.

I guess a flat array could still be debatable if you're usually inserting near but not at the end, as it'll still need to move all the following elements. But it seems dubious.

I got the impression that the expected performance of the double linked list insertion is actually O(1), since in most cases the elements arrive in sorted order. It's been a time since my algorithm courses, but I think all the 'normal' trees have log(n) insertion in that case.
O(n) is generally indistinguishable from O(log n) so if there is any chance of different behavior than the expected optimum, go with the better algorithm.
Neat find! This proves to me a huge benefit of open source software: individual developers can satisfy their curiosity towards particular issues (with additional motivation from being personally affected) and improve the package for everyone once it's fixed.
Except it wasn't improved in this case, if I'm reading the gist correctly. The author didn't like their fix enough to make a pull request.
Just like “this entire meeting could have been an email”, all this effort writing this gist could have been spent creating a PR.
Ah yes, the most reliable solution to a wrong choice of a data structure: add data-specific hacks until it works. For the life of me I can't figure out why people never consider replacing linked lists. Even without worst-case O(n) insertion, they usually behave worse than vectors, deques, or hives.
What is a hive in the context of data structures?
I meant something close to https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p04...

Perhaps a simple way to look at it is that it's like a dynamic array, but when the capacity is exceeded, you don't reallocate the array, just just allocate a new (exponentially larger) chunk and keep the old one as-is. Then just link the chunks in a (very short) linked list, or keep a separate small list of chunks (you're never gonna need more than 48, so you can just have a fixed allocation for it), or what have you. The bonus here is that it reduces latency on pushing and has more predictable performance.

I assume it's an autocorrect of heap.
There's also the choice of data structure that appears to force multiple copies of the same thing rather than thin references and ideally copy on modify.
I had something like this. I was making a script that would parse a standardized file describing a molecule and create the atoms and bonds and whatnots. I found out that they time an object is added it iterates through all the objects to update them of the addition. This was very noticable even around 1k objects. I later grouped all the duplicated objects (such as all the oxygens) into 1 discontinuous object, leading to O(1 [the bonds] + #elements) instead of O(#bonds * #atoms).

In the end I still lost out to another package that did the same function a lot faster. May be interesting to look back to see how they did it.

Why not just change the “%s.%u” to “%s.%010u” and no code changes?
You just created a fix up to a large, but arbitrary, number just pushing the problem down the road instead of fixing it.
If I understood the article correctly it caused problems when the same file was imported multiple times, or when another file with the same base name was imported.

https://gist.github.com/bssrdf/397900607028bffd0f8d223a7acdc...

Yes, TFA says the issue is because "12345" sorts before "9888"

My solution avoids that.

"Now, it should be clear that most of the time (28s-53s on the time line) of importing that particular USD file is spent in id_sort_by_name".

Luckily there's a graph screenshot. But the graph it displays is incomprehensible. Without "id_sort_by_name" being on the one bar, I wouldn't even know what I'm looking at.

The timeline view in the upper screenshot is fairly straightforward: the region of 28-53s is selected, other parts of the timeline are grayed out, and the statistics for the selected time region down below show 94.4% of "Total CPU" being in id_sort_by_name.

The lower screenshot is a flame graph. If you haven't encountered one of those before, it's totally reasonable to not know how to read it. But it's a standard and common way to present this sort of data. See https://www.brendangregg.com/flamegraphs.html for more information.

2 questions I would look into before anything else:

1. Does this have to be sorted?

2. Why is it sorted alphabetically instead of naturally?

“alphebatically” is used 3 times, and otherwise no typos that I noticed. So I wondered if it’s a term I’m not familiar with.