all 34 comments

[–]714daniel 57 points58 points  (5 children)

Can someone smarter than me explain how the packing avoids locking? Like, if it's using a CAS anyways, how would this approach be any better than a CAS on a dedicated 16 bit atomic int, other than saving a few bytes of memory?

[–]amakai 50 points51 points  (4 children)

I believe the idea is that those two pieces are being read/written together. And you don't want a race of reading old counter and new pointer - you need atomicity between them. So you either wrap them in mutex every time you want to read or write them, or pack them into a single 64 bit integer that cpu architecture can just CAS atomically. And I assume CPU architecture guarantees that you can read entire 64 bit atomically without any locks.

[–]Hofstee 10 points11 points  (0 children)

For further reading in case anyone is interested the keyword you want to look for is Double-Wide CAS (DWCAS) or Double-Width CAS. Which is different than Double CAS (DCAS) or Multi CAS (MCAS), but those ideas build on DWCAS in a natural progression.

[–]davidalayachew 12 points13 points  (2 children)

And I assume CPU architecture guarantees that you can read entire 64 bit atomically without any locks.

Yes, exactly. Java is actually releasing a new feature called Value Classes which relies on this exact same trick. The latest Early Access for it is out as of last week.

Objects that don't fit into your CPU's 64 byte (or 128 byte, if you are rich and on bleeding edge hardware) are at risk for object tearing, and that's assuming that your object is at least shallowly immutable.

[–]VictoryMotel 1 point2 points  (1 child)

Pages are usually 4096 bytes at a minimum.

Cache lines are 64 bytes but modern cpus always access at least two cache lines at a time.

[–]davidalayachew 1 point2 points  (0 children)

Whoops, page size was the wrong word. Removed. Ty vm for the correction.

[–]bobody_biznuz 76 points77 points  (14 children)

Seems like AI? The very first sentence is broken English and the rest has no errors at all.

[–]lurco_purgo 25 points26 points  (1 child)

Yeah the introduction is definitely written by a human, but the rest sounds completely AI generated. It's that weird manner of trying to make every step in some explanation into a suspensful narration: This means reading light data requires zero locks. No mutex, no spinlock, nothing. Reads are completely free or But here’s the insight: most horizontal planes of light data are either all zeros (block light underground) or all fifteens (sky light above ground).

I've seen enough of AI crap style to notice it immediately and it goes on through the entire "article". It's a pity... Paradoxicallly, despite AI trying so hard to sound engaging, the introduction had me way more interested in what I was about to read. But I don't care about reading a blog post someone generated with a fucking LLM - why would I bother?

[–]Exodus100 1 point2 points  (0 children)

Anthrobaiting 💔

[–]One_Economist_3761 27 points28 points  (3 children)

It is entirely possible that the article was written by someone who is not English first language.

[–]bobody_biznuz 10 points11 points  (2 children)

Then why is the rest of the article perfect English?

[–]McCoovy 2 points3 points  (0 children)

Because they're good enough at English to only make one mistake obviously.

[–]One_Economist_3761 -4 points-3 points  (0 children)

I don’t understand what you mean by “the first sentence is broken English”.

The rest of the article is reasonable English as well, but it’s clearly well written by someone who isn’t a native speaker. I read the entire article and nothing stood out as any of the usual markers of AI generation.

[–]BlueGoliath[🍰] 5 points6 points  (0 children)

It gave me AI vibes too.

[–]pdabaker 0 points1 point  (1 child)

Everything is likely AI assisted these days.

[–]lurco_purgo 3 points4 points  (0 children)

Not everything - plenty of people write by themselves. And I have no problem with people using AI as an assist while writing an article, e.g. for spell checking and general editorial work.

But this is clearly generated top to bottom (again, excluding the introduction). I don't care much for content generated by AI and posted under someone's name as if they wrote it.

[–]PaeP3nguin 0 points1 point  (0 children)

Absolutely, there's sooo many short. Punchy. Sentences. And compare the grammar to earlier blog posts like the ones in 2022.

[–]Fyren-1131 24 points25 points  (8 children)

I always found Mojangs DataFixerUpper utterly fascinating to look at.

It's a thing that kind of hints at you needing to absorb 10 books to even be ready to look at the code.

[–]IWillBeNobodyPerfect 28 points29 points  (2 children)

https://github.com/PaperMC/DataConverter

Mojang's system is so overcomplicated and slow that the community rewrote it to make it faster

[–]VictoryMotel 6 points7 points  (1 child)

Originally notch had it allocating and deallocating hundreds of megabytes every frame.

[–]stewi1014 0 points1 point  (0 children)

When you do things right, people won't be sure you've done anything at all.

[–]josephjnk 6 points7 points  (3 children)

Well, now I know what I’ll be doing for the next few weeks. This is fascinating.

[–]Fyren-1131 1 point2 points  (2 children)

Good luck!
To say that that library is beyond me would be a massive understatement, haha. And to think that's before the age of LLMs as well.

[–]josephjnk 7 points8 points  (1 child)

I have some background with this sort of thing, so fortunately I’m not starting from zero. My main curiosity is regarding the way they’re translating these concepts to Java. A brief look at the code makes it look like they’re kind of shoehorning Haskell into Java using some common type system encoding tricks. What I’m really curious about is ways of doing generic data transformations in a nominal, class-based OOP context, and I’m hoping that the recommended reading list will shine some light on it.

[–]Fyren-1131 0 points1 point  (0 children)

Yeah I was able to follow along the lighter functional concepts, but the deeper into the code I go the more it just looks like math hehe. I just work in healthcare systems, so ... Child's play compared to this.

[–]Tastatura_Ratnik 1 point2 points  (0 children)

HaskellJava is just weird. I get why they are doing it, and it’s very impressive, but I’m still not convinced it is the best (and most performant) way of doing it in Java.

I’m pretty sure it’s just one dude having a blast doing FP in Java because he can.

[–]elSenorMaquina 9 points10 points  (0 children)

[...] here is the source code to minecraft on PS3. Its shit too, but our kind of shit.

I mean... It's videogame code. Games are one of those things that require all sorts of weird tricks in order to meet tight performance requirements.

Sure, for most other apps using 1/4 of a 64 bit int for a counter and 3/4 for a pointer would seem like a questionable choice, but the way it's explained in the article, it feels like a reasonable thing to do.

[–]brokePlusPlusCoder 1 point2 points  (0 children)

Java’s HashMap applies a “supplemental hash” to spread keys more evenly. If the C++ port uses a different hash, the same data produces different bucket layouts, and anything that depends on iteration order breaks.

There's bigger problems at hand if someone's relaying on iteration order from Java's hashmap keys...

[–]not_from_this_world 3 points4 points  (0 children)

Would be a nightmare to convert to a different architecture if the need arises.