Hacker News new | past | comments | ask | show | jobs | submit | SunnySkies's comments login

You're misunderstanding the advice about contiguous. It's not that it's more likely to stay in cache, but if you're accessing data sequentially it's more likely the data you're going to access next is already in cache.

Most (all I've read/looked at) benchmarks in Java have data structures backed by linked lists utterly smashed by things implemented by an array.

There was in the last year or two a good c++ talk where a game or game engine changed their storage to be more array like and got roughly a 30% boost in performance. Memory locality is usually king, which is why linked lists are rarely used.



If you already know your data (which must be the case with arrays anyway), you can simply pre-allocate a pool from where the elements of the linked list will be taken. This guarantees that elements will be allocated from the same region in memory. It is a simple technique that I use whenever I believe that a linked list will be needed in a high-performance context.


Vectors are also usually more memory-efficient, period. A singly-linked list uses a pointer for each element an a doubly-linked uses two, while a vector has constant [a pointer and two integers (size and capacity)] overhead. (Unless the vector was dynamically resized and isn't near capacity.)


> if you're accessing data sequentially it's more likely the data you're going to access next is already in cache

Why?


Cache consists of cache lines (usually 64 bytes). If an element in the list is smaller than the cache line a whole line is put in the cache anyway. This means that data is cached that you have not read yet. When using a contiguous array this unread data contains the next elements. So they are cached without ever having been read.


Cache prefetching. (https://en.wikipedia.org/wiki/Cache_prefetching)

Essentially, many workloads access data sequentially and therefore modern cache architectures have special optimizations to make these memory accesses as fast as possible, by prefetching the next item in the sequence before it is actually needed.


Ah, yes.

Well, anyways I value code simplicity more than anything.

Maybe CPUs will learn to do cache prefetch for link oriented languages: if a "load and dereference" pattern is detected, CPU could prefetch the data referenced by pointers it has in registers or recently fetched cache line.

BTW, linked list elements are not necessary located far away from each other, if we allocated them one after another chance are they are near each other in memory.


the time complexity of insert etc is superior and a good reason for abstraction on top of an array, I would think.


Yeah, I like open offices. It's nice to be able to take off my headphones and ask a question to someone close by. Or just take a break and talk to them about something non work related.

Even if I had a private office I would have my headphones on 80% of the time.


... That guide is actually fucking hilarious.

'However, that’s not too bad, because even the makers of GNU indent recognize the authority of K&R (the GNU people aren’t evil, they are just severely misguided in this matter)'


but the preferred way, as shown to us by the prophets Kernighan and Ritchie, is to put the opening brace last on the line, and put the closing brace first, thusly:

So much for the idea that kernel developers are super-smart programming "gods". Formatting code that way is brain-dead and Kernighan and Ritchie are false prophets. The real prophet, Allman, tells us the Truth - curly braces go on a line by themselves, aligned vertically and indented to the same level as their corresponding control structure.


These theories were ultimately settled by trial by combat between the champions representing each: Linux and BSD. Linux has clearly won this battle, and thus, the gods have spoken. ~


Huh, I was assigned to Windows km development for my first ever internship. I had a decent amount of winternal and cpp experience, but it really is a different ball park and made me a much better computer scientist.


Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: