The true cost of linked lists
ykarroum.com
ykarroum.com
Big-O assumes all "operations" are equally costly. That's not the case on real hardware, and pretty much never has been. Some instructions take more cycles than others.
Big-O assumes that only asymptotic behavior matters, but real-world workloads have finite input sizes.
Etc, etc. An algorithm's complexity is loosely correlated with its performance, but the two are not identical.
Yes this loop is fast now with my 1000 items, but what if the input grows to 100000 or more?
Keeping it in mind can also help you avoid accidentally writing O(n^2) loops or worse. More than once I've been unsure about the complexity of a library call, so I check the code and it's say O(n) rather than O(1), potentially turning my own O(n) into a O(n^2).
Assuming that operations take a linear amount of time (i.e. multiplying three times takes three times as long as multiplying once) this won't affect the asymptotic behavior.
>Big-O assumes that only asymptotic behavior matters, but real-world workloads have finite input sizes.
This is definitely something to keep in mind when analyzing algorithms, but that does not imply asymptotic complexity is not useful when analyzing performance. There are other measures (e.g. how an algorithm performs on a random small input, or maybe your domain is restricted somewhat) and sometimes those measures areore useful than big O, but big O remains useful, it just is not the end all be all.
This assumption is demonstrably broken if the first multiplication is a cache miss but the other multiplications then aren't -- an easy example is when the other two multiplications have data on the same cache line as the first multiplication's data.
[1] https://isocpp.org/blog/2014/12/efficiency-with-algorithms-p...
For example you could count only non-cached memory accesses on a particular model cpu. See "Idealized Cache Model" from https://en.m.wikipedia.org/wiki/Cache-oblivious_algorithm for example.
This is part of algorithms analysis that a lot of developers skip: these are just tools, there's many models available to use with them, or if you want to, just use wall-clock, with all of the pain that comes with that.
Everyone seems to forget this. It's more a measure of how an algorithm will scale rather than performance.
e.g. an O(n) algorithm will most likely outperform an O(n^2) algorithm, but two different O(n) algorithms can still perform quite differently.
But you're right, it's meant for comparing scaling.
https://github.com/sio2project/sio2jail/blob/master/src/perf...
No, it only assumes that there is a constant factor between the fastest and slowest "operations". It does not matter that one instruction can take a thousand times more cycles than another, if you have n² fast instructions and n slow ones, the running time will still be dominated by the n² fast ones for large n.
> Big-O assumes that only asymptotic behavior matters.
Yes, and this is the only simplification that it does.
As a result k*N can actually be bigger than N^2 when in fact N isn't "an integer" in a mathematical sense but merely a 32-bit machine integer, for example - simply by k being more than 4 billion in that case.
If you're determined to throw out any concepts which technically only apply to theoretical computers with unbounded memory, then go all the way. Your actual physical computer can trivially iterate through all of its possible states in a fixed amount of time. The halting problem is trivially solvable for all programs that your actual physical computer can execute. Your actual physical computer isn't even Turing complete.
Nope. You're in an imaginary world again. This universe will cease to support computation a long time before it would be possible for the computer to try all possible states.
Yes, for the purposes of Big O, we define operations such that they aren't simply 1:1 instructions to the hardware, but they should be basic commands offered by your programming language.
If the basic commands in your language are taking extremely high bounded amounts of time, that's a sign your programming language is extremely poorly optimized, not that Big O isn't useful.
Sure, you can choose a large constant such that numerically it is equal to or smaller than O(n^2) for a given n, but as you vary n then O(1) should approximate a flat `y=N` line, while O(n^2) should approximate a parabola and would result in values larger and smaller than N as you vary it.
This is a big misconception about "Big-O". It does not "assume" anything. It's just not what people think it is.
It's an asymptotic upper bound on counting something. In sorting algorithms you count the number of comparisons expressed as a function of collection size. In collection insertion algorithms on some underlying data structure, it's counting some not-very-clearly-defined atomic operations that are being executed when inserting an element.
Where the assumptions come from is if you start using the notation to give performance comparisons and also start assuming things about Big-O that are just not true. But that has nothing to do with "Big-O".
There is a family of symbols, called the Landau symbols, of which Big-O is only one. There is a lot of misuse of the symbol and in many cases what people actually mean is Theta(N) or Omega(N).
"People who believe in X are wrong because A, B, C."
"No, X is actually right, because if you know about these obscure parts of X-theory which laypersons never hear about, you will see that it addresses A, B and C."
Of course, both these comments are right, because in addition to the fact that X-theory does in fullness address those issues, few people know how it does.
Also probably a part of a course many people snooze through.
The courses that introduce big-O are often the weeder courses, they don't aim to education, they aim to flunk out the people that might be hard to teach so that CS departments can gate keep.
When you have top CS researchers having to give talks to remind people that layout matters more than instruction selection, the CS as a whole has focused on the wrong things.
https://www.youtube.com/watch?v=7g1Acy5eGbE
Figure how to build people up, not tear them down.
That is an often re-iterated conspiracy theory. In practice nothing could be further from the truth. Professors have nothing to gain from weeding out students. They have to gain by ensuring a certain standard. By the way it is also better for a student to encounter the difficult parts of their subject as early as possible, so that they can decide whether they have chosen their subject wisely. On the other hand, not failing students on fundamentals means complicating advanced subjects even further --- for everybody.
At some point in life students have to accept that neither their time in university nor their time in the industry will consist only of externally motivating tiny bites of tasks that are awesome. In order to understand complicated matters, you have to put in the work. Spending hours in the gym is also quite boring, but most people accepts that it is necessary for certain results.
I've been involved with undergrad teaching in a University. And the amount of time we spend on finding ways to teach and test better was way more than I could have expected as a student myself. On the other hand I couldn't have imagined the arrogance and sometimes stupidity of first year student claiming to know already what they will need in there career and what not.
So much to unpack there. Some rhetorical hyperbole, other false dichotomy and we are both arguing from anecdote.
Weeder courses exist and as you even admit, they aren't solely meant to educate. That is the point. Anything that is designed to test students resolve as well as not waste professors time is not designed to educate, it is designed to reduce the size and shape of the student population. I applaud that CS departments are adding more and more courses for non-majors. But lets look at the syllabus for a 10 week quarter for the first UW CS course, Foundations of Computing [1] which also expects the students to layout their homework (proofs) in LaTex. Jesus. This is mostly a hazing ritual. In my 3 minute sampling of Reddit, most CS students rank 311 as the hardest course in the major and it is the first. [2]
> At some point in life students have to accept that neither their time in university nor their time in the industry will consist only of externally motivating tiny bites of tasks that are awesome.
I never said anything of the sort, you putting words in my mouth.
[1] https://courses.cs.washington.edu/courses/cse311/21sp/
[2] https://www.reddit.com/r/udub/comments/cdb5kp/cse_311_cse_33...
Your argument might make more sense if 311 were the first CS course, but it's not. You can easily verify that by looking at the course listings which reveals it has a prereq, CSE143, which has a prereq of CSE142. So short of skipping those, it's the third course at the earliest.
311 is the first course of the major and is also by the sample of folks in the reddit discussion also the hardest, covering the most advanced material in the shortest amount of time. It is a weeder course for the major. The 100 level classes are open to everyone.
Nailed it. My frustration is that those words are rarely mentioned, and that atomic isn’t really atomic except in the context of the problem statement and solution.
In my experience, we all spend more of our time dealing with what people think things are than what they really are. Even if reality tends to pop its head up now and then.
I think the root comment is correctly describing "how people talk about Big-O" - even though, as you point out, they are mischaracterizing it.
I find this is often a more vexing problem than the underlying performance questions: how do we find good ways to talk about the use (and mis-use) of analysis in a way that produces good tools?
Similarly “competitive programming”, A&D and coding puzzles which I find fun but only tangentially useful, will typically completely neglect engineering constraints. The shape, size frequency and variations of data can almost always be at least estimated in the real world, or simply assumed.
Real assessments of performance go far beyond the depth and breadth of expertise that most students get in undergrad - so it's a fools errand to try and junior engineers about concepts like that. Big-O has a lot of flaws but I get why it seems so useful to all the groups that use it.
It’s really just about making decent estimates, testing and some profiling. There’s of course potential mastery here, specific knowledge and tooling, you can dig down almost indefinitely, but the basic motions can be taught with simple projects. You don’t need Google scale to encounter performance problems and potential improvements.
I know this because I work on small scale things, and had to learn this kind of thing on the job. I can imagine a ton of educational projects that teach this.
Complexity analysis and A&D are very useful when you need them, but they are just two of several tools.
Similarly the term we use is often "N" and "N" could mean anything, and in practice you need to know what "N" you are optimizing for/against. That's where the engineering constraints fit in. It doesn't matter of you pick an algorithm with good Big-O in time (where n represents number of operations, for instance) if your are constrained in space (memory size; cache size; cache locality) and vice versa. Big O notation is an approximation with respect to some function and that n among a selection of choices of different functions/different "ns", that complexity function itself can vary a lot based on your trade-offs. (Which also gets back into Big O is only one such tool, as well. Sometimes you really need to know worst case "Omega notation" and not really Big O notation, if your constraints include lots of worst case things. And so forth.)
Estimation isn't "destiny": know what you are estimating, why you are estimating it, and with respect to what constraints you are estimating it. Big O notation is "back of the envelope math" for algorithms. It serves some great uses, especially in practice, but you need to know what you are estimating and why.
Typically a program is evaluated in terms of something like the "Random-access machine" where every instruction has a constant cost. There's a bunch of additional assumptions hidden in this machine model!
In the real world, the speed of light and the Bekenstein bound conspire to make constant-time random-access to a memory of unlimited size impossible. In practice, a random memory access takes O(sqrt(N)) time. We like to pretend that there's a constant worst-case access time, but that only works out because our machines have a limited amount of memory -- it's not really appropriate for an asymptotic analysis.
So complexity theory based on the "Random-access machine" is just measuring a theoretical instruction count that doesn't necessarily correspond to the real-world run-time. There's other models that get closer, e.g. the "cache-oblivious model".
In its own constructed universe, orders of complexity doesn't obey basic physics. Imagine I'm trying to sort a billion unique books alphabetically by content. In order for the books to be unique, m > logn. For many big problems logn < m < sqrt(n) but for a lot of the rest n < m on day one, and stays that way for quite some time. For large objects, an algorithm that avoids comparisons can be faster with real data even if its complexity is n(logn)^2.
Where we fuck up is pretending that mathematical operations are O(1) when they are O(logn) on a good day and O(n) on a bad one. + and - are constant if the numbers fit into a single integer. [] is only constant for n < L1 cache size, and even then only if no other tasks are happening. And as I've just described, < and == are not constant time either. We need better math for these sorts of things.
Initially, prefer to task CPU over memory allocation. Crufty allocation generally will always have a real negative impact felt by the end-user. A low memory footprint is always a win, while going for a (theoretical) fast running time may be effort wasted.
1. A small computation that you perform many, many times.
2. A very, very large computation.
For #2, big-O can still generally tell most of the story. (Example: 100 insertions in the middle of a list with a billion elements.) For #1, big-O almost irrelevant, and benchmarking is key. (Example: a billion insertions into the middle of length-100 lists.) So knowing which situation you're in is important.
As you go up the scale the Big-O values are almost always dominant. It's very rare to see a situation where you would choose the algorithm with the higher value.
However, when two algorithms have the same Big-O there can still be a big difference in performance, especially in the face of how a cache influences things.
(There are cases where the memory accesses are so important that they can make the "bad" approach better. A while back I did some time testing and found the Sieve of Eratosthenes inferior to brute force because of all the cache misses.)
But if you are starting out with nothing built and only have time to write one, you can't really go wrong by going with the one with the better Big O. If it's the same, coin toss I guess. :)
Well I did have a CS teacher that said that O(log n) is basically O(1) because in practice, n usually will fit it in a 32bit and log n then is 32 at most :D
It might have been said in jest in part but really it's not that far fetched.
Big-O is a statement about the limit of a function as the input goes to infinity. f(x) = O( g(x) ) is the statement that the limit (f/g)(x) exists as x goes to infinity. f(x) = o( g(x) ) is the stronger statement that the same limit exists and is equal to zero.
You'll get a similar reaction to if you try to show people who won money on crypto art or tesla that they are lucky early members of a pyramid scheme.
And the usual argument is, "well, it's understood..." which is a terrible argument because the whole notion of ignoring constant factor C is that it's supposed to go to the noise floor as n approaches infinity. So if you're going to claim that we didn't mean n approaches infinity, we meant "n fits into a CPU word and the data set fits into memory" then you're trying to have it both ways and that's pure nonsense.
If you want a better overview of algorithms for practical reasons, you can use o(), O(), average case and ammortised complexity, or just provide histograms of runtimes for algorithms you're comparing.
result=None
for i in 0...INT_MAX
if i*i==searched
result=i
return i
and for i in 0...searched
if i*i==searched
return i
The first algorithm is O(1), the second is O(sqrt(searched)). Despite this, the second will clearly be faster in actual execution time. However, if your number range changes from 0-100 to 100,000,000-250,000,000 , the former will still take the same time while the latter will take a lot longer. Now, in this example, this is quite obvious, but in the real world, you might encounter cases where a quadratic complexity solution is completely fine, until you have a lot of data and then suddenly your code slows to a crawl [0]. That's why we need computational complexity - it was never designed to perfectly measure or predict execution speed. This is also the reason constants are dropped in the notation.For real world performance, benchmarking is the key. Computers are very complex beasts and there are a lot of potential speedups or slowdowns you might never think of - memory bank order, thermal throttling and compiler optimizability can drastically change the results, just to name a few. Computational complexity is totally fine as an angle to find new possible optimizations, but in the end, you need to compare it to the other approaches and see what actually works.
> to find the square root of an int:
so the value can't exceed INT_MAX :)
Overall, I know the two algorithms aren't perfect, but they're a simple minimal example to show the difference between complexity and runtime.
Only it's not enough. When you benchmark an O(N^2) algo may seem fine but then three years later the data has changed and is now an order of magnitude larger. So you need not only know your data, you also need to spend time thinking about how large it can become.
So you start with benchmarking your current data. You still need to always think about how your data will grow.
True. But I do highly recommend watching Ben Deane's 2015 talk about testing Battle.net. In it he briefly touches upon benchmarks and estimating algorithmic complexity. It's somewhat difficult to suss-out the details of it. But the short version is basically to run the benchmark with a few different sizes and then estimate the complexity growth from that.
The GTA Online loading screen bug is a recent example of this problem, with its in-game purchasable items growing larger over time: https://nee.lv/2021/02/28/How-I-cut-GTA-Online-loading-times...
> ... you might encounter cases where a quadratic complexity solution is completely fine, until you have a lot of data and then suddenly your code slows to a crawl [0]. That's why we need computational complexity ...
It seems like you're disagreeing only because you pulled out the one quote about benchmarking. But they were just saying that you need benchmarking to bootstrap the meaning of O(...) of an algorithm. That's the point that the original article missed, which is presumably why they described it as key.
If your data changes, and you care about performance, then your data structures and algorithms should be reevaluated.
Functionally, though, I would describe this algorithm as O(N). It's just a straight up linear scan of the problem space. It's unbounded, with a worst case of O(Infinity) and no upper limit on what searched can be, but it's functionally O(N) regardless.
O(1) needs to basically be something like a hash key/pointer to memory address to get the result you're looking for. It will not increase linearly as the problem space increases, whereas your first example increases exactly linearly.
If an algorithm will always take 1 billion years to complete regardless of the input, then it's still O(1).
The behaviour does flatline as n approaches infinity
In the end, it's just not a useful way of modelling the problem.
Floats aren’t real numbers but we treat them like the mathematical object that they model for most purposes.
Of course, being aware of the limits of our models are important, but abusing the tragic finitness of our models to “well actually” someone is generally not helpful.
Mixing real-machine finitude with big-O theory often gives unexpected or unhelpful results. Once in an interview I argued that since hash tables have a finite number of buckets, at large enough scale they really have O(n) worse-case performance. That didn't go over very well. :-)
In the game industry, we use contiguous-allocated intrusive free list memory pools. For enemies or projectiles as an example. Those things live and die (they are removed from the free list and inserted in the "live" list or put back in the free list when they die) in such a way the locality is kept good.
Admitedly I dont have sources nor benchmarks and never did. But its obvious it at least invalidates author's point in the sense that benchmarks gota be done in real life programs.
If you use them as a single-linked free list they are faster than a vector since you only need to fetch a cache line for the object rather than a cacheline for the object and one for the vector storing free objects.
When performance needs to be maximized, games switch to entity component systems and switch from arrays-of-structs to structs-of-arrays. This enables processing all objects as a vector, linearly from start to end, and often without needing to fetch any irrelevant bytes that aren't processed in a given pass. This sometimes also helps utilize SIMD for data spanning more than one entity, which you can't do when using linked lists.
- Use blocks of multiple elements per actual list element that fit your cache line size. Blocks also have the benefit of potentially allowing SIMD processing of your data.
- Allocate blocks out of a vector or some other structure that reduces your actual number of allocation calls. Maintain a free list threaded through this vector.
- Tombstone list elements in their blocks on removal instead of repacking the entire list. This allows for fast deletions and fast insertions at specific locations (in that you can always insert a new block between existing blocks containing only a single element or get lucky and re-use a tombstoned slot).
Note that most of these optimizations trade some memory efficiency for speed. This is a common theme in optimization. Using more memory, but using it more intelligently such that you are potentially accessing less of it and accessing it sequentially where possible.
CPU cores have a special functional block which observes addresses of cache lines requested from memory, detects sequential access pattern, and when detected pre-loads data into caches (including L1d) in advance. The RAM access pattern of std::vector search benchmark is an awesome use case for that thing.
For some simple algorithms which need adjacency information, that’s everything needed. For instance, to compute per-vertex normals, nothing else is required, create an std::vector for the per-vertex accumulators, and iterate over the triangles.
For complicated algorithms which need adjacency information, I build special indices over the same data. To find triangles connected to specific triangle, a hash map with uint64_t keys (two sorted uint32_t vertex IDs in the lower/upper half of the integer) and a structure of two uint32_t values (triangle IDs, good meshes are guaranteed to have exactly 2 triangles for each edge, with opposite winding directions). To find triangles by vertex, a multimap from uint32_t vertex to uint32_t triangle.
For algorithms which need to modify these meshes, sometimes I generate new meshes instead of modifying old ones. Other times I replace erased elements with special values (like UINT_MAX for integer indices), append new elements to the end of the vectors, and when the algorithm is complete I re-index the mesh while removing unused vertices/triangles.
I believe that if someone tried to talk about topology while using Rust as a language for sample code, he/she would use some other representation, because a soup of pointers is a PITA in Rust. It is easier to claim that we will be using a connectivity matrix, or a Vec of edges, and to explain how it works, than to juggle with pointers.
However, in my limited experience with such things I have always found myself needing both walking around and a big-picture list of items. Thus I have always implemented such things as a list of elements and storing indexes rather than pointers.
Array copying is really optimized on current hardware.
i thought that was the point of linked lists: O(1) insertion & deletion.
If all you're doing is "Remove the mth item of the list" takes O(m) time to traverse and O(1) to do the removal if you're starting from the head.
On the other hand, if you already have the pointer to the list element for some other reason, somebody else has already been billed for the traversal and the insert or removal is O(1).
You see linked lists used a lot in e.g. the kernel where the traversal has been paid for to get a pointer to an element that has then been used for a bunch of things before a deletion or insertion happens.
They also have the advantage that in the face of other threads mutating the list, the address of the elements remains constant. If you have a pointer ti an element in an array, and another thread comes along and does an insertion or deletion that moves it, now you have a problem.
Single-threaded performance isn't the only criteria for picking a data structure.
A queue very well might be best implemented as a linked list rather than an array.
For example you have a list of points that define a linestring, and have to make sure that no two points are further apart than some value. It's really simple to iterate over linked list, insert a midpoint into the list, stay at the same step and redo the calculations(because p0-midpoint can still be longer than some value).
But other than that i don't think i had to do many random inserts/removals in non-DB contexts.
one of the benefits of a linkedlist is having the ability to remove an element from a list in O(1) by having reference to an item that is stored in the list. This is allowed since a linked list node can remve itself in constant time. This is a common pattern in linked lists used in C code for example.
javas linkedlists don't allow for this benefit. since in java you have to perform a search for the node to be removed and pay a O(n) penalty on a O(1) operation.
This is a narrow view of the costs of the STL::list container class, not of linked lists in general.
Linked lists are at their best when they are internal storage, meaning the links are part of the class being stored, in order to prevent unnecessary mallocs. STL::list is an external storage container, which automatically compromises some of the potential benefits of a linked list. Linked lists are also best when you don’t malloc to build the list at all, but maintain things already in memory. Linked lists are best used in places where using vectors is impractical or impossible, like the insides of a memory manager.
I don’t feel like timing many inserts using STL::list says a lot about linked lists at all, and what it does say is mostly focusing on the wrong things. Definitely use vector when you can, especially if you’re just comparing container classes.
Also, I only skimmed it, but the article seems to ignore the fact that even for an unbounded/growing array like std::vector, the growth strategy does not free+malloc/realloc on each insertion in practice, as the growth strategy will leave unused capacity for future insertions, and in such cases the cost is just memmove (for simple types at least). Maybe I missed that part, but it seems like an important point worth highlighting.
Right! Yes, that’s part of my point, STL::list isn’t being careful with cache, or with allocations. Really it just rarely makes sense to even compare STL::list to STL::vector as if they’re otherwise equal choice. Usually the choice is (or should be) driven by constraints, not by which has a slight perf edge, right? Inside a memory manager, use of a vector isn’t usually considered a choice. Maybe it’s possible to build a free page vector, but I think isn’t common, and people usually pay the costs of pointer chasing on the free list because there aren’t practical alternatives.
> the <vector> growth strategy does not free+malloc/reallocate on each insertion
Yeah very good point. Does STL::list do the same for the container of pointers? I don’t even know, but maybe it can’t, and maybe the primary perf advantage of STL::vector over STL::list is due to vector’s amortized mallocs?
I think the list vs vector comparison is fair, as most people are taught to “just use the standard library, don’t be a hero, don’t commit a NIH crime”. I have done this myself at times, but in fairness, mostly when discussing adaptations of existing code, where there were bigger structural issues. And it cannot be repeated enough, that vector is far better than list, even at prepends or random insertions, for a surprisingly large number of elements (it was at least thousands of int32-s when I checked it about a decade ago). Typically the justification for a list is that the workload is prepend/random insert heavy, but in my experience there is more often than not a bound on the size that strongly favors vector.
No, if you want a list with amortized malloc, that's a std::deque
Immutable, singly linked lists (aka cons lists) are a different beast entirely, and don't benchmark well in languages with heap fragmentation issues.
Also it would be possible to implement such an approach in a C++ list container where you don't need boxing (the content type is known).
Microsoft has solved most of these issues on Windows, couple decades ago. The feature was introduced in WinXP, and enabled by default in Vista and all newer versions: https://docs.microsoft.com/en-us/windows/win32/memory/low-fr...
I’m not an expert in Linux but I would be surprised if Linux didn’t do the same. RAM costs have plummeted. The losses from RAM usage overhead of LFH became insignificant compared to the issues caused by the fragmentation.
But yes, the default glibc allocator does have dedicated areas for specific sizes of allocation:
> The normal bins are divided into "small" bins, where each chunk is the same size, and "large" bins, where chunks are a range of sizes[1]
But fixing heap fragmentation doesn't make linked lists suddenly fast. You still have your cache bloated with the overhead bytes needed for allocation. List elements can still be allocated non-contiguously, giving the prefetcher a bad day.
[1]: https://sourceware.org/glibc/wiki/MallocInternals#Arenas_and...
Most of the time it is, but note that ArrayList has to occasionally allocate a new array when the list outgrows the array inside it, then copy the list.
When the list gets huge, that operation of reallocating and copying gets disruptive as it puts a lot of pressure on the cache, memory allocation system, etc.
Though in practice I find that unless you are often deleting elements from the middle, modern CPUs will really prefer copying a huge amount of serial data, so an arraylist may still be faster all around then LinkedLists. (The CPU will recognize you moving values in a given direction and will have the best pipeline it can have)
On the other hand, pointer chasing often isn't as bad as you think it might be. That is, modern allocator/garbage collectors often end up laying out the parts of a linked list in a predictable way such that access is somewhat strided and the fetcher is reasonably efficient at traversing the list.
It kinda really is, though. In addition to cache line locality, serial access also benefits from being speculatable. The CPU can't very effectively speculate past a pointer chase (and on arm little cores it doesn't even try), so those become pipeline stalls.
Even if the pointer happened to be close-ish, it's still going to end up being a stall more often than not.
And an allocator / GC is only going to lay out a linked list in any sort of predictable way if the linked list is built up all at once, in which case a linked list is obviously not the right data structure anyway ;)
Tsk tsk. A critical rule for flame wars that also applies to mere discussions among common folk is that you should avoid using absolute terms, as it gives your opponent an easy opening. Instead, couch your statements in vague and wishy washy terms like 'usually' and 'often'.
This also helps when your opponent produces a valid counterexample - you can petulantly retreat to safer ground while grumbling about how of course there are occasional exceptions and then, if you wish, you can sidetrack the argument into a debate about how often it really happens. From there you can feign boredom and exit, think up a snarky, mic-drop conclusion, etc.
- As taught in Raised on the streets of BBSs and USENET
Senior Architect?
No seriously, once you recognize the components of Rhetorical Combat, you can determine if you are going to have a civilized discussion, or if the parties will weasel their way around for sport or entertainment.
If someone attacks my argumentation with uncharitable takes, my discourse with them is over.
I personally would rather arrive that the truth and be wrong, than win an argument and let the truth escape.
ArrayLists grow by allocating a new buffer whose size is a multiple of the current size. This means that as the buffer size gets bigger, the reallocations become less frequent.
The end result is that appending to an ArrayList has constant time amortized complexity, even though it will periodically do increasingly large copies.
https://en.wikipedia.org/wiki/Amortized_analysis
I bombed an interview once because my interviewers didn't understand amortized analysis and I wasn't able to get them to understand it.
But in case of a linked list that large amount of memory doesn't have to be contigous, and you don't have to perform a lot of copying all at once which kills responsiveness.
True! Though in rare cases where that becomes a problem, you are probably better off doing a hybrid solution where you store the data in a relatively small number of chunks or pages. If you have so much data that you are having trouble getting a contiguous allocation, you probably also can't afford the overhead of an additional pointer for each element, which is what a linked list would give you.
> you don't have to perform a lot of copying all at once which kills responsiveness.
I believe there are ArrayList implementations that distribute the copy across a series of operations to mitigate this, but, yes, latency can be an issue for some use cases. (In general, though, my experience is that people overestimate how long it takes to copy a contiguous block of memory.)
The ArrayList will be a nice flat chunk of memory... of pointers to the data, so you'll have to do a lookup for each element anyway. The LinkedList will have two lookups still, but it's not an order of magnitude anymore. Now it's more of a tradeoff between bit slower reads / bit faster updates.
You maybe want something like a linked array, but it's staggeringly difficult to find a scenario where a Java LinkedList or c++ std::list is ever the optimal choice. You should pretty much always start with an ArrayList or std::vector and go from there if/when it ever turns out to be a hotspot in benchmarks or profiling
Except that, the benchmark was storing ints but our production code stored std::function closures. Changing the benchmark to store a simple struct with two shared_ptrs invalidated it, showing that list outperforms vector on as little as 4 elements for head&middle insertions.
I think all blog articles about CPU caches could use to repeat their benchmarks on something that hides a function call (move constructor), an atomic write, and an allocation, just to demonstrate how tight the boundaries are.
What is good about sticking with fundamental computer science is that it provides pretty strong guarantee about what can and cannot happen, while hand-written optimizations are fragile. Even if optimization does work today, one year later the next maintainer may alter data types, or production data volumes may change, and the optimization will start doing the opposite.
Doesn't have much practical application unfortunately since there is almost zero support for things like eytzinger layout in most standard libraries and sorting an array with a eytzinger layout is a bit harder than a non-decreasing layout.
[1] "ARRAY LAYOUTS FOR COMPARISON-BASED SEARCHING", Paul-Virak Khuong and Pat Morin, https://arxiv.org/ftp/arxiv/papers/1509/1509.05053.pdf
Really, the only use case for linked lists is if direct pointers to elements are cached somewhere, and in that case you are probably using a map anyway. IMO linked lists should be replaced with an ordered map for this reason.
That being said, the cases where a linked list is better than an array are very low these days.
One case that seems to make sense is any time you want to do constant-time pops/appends… maybe?
only pertains to languages like C. In a virtual machine language like java, this isn't a property that can exist (there's no such thing as an address - at least as far as the language is concerned).
Vectors and arrays have the problem of needing contiguous memory. If an inner cell can have different size, or worse change it mid work, things get ugly really fast.
They're also more useful on microcontrollers where there's no cache, no prefetcher, etc. Less code needed for the data structure means more free space for business logic, and there's no performance penalty for bad cache locality on a system without any cache. Also a hash table has to hash each input, which is a significant amount of work for many microcontrollers.
ListNode* a = new ListNode('A');
ListNode* b = new ListNode('B');
ListNode* c = new ListNode('C');
a.next = b;
b.next = c;
That's a dumb way of implementing a linked list with no redeeming features outside of explaining the concept.This is a linked list with the same shape as the list above, but with completely different performance characteristics:
int* data = new int[3];
data[0] = 'A';
data[1] = 'B';
data[2] = 'C';
int* links = new int[3];
links[0] = 1;
links[1] = 2;
links[2] = -1;
With some elaboration (say exchange the arrays for a mmap-call, turn it into a tree instead of a list) and you're basically looking at the guts of a DMBS or a file system.It depends on the use case.
If you're doing a lot of navigating over links before you reach the destination, maybe you want them separate. Could also be the data is large, even maybe stored on disk while the links are in memory, or whatever. There's a thousand different scenarios with different optimal arrangements.
It also allows you to keep multiple different lists to the same data, say sorted by different fields in the struct.
Sounds a lot like “draw the rest of the damn owl.” [0] :-)
DBMSs and file systems use (extremely sophisticated) tree structures under the hood. Not sure that “linked lists are useful if the linking topology is much more complicated than a simple line” is a ringing endorsement of them.
( Here's an enjoyably lucid explanation on how to draw such an owl: https://www.youtube.com/watch?v=aZjYr87r1b8 )
You actually don't. You can just keep two lists within the same structure, one for occupied nodes and one for free nodes, and just move deletions to the head of the free nodes list.
Example List:
Data: [ a, 0, c, d, e ]
Links: [ 2, -1, 3, 4, -1 ]
Head of occupied nodes: 0
Head of free nodes: 1
Link shape: Occupied Nodes: 0 -> 2 -> 3 -> 4 -> []
Free Nodes: 1 -> []
To Delete C: data[2] = empty // free data
links[2] = 1 // Repoint 2 -> 1
links[0] = 3 // Repoint 0 -> 3
firstFreeNode = 1
This changes the data such: Data: [ a, 0, 0, d, e ]
Links: [ 3, 2, -1, 4, -1 ]
Head of occupied nodes: 0
Head of free nodes: 2
New shape: Occupied Nodes: 0 -> 3 -> 4 -> []
Free Nodes: 2 -> 1 -> []Yes, and constant time insertions/deletions in the middle of the list, assuming you know the address ahead of time.
This makes them great as backing queues in an LRU cache: given some key/value pairs, have a linked list whose nodes map to each key. Additions to the K/V store gets appended to the LL. This lets you easily remove the oldest n elements in the K/V store, by traversing from the head of the LL. It also lets you efficiently remove arbitrary elements from the K/V store, if you store the addresses of each LL node as values in the K/V, since deletions in the middle of the LL are constant time.
>yet I see them employed in various places all the time
I agree, however, that most LL applications (like the one I just mentioned) are fairly niche. LLs are overused because they’re dead simple to implement and commonly taught in intro CS classes.
This also technically applies to lockless data structures for some high performance code too. But those tend to be much much more tightly tuned for performance and the specific CPU they are intended to run on.
Inserting an element in middle is probably only legit use but it is kinda rare to have large enough data that you frequently insert in mid
When the use case requires insertion/removal from the middle of the list. An LRU cache is about the most common use case I've run across. (The "LRU" part requires it to often bump an item to the end of the list, so LL's excel here, as we can shift the element's position in the list in O(1) time. An LRU structure would normally pair the LL with a HashMap, which maps the cached item to its LL entry, so that we've also got a O(1) search into the LL, and don't hit the problem in the article.)
> yet I see them employed in various places all the time, by competent engineers who are definitely aware of their limitations.
I see them employed very, very rarely. Vectors¹ are by far more common in every codebase I've ever worked on. (And should be one's default, IMO, if all you need is a container of stuff that you're going to iterate over, which is the usual case, for the reasons in the article.)
¹ignoring cases where a hash(map|set) is required; then our codebase uses that, b/c that's what's needed.
std::deque will invalidate iterators in some cases. Since my example above was an LRU cache, the cache's removal of an item from the middle of the list (to shift it to the end) is one of those actions that would invalidate iterators in a std::deque.
(Not to malign std::deque: it is a useful datastructure in its own right, and if you need queue-like semantics (e.g. cheap remove from front), it's a better choice than a LL, and both are better than vectors for that purpose. I find deque's internals to be a bit weird … my goto would normally be a ringbuffer, but the STL provides deque instead, so eh, it's good enough, if a bit odd IMO.)
Your question is more accurate than you realize! Because the answer is... the past.
Up until about the 486/50MHz era, CPUs and memory were attached to each other; one CPU cycle was approximately equal to one memory access. I don't mean that you could reach out to RAM in exactly one cycle and get a value, there were still some CPU caches and other considerations, but it was much closer to that ideal than on modern systems. And if you go back in time even farther, that actually was the case (Commodore 64, for instance).
(I don't know if there was ever a "true" 486/66 system, but I remember that as the CPU/clock speed where the CPU finally and definitively detached from RAM speed because that was generally a double-clocked 486/33 from the RAM's perspective. I can't quite remember the marketing term that was used. It's a dead term now because everything works that way.)
In those circumstances, traversing a linked list was not necessarily that much more expensive than a vector, and you could win on the other things a linked list can do faster than a vector, like insert in the middle, especially an insert in the middle when you were traversing the list anyhow. You could also win on a linked list containing a relatively large value organized just by pointers; a sort on the linked list manipulating just the pointers could win versus a sort that was trying to move around larger values constantly during the sort. And so on.
When memory accesses aren't hundreds and hundreds of CPU cycles, when your hardware doesn't have prefecting implemented, when your pointers aren't 64bits wide, when you aren't on modern systems essentially designed to make vector-based access go zoom, linked lists make a lot more sense.
This is how they got embedded in curricula so hard that they are taught to this day. They used to be a very important data structure. Now they're an antipattern. It happened gradually, though, and it doesn't help that linked lists are just about the easiest non-trivial data structure to teach and I'm not sure they could get removed from the standard curriculum for that reason alone.
"One case that seems to make sense is any time you want to do constant-time pops/appends… maybe?"
Another problem linked lists have is that if you know that's what you're going to do, you can build vector-based solutions to that problem that work just fine. Vector-based stacks, for instance, are trivial, and O(1)-amortized for push and pop, which is good enough in practice. (A bit more care is needed than an only-growing vector but IIRC it can be done.) You need not just something linked lists are better at, but some bizarre cocktail of all the things they're just barely better at, and you still need to construct a win out of the combo. It's nearly impossible. Not quite impossible. I assume without looking that the Linux kernel still has some linked lists for good reason, as I'm sure if they could win on performance by removing them they would. But very hard.
Every pop moves the whole list unless you get fancy an implement it as an array with head and tail pointers and grow logic.
If you keep a tail pointer inserts are likewise O(1).
You rarely traverse them.
(The best optimization you could do in such a case would be to allocate multiple slots per pointer, to amortize the malloc/free time. Then you'd want to run benchmarks to tell what the optimal amount of slots would be. If a vector is optimal, as I strongly expect it would be, the answer would come out to be, "all of them".)
Modern processors move chunks of RAM around really quickly. They're really optimized for it. It is one of the major things I'm referring to when I say our systems have been optimized for C. I often wonder about what an architecture designed in a world where linked lists were dominant would look like. However, it is certainly not this world.
The term I remember was 486DX2-66 , so was it DX2?
How practical is it to implement a keyword/type that (portably) represents the L1/L2/LN cache size? Suppose I am implementing an algorithm or data structure and I really don't care what the $BLOCK_SIZE is, as long as it fits reasonably nicely in one of the lower caches as a sane default. It would be nice if I could do this with a magic keyword rather than hardcoding a default (1KiB) and forcing the end user to tune runtime params according to their hardware. Bonus if this can be used for static/stack allocations too.
From 2012. std::deque does very well.
Suspect the difference is fairly anaemic.
Usually if you're choosing data structures, you pick a "good enough choice," (any of the three).
Or, if it matters, you pull out your profiler and pick the "exact" right one.
Not sure what kind of vectors are assumed in the article.
The point is, it doesn't matter if you know the length of the list, you still have to examine all the elements.
Back then, there weren't any list like data structures backed by arrays, like List from C# and Vector from C++.
But learning linked lists was a good thing, we also had to learn about pointers and how memory is layed out, so we also knew that sequential access to memory is faster. Also, learning about stack vs heap, CPU caches, made a big difference in how we wrote programs and how we continued to write programs 25 years later.
So I think I was lucky starting with Pascal and C, continuing with C++ instead of starting with Python or Javascript.
> The theorical complexities are:
> For list: O(n)
> For vector: O(n)
... but then goes on to compare running times of list vs vector and notices vector is 90 times faster than list for many operations, mulls over cache locality and whatnot. Did he seriously expect O(n) to give him a way to compare running times? Big-O is about asymptotic behavior, not a way to compare running times in milliseconds between two implementations.
That is, "how many seconds does this take?" cannot be answered with "oh, it's O(N^2)".
The author seems very confused about what he is trying to argue.
Often schools teach you to only focus on the big O and ignore the constant multiplier. Those same schools then teach vectors and linked lists as the two main data structures. They talk about the cases where one has an obvious strength over the other, such as inserting into the middle, or using an index to access an element in the middle. However, they tend to skim over scenarios where the big O notation is the same but one has an advantage due to the constant multiplier, leading many students to come away with the impression that big O notation is all that matters.
That's news to me. Which schools teach you that? Where I studied CS, algorithmic complexity and Big-O was taught in Graph Theory (Discrete Maths), and no attempt was made to imply it was about run time in milliseconds.
The problem might be that there's plenty of self-taught programmers writing blogs that talk about Big-O without understanding what it means, and people who "learn" about it from said blogs.
There's no theoretical mismatch with reality here. The only confusion might lie in the minds of self-taught programmers.
I just want to point out that your comment could easily be perceived as a ding against self-taught programmers. Many self-taught programmers (which I will define as ones that have not had a formal CS degree) read extensively. Many use their intrinsic motivation to really dive in. Also, many are successful. Bill Gates is one example.
Yes, there are programmers of all kinds that have a tendency to write sloppy blog posts, to make overconfident and inaccurate statements, to forget things they've read, to hack their way around, and so on. There may even be a statistical correlation between self-taught programmers and such behavior. But I'd suggest we points out those behaviors when they are a problem rather than make assumptions about their educational backgrounds.
I may be misunderstanding you?... In my study and practice of complexity theory, algorithms related to graph theory are an object of analysis of complexity theory, not a conceptual foundation for it.
> Yet another subject related to computational complexity theory is algorithmic analysis (e.g. Knuth (1973), Cormen, Leiserson, and Rivest 2005). Like computational complexity theory, algorithmic analysis studies the complexity of problems and also uses the time and space measures `t_M(n)` and `s_M(x)` defined above. The methodology of algorithmic analysis is different from that of computational complexity theory in that it places primary emphasis on gauging the efficiency of specific algorithms for solving a given problem. On the other hand, in seeking to classify problems according to their degree of intrinsic difficulty, complexity theory must consider the efficiency of all algorithms for solving a problem. Complexity theorists thus make greater use of complexity classes such as P, NP, and PSPACE whose definitions are robust across different choices of reference model. In algorithmic analysis, on the other hand, algorithms are often characterized relative to the finer-grained hierarchy of running times `log_2(n)`, `n`, `n log_2(n)`, `n^2`, `n^3`, ... within P.
Source: https://plato.stanford.edu/entries/computational-complexity/
Note: I've done some light edits so the mathematics are roughly in LaTeX syntax; e.g. `_` for subscripts.
Sort of, do you assume you found the right node already, then it's O(1).
If not, it's O(n/2).
Also, pedantically O(n/2) is the same as O(n).
The same applies here. When we are studying a topic, context (scale in particular in this case) matters a lot. Just like our physical world, and how classic mechanics and quantum mechanics are so different.
Or, perhaps, this is saying (again) that one should code then tune? Pick data structures that facilitate the design you’re trying for rather than for theoretical elegance? Chips are cheap while developers are not, and having a cleaner, saner design is a win. Unless your software price per core is so high that people won’t license more.
It may use less power as it is spending more time stalled, waiting for memory. But it could be still using as much time as the OS scheduler is willing to give it.
> - For list: O(n)
> - For vector: O(n)
> Surprisingly enough, the vector version is almost 90 times faster than the list version for 8K. How can we explain this big difference?
That does not contradict the theoretical complexities at all. The article didn't show any "practical" complexities contradicting the theoretical ones.
I do not mean to offend, but it looks like the author doesn't even understand what the O() notation means. The notation does not imply that all O(n) functions take the same amount of time. Multiple functions can scale linearly and still be different. In fact O(n) = O(1,000,000*n).
[0] Seems there are a lot of problems that stem form Comp Sci students skimming the syllabus and not actually reading the material.
In the first table:
Benchmark Time CPU Iterations
-------------------------------------------------------------
BM_ListFind/8 2824 ns 2825 ns 247103
[...]
BM_ListFind/8192 3758778 ns 3758624 ns 204
[...]
the last column makes no sense. Is that an error sorting the data or I'm misunderstanding what it mean?Furthermore, the author is masking the true cost of an array resize, which often happens in a running system where a finite array is completely full and needs to be resized to append an additional element. This is the scenario where linked lists are most useful.
Resize point is fair but we still options here. A segmented approach for very large collections may also help, with tuning knobs of array size / segment. The smallish top level ds maintaining segment ptrs will be super hot and very likely ever present in L2.
It really all depends on how many items are involved and how the data needs to be accessed and used.
So given that this is the case, using immutable data structures with O(log n) performance isn't "any" different than using the mutable ones.
You can have a 2x difference in main memory performance between machines these days.
Ok, not a great pun, but I have always used arrays when possible, even for stuff that needs inserting. I just assume I won't have a million elements to shuffle.
99% of code doesn't need to be efficient, but the maintenance cost tends to relate to the sheer amount of code and it's comprehensibility, and this cost is the one to optimise for, not speed.
For the other 1% that you identify with profilers, go with the more optimal data structure, and accept the reduction in clarity and purpose.
The very first, biggest impact performance metric is the algorithm used — anything besides that will be meaningless given a bad algorithm.
Of course someone will misappropriate it and use it as the core of some terribly thought out data handling app with billions of items, but at least anyone with a clue will be able to figure out why it’s terrible later.
And if no one with a clue is around, then not like there was any better outcome going to happen except by sheer luck anyway.
My default would be to generate maintainable code first, and worry about performance when it matters, but a knee-jerk 'linked lists are slow so avoid' approach is almost always the wrong way of approaching it. Choose the correct data structure and algorithm for maintainability, then optimise if it's too slow should be the default in my opinion.
My point is that worrying about this for a code path that only happens once at startup is silly, but in the main loop of a critical performance code path, then sure, go ahead. The mistake would be to use vectors all over the place instead of lists 'because linked lists are slow' when they have other benefits (e.g. cost of insertion, address of elements don't change etc) which may adversely affect the readability of the surrounding code if you by default use vectors.
Anyhow, just my opinion of course.