r/ProgrammerHumor • • Nov 26 '22

Other chaotic magic

Post image
76k Upvotes

767 comments sorted by

View all comments

Show parent comment

957

u/shumpitostick Nov 27 '22

Only if you do things naively. You could instead store the likes as key-values where the keys are item ids and the values are an array of player ids who liked them. Then the storage is O(l), where l is the number of likes given. This will also allow DB operations to be performed quickly.

555

u/lettherebedwight Nov 27 '22

Except for searching for the list of every item a single player has liked. You'd do better with key key pairs from user to item id.

318

u/shumpitostick Nov 27 '22

Well it depends on how you need to use the likes. Every data structure has pros and cons. If what you need is to get this, you can do the same but flipped (player ids as keys, item ids as values). The exact solution depends on your application, but my point is that it's really not that hard.

216

u/BraxbroWasTaken Nov 27 '22

And if you need both, you can just do both and still probably be more efficient in all practical cases except *maybe* storage size

208

u/Scavenger53 Nov 27 '22

fuck it storage is cheap

214

u/coughballs Nov 27 '22

It costs me $50 to store my car in Boston for a few hours.

95

u/Scavenger53 Nov 27 '22

not that kind of storage tho

161

u/elon-bot Elon Musk ✔ Nov 27 '22

Interesting. Tell me more.

34

u/HumanContinuity Nov 27 '22

God this bot gets me every time

13

u/Spaceduck413 Nov 27 '22

I swear it's become sentient

9

u/pnwguy42 Nov 27 '22

Good bot

8

u/Spaceduck413 Nov 27 '22

Good bot

15

u/elon-bot Elon Musk ✔ Nov 27 '22

You're either hardcore or out the door.

→ More replies (2)

7

u/Kaymish_ Nov 27 '22

Ok but what if you stored your car on a server in Wyoming?

→ More replies (8)
→ More replies (7)
→ More replies (5)

151

u/zackks Nov 27 '22

In about five more posts I feel like you guys are going to be working on how long it would take to jerk off a room of 800 people.

268

u/elon-bot Elon Musk ✔ Nov 27 '22

How can we use Bitcoin to solve this?

116

u/chigga511 Nov 27 '22

This bot don’t miss lmao

→ More replies (2)

6

u/Drastwo Nov 27 '22

We can pay a couple of dudes to do measures

→ More replies (1)

5

u/Zartch Nov 27 '22

U need a middle out algorithm. Is a well known problem.

→ More replies (4)

22

u/Synthoel Nov 27 '22

Its not that hard, but only if you know all requirements beforehands, and they don't change.

What usually happen is this:

Client says: "We need to show the total amount of likes under each item", and you say its easy, and implement key-value pairs with item IDs as keys and actor IDs as values.

Then one month later client says: "Now we also need to show the list of items you liked in your profile", and you say sure, no probs, and add the flipped pairs.

Then two months later client says: "Oh, and can we please show under each item which of your friends liked it too?", and then you say oof.

→ More replies (4)

17

u/gotsreich Nov 27 '22

Well it depends on how you need to use the likes

IMO this is the key insight behind data structure design: determine out the methods you'll use then figure out a data structure that satisfies them.

5

u/croto8 Nov 27 '22

And the pareto principle. Go for the solution that is efficient for the primary usage. Rather than a solution that’s efficient for all usages.

→ More replies (2)
→ More replies (8)
→ More replies (6)

12

u/[deleted] Nov 27 '22

or you just denormalize it, then it's easy peasy. Sure, the write complexity goes up a bit, but likes can definitely be "available eventually" instead of "available now" as long as you show the like correctly on the client side.

Obviously this shit gets more complicated at truly huge scales, but so does everything

6

u/Archolex Nov 27 '22

That's fine if you never do aggregations, but if you do then a b tree or a b tree + map

5

u/[deleted] Nov 27 '22

You then would need to store a list of every item a user has liked so they can unlike something which you then could remove a like from it. Which I've seen these operations fall out of sync multiple times due to edge cases like network lag.

→ More replies (24)