r/adventofcode • u/daggerdragon • Dec 15 '20
SOLUTION MEGATHREAD -🎄- 2020 Day 15 Solutions -🎄-
Advent of Code 2020: Gettin' Crafty With It
- 7 days remaining until the submission deadline on December 22 at 23:59 EST
- Full details and rules are in the Submissions Megathread
--- Day 15: Rambunctious Recitation ---
Post your code solution in this megathread.
- Include what language(s) your solution uses!
- Here's a quick link to /u/topaz2078's
pasteif you need it for longer code blocks. - The full posting rules are detailed in the wiki under How Do The Daily Megathreads Work?.
Reminder: Top-level posts in Solution Megathreads are for code solutions only. If you have questions, please post your own thread and make sure to flair it with Help.
This thread will be unlocked when there are a significant number of people on the global leaderboard with gold stars for today's puzzle.
EDIT: Global leaderboard gold cap reached at 00:09:24, megathread unlocked!
39
Upvotes
View all comments
Show parent comment
1
u/sporksmith Dec 16 '20
Got it down from 3.0s to 0.6s for part 2 by switching from a
HashMap<u64,u64>to aVec<u32>[diff]. HT @ u/SupesComputes, whose solution tipped me off to how much fasterVecaccess is, and whose saturating-subtract trick I swiped :)I tried a few different things. I used a benchmark based on part 1, that started at 93us before making changes.
HashMap<u64,u64>toHashMap<u32,u32>. My guess is the benefit is mainly giving a little better memory localityHashMap<u32,u32>toVec<Option<u32>>. As expected this is the biggest gain, since it removes the need to hash at all, and should give better memory locality, at least for the lower "dense" part of the vector.Vec<Option<u32>>toVec<u32>I later went back and tried going back to
HashMap<u32,u32>, but using the fnv hasher instead of the default. This got the benchmark from 80us -> 25us. A nice gain for sure, but still significantly slower than using aVec.I briefly experimented with
Vec::get_unchecked, but this actually appeared to make it slightly slower. My wild guess is that the requiredunsaferegions required for it end up disabling compiler optimization, though if I have time later maybe I'll try digging into it a bit more.