Notes on profiling
[advent-of-code-21.git] / README.md
1 ---
2 title: "Advent of Code 2020"
3 output: html_document
4 css: modest.css
5 ---
6 Code to solve the [Advent of Code](http://adventofcode.com/2020/) puzzles. This year, I'm using the puzzles to develop my skills in [Haskell](https://wiki.haskell.org/Haskell). I'm writing up a [commentary on these puzzles and my solutions](https://work.njae.me.uk/tag/advent-of-code/) on my blog.
7
8 [Learn you a Haskell](http://learnyouahaskell.com/chapters), [Introduction to Haskell 98](https://www.haskell.org/tutorial/index.html), and [Hackage](https://hackage.haskell.org/) are good resources.
9
10 The [Cabal user guide](https://cabal.readthedocs.io/en/latest/index.html) and [How I Start: Haskell](http://howistart.org/posts/haskell/1/) are good sources of using the tools.
11
12 # Toolchain
13
14 Install Ghcup following [the instructions](https://www.haskell.org/ghcup/install/#installation), making sure to load the updated environment with
15
16 ```bash
17 source /home/neil/.ghcup/env
18 ```
19
20 and then set the default GHC to use with `ghcup set ghc 9.0.1` .
21
22 Install [Haskell Language Server](https://haskell-language-server.readthedocs.io/en/latest/configuration.html) for Sublime Text
23
24
25 ## Creating the repository and project
26 Create the repository as normal: create the project in Gitolite, clone it, and insert the `.gitignore` and `README.md` files.
27
28 There's one package per day, with the code for each package in sub-directories of the root directory.
29
30 Create the basic `cabal` project.
31
32 ```
33 cabal init
34 ```
35
36 Modify the `advent-of-code21.cabal` file as needed, such as updating the Cabal version and writing the `common` stanzas.
37
38 ## Creating subsequent days
39
40 Each day lives in a separate directory, with code in the `src` directory.
41
42 Compile with
43 ```
44 cabal build
45 ```
46 or
47 ```
48 cabal build advent01
49 ```
50
51 Run with
52 ```
53 cabal run advent01
54 ```
55
56 If you want to pass in additional RTS parameters, do it like this:
57 ```
58 cabal run advent01 -- +RTS -K0 -RTS
59 ```
60
61 Run interactively with
62 ```
63 cabal repl advent01
64 ```
65 or
66 ```
67 stack ghci advent01:exe:advent01
68 ```
69 if the first form is ambiguous.
70
71 To profile, use
72
73 ```
74 cabal run advent01 --enable-profiling -- +RTS -N -p -s -hT
75 ```
76
77 Or, you can simplify the RTS options by adding them to a new stanza in the cabal file:
78
79 ```
80 executable advent01prof
81 import: common-extensions, build-directives
82 main-is: advent01/Main.hs
83 build-depends: text, containers, linear, array, pqueue, mtl, lens
84 ghc-options: -O2
85 -Wall
86 -threaded
87 -rtsopts "-with-rtsopts=-N -p -s -hT"
88 ```
89
90 then running
91
92 ```
93 cabal run advent01prof --enable-profiling
94 ```
95
96
97 Generate the profile graph with
98 ```
99 hp2ps -M advent01.hp
100 ```
101
102 For Cabal, look at [profiling with Cabal sandboxes](https://nikita-volkov.github.io/profiling-cabal-projects/)
103
104
105 # Packages
106
107 Packages I used a lot:
108
109 * [Containers](https://hackage.haskell.org/package/containers) (and some [better documentation](https://haskell-containers.readthedocs.io/en/latest/intro.html)); [Unordered containers](https://hackage.haskell.org/package/unordered-containers) is a mostly-equivalent alternative.
110 * [Attoparsec](https://hackage.haskell.org/package/attoparsec) (and [Megaparsec](https://hackage.haskell.org/package/megaparsec), and [ReadP](https://hackage.haskell.org/package/base-4.14.1.0/docs/Text-ParserCombinators-ReadP.html) once).
111
112 There are somewhat decent [tutorials on Megaparsec](https://markkarpov.com/tutorial/megaparsec.html) and [Attoparsec](https://www.schoolofhaskell.com/school/starting-with-haskell/libraries-and-frameworks/text-manipulation/attoparsec).
113
114 Packages I didn't use much, but need to remember:
115
116 * [Arithmoi](https://hackage.haskell.org/package/arithmoi) for number theory
117 * [Pointed List](https://hackage.haskell.org/package/pointedlist-0.6.1) for zipper lists (sometimes circular)
118 * [Vector](https://hackage.haskell.org/package/vector) for array-like things
119 * [Linear](https://hackage.haskell.org/package/linear) for coordinate-vector like things
120 * [Grid](https://hackage.haskell.org/package/grid) for 2-d grids
121 * [Graph-wrapper](https://hackage.haskell.org/package/graph-wrapper) for graphs
122 * [Lens](https://hackage.haskell.org/package/lens) (and a [summary of operators](https://github.com/ekmett/lens/wiki/Operators)). I didn't use these much this year, but did a lot last year.
123 * [RWS](https://hackage.haskell.org/package/mtl-2.2.2/docs/Control-Monad-RWS-Lazy.html) (Reader-Writer-State monad stack); again, used a lot last year but not this year
124 * [Monad loops](https://hackage.haskell.org/package/monad-loops-0.4.3/docs/Control-Monad-Loops.html), and [a description](https://conscientiousprogrammer.com/blog/2015/12/11/24-days-of-hackage-2015-day-11-monad-loops-avoiding-writing-recursive-functions-by-refactoring/)
125 * [Replace-Megaparsec](https://github.com/jamesdbrock/replace-megaparsec), for using Mpc for all sorts of things traditionally done with regex substitutions.
126
127 # Readme
128
129 Build this readme file wth
130 ```
131 pandoc -s README.md > README.html
132 ```
133
134 (Using the [Modest style](https://github.com/markdowncss/modest).)