Debian Codegraph @

README.md

raw
1 # asm ![build status](https://github.com/segmentio/asm/actions/workflows/go.yml/badge.svg) [![GoDoc](https://godoc.org/github.com/segmentio/asm?status.svg)](https://godoc.org/github.com/segmentio/asm)
2
3 Go library providing algorithms that use the full power of modern CPUs to get
4 the best performance.
5
6 ## Motivation
7
8 The cloud makes it easier than ever to access large scale compute capacity,
9 and it's become common to run distributed systems deployed across dozens or
10 sometimes hundreds of CPUs. Because projects run on so many cores now, program
11 performance and efficiency matters more today than it has ever before.
12
13 Modern CPUs are complex machines with performance characteristics that may
14 vary by orders of magnitude depending on how they are used. Features like
15 branch prediction, instruction reordering, pipelining, or caching are all
16 input variables that determine the compute throughput that a CPU can achieve.
17 While compilers keep being improved, and often employ micro-optimizations that
18 would be counter-productive for human developers to be responsible for, there
19 are limitations to what they can do, and Assembly still has a role to play in
20 optimizing algorithms on hot code paths of large scale applications.
21
22 SIMD instruction sets offer interesting opportunities for software engineers.
23 Taking advantage of these instructions often requires rethinking how the program
24 represents and manipulates data, which is beyond the realm of optimizations that
25 can be implemented by a compiler. When renting CPU time from a Cloud provider,
26 programs that fail to leverage the full sets of instructions available are
27 therefore paying for features they do not use.
28
29 This package aims to provide such algorithms, optimized to leverage advanced
30 instruction sets of modern CPUs to maximize throughput and take the best
31 advantage of the available compute power. Users of the package will find
32 functions that have often been designed to work on **arrays of values**,
33 which is where SIMD and branchless algorithms shine.
34
35 The functions in this library have been used in high throughput production
36 environments at Segment, we hope that they will be useful to other developers
37 using Go in performance-sensitive software.
38
39 ## Usage
40
41 The library is composed of multiple Go packages intended to act as logical
42 groups of functions sharing similar properties:
43
44 | Package | Purpose |
45 | ------- | ------- |
46 | [ascii](ascii) | library of functions designed to work on ASCII inputs |
47 | [base64](base64) | standard library compatible base64 encodings |
48 | [bswap](bswap) | byte swapping algorithms working on arrays of fixed-size items |
49 | [cpu](cpu) | definition of the ABI used to detect CPU features |
50 | [mem](mem) | functions operating on byte arrays |
51 | [qsort](qsort) | quick-sort implementations for arrays of fixed-size items |
52 | [slices](slices) | functions performing computations on pairs of slices |
53 | [sortedset](sortedset) | functions working on sorted arrays of fixed-size items |
54
55 When no assembly version of a function is available for the target platform,
56 the package provides a generic implementation in Go which is automatically
57 picked up by the compiler.
58
59 ## Showcase
60
61 The purpose of this library being to improve the runtime efficiency of Go
62 programs, we compiled a few snapshots of benchmark runs to showcase the
63 kind of improvements that these code paths can expect from leveraging
64 SIMD and branchless optimizations:
65
66 ```
67 goos: darwin
68 goarch: amd64
69 cpu: Intel(R) Core(TM) i9-8950HK CPU @ 2.90GHz
70 ```
71
72 ```
73 pkg: github.com/segmentio/asm/ascii
74 name old time/op new time/op delta
75 EqualFoldString/0512 276ns ± 1% 21ns ± 2% -92.50% (p=0.008 n=5+5)
76
77 name old speed new speed delta
78 EqualFoldString/0512 3.71GB/s ± 1% 49.44GB/s ± 2% +1232.79% (p=0.008 n=5+5)
79 ```
80
81 ```
82 pkg: github.com/segmentio/asm/bswap
83 name old time/op new time/op delta
84 Swap64 11.2µs ± 1% 0.9µs ± 9% -92.06% (p=0.008 n=5+5)
85
86 name old speed new speed delta
87 Swap64 5.83GB/s ± 1% 73.67GB/s ± 9% +1162.98% (p=0.008 n=5+5)
88 ```
89
90 ```
91 pkg: github.com/segmentio/asm/qsort
92 name old time/op new time/op delta
93 Sort16/1000000 269ms ± 2% 46ms ± 3% -83.08% (p=0.008 n=5+5)
94
95 name old speed new speed delta
96 Sort16/1000000 59.4MB/s ± 2% 351.2MB/s ± 3% +491.24% (p=0.008 n=5+5)
97 ```
98
99 ## Maintenance
100
101 The assembly code is generated with [AVO](https://github.com/mmcloughlin/avo),
102 and orchestrated by a Makefile which helps maintainers rebuild the assembly
103 source code when the AVO files are modified.
104
105 The repository contains two Go modules; the main module is declared as
106 `github.com/segmentio/asm` at the root of the repository, and the second
107 module is found in the `build` subdirectory.
108
109 The `build` module is used to isolate build dependencies from programs that
110 import the main module. Through this mechanism, AVO does not become a
111 dependency of programs using `github.com/segmentio/asm`, keeping the
112 dependency management overhead minimal for the users, and allowing
113 maintainers to make modifications to the `build` package.
114
115 Versioning of the two modules is managed independently; while we aim to provide
116 stable APIs on the main package, breaking changes may be introduced on the
117 `build` package more often, as it is intended to be ground for more experimental
118 constructs in the project.
119
120 ### Requirements
121
122 Some libraries have custom purpose code for both amd64 and arm64. Others (qsort)
123 have only amd64. Search for a `.s` file matching your architecture to be sure
124 you are using the assembler optimized library instructions.
125
126 The Go code requires Go 1.17 or above. These versions contain significant
127 performance improvements compared to previous Go versions.
128
129 `asm` version v1.1.5 and earlier maintain compatibility with Go 1.16.
130
131 ### purego
132
133 Programs in the `build` module should add the following declaration:
134
135 ```go
136 func init() {
137 ConstraintExpr("!purego")
138 }
139 ```
140
141 It instructs AVO to inject the `!purego` tag in the generated files, allowing
142 the libraries to be compiled without any assembly optimizations with a build
143 command such as:
144
145 ```
146 go build -tags purego ...
147 ```
148
149 This is mainly useful to compare the impact of using the assembly optimized
150 versions instead of the simpler Go-only implementations.