News for hackers. Stuff that matters.
Re:New Grad vs. Senior Dev by corysama on Saturday March 28, 2020 @12:58PM (#22712394)
It’s because Big O is Computer Science. Cache effects are Software Engineering. Professors of CS do a fine job of teaching CS. They even briefly mention that there is a implicit constant factor k in O(k n log(n)) and then they never mention it again. They certainly don’t mention that k can easily vary by 128x between algos. AKA: 7 levels of a binary tree. Or that most of the data they will be dealing with in practice not only won’t be infinite, but will actually be less than 128 bytes. Or, that even with huge data and an proven-ideal O() algo, there is often 10x speed-up to be had with a hybrid algo like a b-tree instead of a binary tree. And, another 2-10x with SIMD vs scalar. 100x isn’t infinite, but it’s still counts.

So, grads listen to their CS professors and that’s what they know. It’s not until they get lectures from greybeard software engineers that they learn about the reality algos and not just the idealized algos.

Re:New Grad vs. Senior Dev by MaxBarraclough on Saturday March 28, 2020 @02:50PM (#22713386)
> briefly mention that there is a implicit constant factor k in O(k n log(n)) and then they never mention it again

A fine concrete example of this is the Coppersmith–Winograd algorithm (and its derivatives), a matrix multiplication algorithm with impressive complexity properties, but which in practice always loses to the Strassen algorithm, despite Strassen's inferior complexity. [0][1][2]

(Aside: the Strassen algorithm is pretty mind-bending, but also easily shown. If you've got 22 minutes spare, there's a good explanation of it on YouTube. Perhaps there's a more dense source elsewhere. [3])

> It’s not until they get lectures from greybeard software engineers that they learn about the reality algos and not just the idealized algos.

To mirror what some others are saying here, students should also be taught the realities of cache behaviour, SIMD-friendliness, branch prediction, multi-threaded programming, real-time constraints, hardware acceleration, etc.

[0] https://en.wikipedia.org/wiki/Coppersmith%E2%80%93Winograd_a...

[1] https://en.wikipedia.org/wiki/Strassen_algorithm

[2] https://en.wikipedia.org/wiki/Computational_complexity_of_ma...

[3] https://www.youtube.com/watch?v=ORrM-aSNZUs

Re:New Grad vs. Senior Dev by dwohnitmok on Saturday March 28, 2020 @03:54PM (#22713860)
Even Strassen can be overly expensive for reasonably sized matrices. Almost all matrix multiplication algorithms have a threshold only beyond which do they use Strassen.

Although apparently that threshold can be lowered (http://jianyuhuang.com/papers/sc16.pdf), but even then it's a matrix that's several hundred columns by several hundred rows large.

Some CS classes explicitly use Strassen to teach the realities of asymptotic vs wall-clock time complexity, challenging students to come up with a hybrid matrix multiplication algorithm that performs the fastest and switches at the best thresholds of matrix size.

Re:New Grad vs. Senior Dev by msla on Saturday March 28, 2020 @04:45PM (#22714188)
> To mirror what some others are saying here, students should also be taught the realities of cache behaviour, SIMD-friendliness, branch prediction, multi-threaded programming, real-time constraints, hardware acceleration, etc.

Which would have the positive knock-on effect of the textbook being sufficiently obsolete every year or so that the students could no longer trade it in for credit, saving the bookstores money!

More seriously, that knowledge (at least once you attach numbers to it) has a shelf life, and not a very long one. Teaching big-O analysis means the knowledge is timeless, which any good theoretical knowledge is, and moving more towards practice would force the professors to keep on top of the state of the art, and the state of the mainstream, of hardware design in addition to everything else they're doing.

Re:New Grad vs. Senior Dev by temac on Saturday March 28, 2020 @09:22PM (#22715597)
> Which would have the positive knock-on effect of the textbook being sufficiently obsolete every year or so that the students could no longer trade it in for credit, saving the bookstores money!

That's naive. E.g. SIMD is here since a good time and going to stay. So are GPGPU, with now quite similar architectures for tons of chips.

And Computer Science can actually be about science for real computers, and computers are not 8086 nor PDP11 anymore, and have never been a turing machine. So there actually is some existing generic CS and ongoing research that cares about cache effects and so over. Maybe it is applied CS if you want, and some kind of pure CS should not care about that, but I really don't see what should be the criteria to decide which is what anyway, so IMO there should not be any (but I do not mean that all research should care about e.g. cache effects, just that it is not really useful to attempt to distinguish between those which do and those which don't).

We don't teach advanced math by only showing what was done at e.g. the beginning of algebra. Neither should we stick to only basic subjects in computer science.

Re:New Grad vs. Senior Dev by MaxBarraclough on Saturday March 28, 2020 @08:11PM (#22715290)
> textbook being sufficiently obsolete every year or so

I wasn't very clear on that point, but didn't mean to suggest it be the same textbook. These other topics deserve courses and books of their own. The algorithms lecturer should be careful to emphasise the limitations of complexity theory though.

> that knowledge (at least once you attach numbers to it) has a shelf life, and not a very long one

Plenty of long-lived principles to be learned there, even if the particulars change over time. Caches are still going to be around in 10 years time.

Re:New Grad vs. Senior Dev by Tomis02 on Saturday March 28, 2020 @09:25PM (#22715611)
Without knowledge of the hardware your software runs on you're likely to be one of those people who uses lists instead of vectors without really understanding the difference. Also, as other people said, the shelf life for that knowledge is actually pretty long. Hardware will always be your platform, no matter how many layers of abstraction you have in between.
Re:New Grad vs. Senior Dev by LolWolf on Saturday March 28, 2020 @09:23PM (#22715599)
> To mirror what some others are saying here, students should also be taught the realities of cache behaviour, SIMD-friendliness, branch prediction, multi-threaded programming, real-time constraints, hardware acceleration, etc.

They are in many places I'm aware of. At least, as an EE (at Stanford, but I've heard MIT and several others do the same), I had to take a digital system design class, but the majority of the class was spent on performance engineering. In fact, the very first (actual) project of the class was to take a 10-line piece of C code, which applies a simple filter in real time to a video, and make it performant. The initial code runs at around .5 FPS.

Our resulting performant code was, of course, many times larger (I think it might have been ~150 lines), but it ran incredibly quickly (110 FPS, iirc), by doing crazy compiler tricks and often calling ASM from within the C code, even though the asymptotic (big O) performance was exactly the same.

For context, this is not just a digital systems thing (my work is in mathematical optimization theory and my undergrad was in photonics and physics), but I do know that this class is not a requirement for CS since it's potentially too hardware oriented. The classes exist, but I'm not sure people are taking them.

Re:New Grad vs. Senior Dev by MaxBarraclough on Sunday March 29, 2020 @10:57AM (#22719503)
> They are in many places I'm aware of.

Yes, sure. I hadn't meant to imply otherwise. The pure-algorithms lecturer needn't cover these other topics in detail in their course, but should be careful to emphasise the uses and limitations of complexity theory.

> this class is not a requirement for CS since it's potentially too hardware oriented

I don't see the sense in this. Computer scientists publish work on applying GPU acceleration, as they should - that's not electronic engineering work they're doing. We could quibble about whether it's computer science of software engineering.

Re:New Grad vs. Senior Dev by LolWolf on Sunday March 29, 2020 @07:53PM (#22723858)
> I don't see the sense in this. [...] We could quibble about whether it's computer science of software engineering.

I agree, I'm not sure why this is the case either, just my idea as to why it may not be a requirement. (Some part of the class does involve writing a good chunk of a 5-stage RISC processor based on MIPS, but this was still relatively straightforward with just a basic understanding of digital logic.)

Re:New Grad vs. Senior Dev by herbstein on Saturday March 28, 2020 @05:11PM (#22714359)
I always loved my algorithms and datastructures professor talking about Brodal queues[0] - a datastructure named after him. They're super interesting from a theoretical point of view, but they're not useful for anything.

[0] https://en.wikipedia.org/wiki/Brodal_queue

Re:New Grad vs. Senior Dev by MaxBarraclough on Sunday March 29, 2020 @05:00PM (#22722673)
That's a great example. Hadn't heard of it before.
Re:New Grad vs. Senior Dev by dilyevsky on Saturday March 28, 2020 @01:03PM (#22712433)
Every good cs course has a section on cache aware algorithms. And i call bullshit that constant factor is not mentioned too
Re:New Grad vs. Senior Dev by corysama on Saturday March 28, 2020 @01:25PM (#22712629)
It wasn’t taught to me. And, in my previous job I interviewed many dozen fresh grads. One of my questions was “How much slower is it to sum integers in a trivial linked list vs. a trivial array?” 90% answered “Umm... I don’t know. 2x?” When asked why, they all said “1 op to sum the int +1 op to traverse the pointer.” It was amazingly consistent.
Re:New Grad vs. Senior Dev by onekorg on Saturday March 28, 2020 @02:25PM (#22713159)
The answer could be 2x. Let's say you're in a 64 bit platform. Your linked list nodes consist of a next pointer and a 64 bit integer.

If your linked list nodes are all allocated sequentially in memory then it'd only be 2x as slow as an array of 64 bit integers.

But maybe it's not fair to call sequentially allocated linked list a "trivial linked list".

Re:New Grad vs. Senior Dev by koala_man on Saturday March 28, 2020 @04:20PM (#22714037)
This kind of CS-based rationalization is arguably another aspect of what the article comments on. I wrote a benchmark and found the difference in this case to be 3x-3.5x.
Re:New Grad vs. Senior Dev by willberman on Saturday March 28, 2020 @01:40PM (#22712784)
I can see how this wouldn’t be covered in an undergrad cs education. I took only a single computer architecture class which was extremely limited in scope. The only reason I knew about vectorization during undergrad is because a friend mentioned it to me once.
Re:New Grad vs. Senior Dev by temac on Saturday March 28, 2020 @09:25PM (#22715612)
The basic speed up will not even be because of vectorisation. It will be because of caches.
Re:New Grad vs. Senior Dev by willberman on Sunday March 29, 2020 @10:42AM (#22719368)
Are you saying that the majority of the speed up is from caches and then there's a secondary, much smaller, speed up from the vectorization? Or are you saying all the speed up is from caches and I'm off base here with vectorization.
Re:New Grad vs. Senior Dev by joshlemer on Saturday March 28, 2020 @02:44PM (#22713339)
What would be the kind answer you're looking for?
Re:New Grad vs. Senior Dev by corysama on Saturday March 28, 2020 @03:12PM (#22713560)
The question had 2 goals:

1) Do you think about cache at all or is it just something you heard mentioned as important that one time?

2) It's a good lead-in to discussing the effects of cache in algorithms. How that conversation goes helps me to understand how that person thinks and discusses complex problems.

A good answer would be "I'm not sure, but probably way, way slower because linked list can point all over memory but arrays cache really well."

An excellent, A+ answer would be "In the best case it might not be too much slower if you have an intrusive linked list is arranged sequentially in memory like an array like onekorg explained. But, in practice most will be 20-200x slower because they are usually implemented as pointers to nodes containing pointers to data and each piece is allocated piecemeal in a already fragmented heap. Uncached memory reads can take 100+ cycles and summing an int is not enough work to hide that even if the CPU speculatively prefetches."

I mainly expect a surprised reaction that they could be so slow and looked forward to the follow-up discussion.

Re:New Grad vs. Senior Dev by TheOtherHobbes on Saturday March 28, 2020 @04:51PM (#22714236)
I sometimes wonder if CS should be renamed Computer Pseudo-Science. I blame Knuth and (mostly) Dijkstra for propagating the falsehood that you can estimate the performance of real code on real hardware with a some elegant academic blackboard math which gives you the wrong answer for many practical applications.

It's not that Big O isn't useful - it's that it's taught as a set of "proofs" which somehow make it appear objective and "correct", when in reality performance is at least as dependent on cache architecture, median size-of-n, memory bandwidth, and other implementation details.

Anyone who graduates CS without having been taught this very forcefully - preferably during a practical project - should be refunded at least some of their course fees.

Re:New Grad vs. Senior Dev by herbstein on Saturday March 28, 2020 @05:15PM (#22714389)
But Big O was a lot more directly correlated when the CPU wasn't doing "magic" optimizations on its own. It was still estimations with an invisible constant factor, of course,
Re:New Grad vs. Senior Dev by koreth1 on Sunday March 29, 2020 @02:32AM (#22716875)
This is a key point. The gargantuan performance difference between main memory and the CPU cache (or indeed, the existence of significant CPU caches at all) happened well after big-O was firmly established in the CS curriculum.
Re:New Grad vs. Senior Dev by Cpoll on Saturday March 28, 2020 @09:09PM (#22715550)
> But, in practice most will be 20-200x slower

My A+ answer is "My guess is [x] but instead of speculating we can create a test to discover the performance. [Describes test]."

Koala_man above says:

> I wrote a benchmark and found the difference in this case to be 3x-3.5x.

The actual number depends on a lot of things, of course (language, architecture, test methodology...), but it is possible that your 20-200x A+ answer is incorrect.

Re:New Grad vs. Senior Dev by temac on Saturday March 28, 2020 @09:29PM (#22715623)
There is no answers if you don't specify tons of parameters, like the number of element, prior memory fragmentation, etc.

200x can be a reasonable outcome. So can be 3x in other conditions.

As a rule of thumb I now consider that a completely random memory access is on the order of accessing 1000 sequential bytes.

Re:New Grad vs. Senior Dev by james_s_tayler on Saturday March 28, 2020 @09:20PM (#22715591)
Question... What kind of software do you mainly work on?

I'm guessing it's not CRUD apps.

Re:New Grad vs. Senior Dev by corysama on Sunday March 29, 2020 @12:55AM (#22716535)
High end 3D mobile games. We regularly measured direct correlations between performance and revenue. Higher performance meant a larger install base of devices could run the app with better responsiveness and lower battery burn. Thus, higher retention and engagement. Thus higher monies.
Re:New Grad vs. Senior Dev by jodrellblank on Saturday March 28, 2020 @03:38PM (#22713756)
At a guess, some understanding of this article[1] - if a CPU instruction scaled up to human time takes 1 second then a Level 1 cache lookup takes 2 seconds and a main memory lookup takes 4 minutes.

Imagine for the array it's 1 CPU instruction to load a value, 1 to load the next value, 1 to add them, and one to store the result, that would be 4 instructions per sum; ideally the array would stream into the CPU after a single main memory lookup delay up-front, and then be 4 instructions per pair, summed as fast as the CPU can loop.

The linked list at worst needs an imaginary 1 CPU instruction to load a value, 1 to load the pointer value, 1 to reference the pointer, a delay of 2 seconds to get that value from L1 cache - missed, it's not in cache - 240 seconds stalled waiting for main memory, 1 to add, 1 to store the result. Worst case, >240x slower.

The linked list is not guaranteed to be in contiguous memory, but it might be, so the cache might have the right data in it. The linked list is 50% data, 50% metadata, so the cache is half wasted / can hold half as much data, and if the linked list is coming in from a big memory read quickly, half the bandwidth is carrying pointer addresses not data, so the throughput is halved for that, too, and the processor cycles were already able to happen much faster than the main memory bus max speed. If it's not contiguous memory, you don't know in advance where it is to request all the right memory areas at once - not until you read sequentially to the last item pointer and find there are no more.

Maybe if they are both small, both in contiguous memory and go into Level 1 cache after a single main memory delay, it could be only ~2x time, but the more data there is overall, the more chance the linked list will bust the cache or be discontinuous in memory. And on the plain array side, it might be possible to speed up with SIMD/SSE instructions to spend fewer cycles adding and storing per element, which the linked list approach might not be amenable to at all[2], then best case might be ~4x slower, worst case ~500x slower.

[1] https://www.prowesscorp.com/computer-latency-at-a-human-scal...

[2] https://stackoverflow.com/questions/10930595/sse-instruction...

Re:New Grad vs. Senior Dev by cjfd on Saturday March 28, 2020 @01:44PM (#22712809)
O dear.... Am I happy that I never studied computer 'science'..... On the other hand, there must be smart computer science students and/or smart places of education where actual learning about processor caches and the like takes place.....
Re:New Grad vs. Senior Dev by lghh on Saturday March 28, 2020 @02:08PM (#22713005)
I don't think anyone answering in this way would be "not smart" like you seem to imply.
Re:New Grad vs. Senior Dev by dwheeler on Saturday March 28, 2020 @01:13PM (#22712520)
Mentioned, yes, but often not learned. In many situations the only thing that matters is the constant factor. If the number of data items is relatively small, the difference between N log N and and N squared may be completely dominated by the constant factor. In addition, there is the challenge of maintaining the code later and making sure it's correct.
Re:New Grad vs. Senior Dev by vvanders on Saturday March 28, 2020 @01:18PM (#22712567)
Yup, so much truth here.

Take a look at Real-time Collision Detection[1]. I takes a great look at both algorithmic complexity and cache awareness. That's how it should be done.

[1] https://www.amazon.com/dp/1558607323

Re:New Grad vs. Senior Dev by eyegor on Saturday March 28, 2020 @06:04PM (#22714672)
I've looked before, but I've never seen a class dedicated to practical algorithm design. Being able to reason about cache layout, context switch costs, branch prediction behavior, simd refactoring, and basic compiler level optimizations will result in much more performant code. In the real world people often write complex algorithms which operate on structs/classes instead of primitives. This means there's a huge performance hit just from pointer traversal in a hot path, especially if someone naively does math across data inside heap objects. You can easily write a fancy dynamic algorithm approach which has theoretical O(k*n) performance which takes forever in the real world due to abstraction traversal. If you're doing more than one operation, it's often a massive performance boost to cache all your object pointer evaluations into primitive arrays, do simd operations on them, and then populate the objects at the end.

Does anyone have a good textbook suggestion for cache/simd aware algorithm design? I've seen plenty of papers that cover single examples but never something the scope of a book.

Re:New Grad vs. Senior Dev by willberman on Saturday March 28, 2020 @01:36PM (#22712740)
When I took data structures and algorithms, I believe almost every single lesson was concluded with the caveat that constants are important.
Re:New Grad vs. Senior Dev by lonelappde on Saturday March 28, 2020 @01:20PM (#22712586)
That's an arbitrarily restrictive view of computer science. It's like saying teaching physics ignoring friction perfectly fine.
Re:New Grad vs. Senior Dev by pretendscholar on Saturday March 28, 2020 @01:36PM (#22712744)
Yes you should ignore the details of friction for the first few semesters.
Re:New Grad vs. Senior Dev by lonelappde on Saturday March 28, 2020 @01:39PM (#22712774)
For how many semesters? All the way through to graduation?
Re:New Grad vs. Senior Dev by eternauta3k on Sunday March 29, 2020 @12:30PM (#22720259)
Funnily enough, you only solve problems with friction in your first semester (and maybe a little bit afterward when you study waves).
Re:New Grad vs. Senior Dev by rriepe on Saturday March 28, 2020 @01:43PM (#22712805)
No. You wouldn't want people slipping on stage.
Re:New Grad vs. Senior Dev by alasdair_ on Saturday March 28, 2020 @05:11PM (#22714358)
> No. You wouldn't want people slipping on stage.

Since all the students will merely be spheres of equal density, that shouldn’t matter much.

Re:New Grad vs. Senior Dev by bialpio on Sunday March 29, 2020 @03:03AM (#22716981)
I guess in that case you can also skip teaching them about angular momentum!
Re:New Grad vs. Senior Dev by saagarjha on Sunday March 29, 2020 @04:06AM (#22717226)
They’d still have angular momentum; it just wouldn’t be immediately apparent.
Re:New Grad vs. Senior Dev by Jasper_ on Saturday March 28, 2020 @02:34PM (#22713235)
Models are easy when you turn every cow into a sphere. But physicists never believe their models respect the real world. Computer Science should be about the Science of Computers, not hypothetical models acting on hypothetical architectures.
Re:New Grad vs. Senior Dev by duck2 on Saturday March 28, 2020 @02:46PM (#22713354)
Computer Science existed long before there were computers. I think the Science of Computers you mention is known as Computer Architecture.
Re:New Grad vs. Senior Dev by msla on Sunday March 29, 2020 @03:27AM (#22717075)
> Computer Science existed long before there were computers.

No, various bits and pieces of it did, but not the whole, coherent field, which is motivated by the existence of computers.

Re:New Grad vs. Senior Dev by msla on Saturday March 28, 2020 @04:46PM (#22714195)
> Computer Science existed long before there were computers.

Not in its current form, and not if you define "computers" with a sufficiently broad net.

(Or broad loom, tipping a hat to Jacquard... )

Re:New Grad vs. Senior Dev by jrs95 on Sunday March 29, 2020 @05:24AM (#22717513)
And then we seriously overvalue these "core CS skills" for a lot of positions, many of which will not involve actually using any of it at all. I can't tell you how many people I've worked with that could tell you a bunch of Big O crap off the top of their heads but all they were working on was web apps and they couldnt seem to remember that running an ORM query in a loop is a bad idea. The way the U.S. uses college education as a prerequisite to employment but also seems to not be very good at teaching stuff that's actually relevant to the work you allegedly need the degree for is very disturbing to me.
Re:New Grad vs. Senior Dev by fchu on Saturday March 28, 2020 @04:08PM (#22713947)
As a side note: O(k n log(n)) and O (n log (n)) have exactly the same meaning for k non-null constant by the definition of big O notation. The more you know!