-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathconcurrent-vector.lisp
295 lines (227 loc) · 8.36 KB
/
concurrent-vector.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
;; -*- lisp -*-
;; This file is part of STMX.
;; Copyright (c) 2013-2016 Massimiliano Ghilardi
;;
;; This library is free software: you can redistribute it and/or
;; modify it under the terms of the Lisp Lesser General Public License
;; (http://opensource.franz.com/preamble.html), known as the LLGPL.
;;
;; This library is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty
;; of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
;; See the Lisp Lesser General Public License for more details.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ;;
;; UNFINISHED! DO NOT USE! ;;
;; ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(in-package :cl-user)
(defpackage #:stmx.concurrent
(:use #:cl
#:bordeaux-threads
#:stmx.lang
#:stmx
#:stmx.util)
(:import-from #:stmx
#:new
#:raw-value-of
#:do-hash))
(in-package :stmx.concurrent)
(defmacro 1m (&rest body)
`(time (dotimes (,(gensym) 1000000)
,@body)))
(defmacro 1g (&rest body)
`(time (dotimes (,(gensym) 1000000000)
,@body)))
;;;; ** lock-free concurrent vector
(defvar *empty-vector* #())
(defstruct (cvector (:constructor %make-cvector))
(data *empty-vector* :type simple-array)
(mod-hi 0 :type sb-vm:word)
(mod-lo 0 :type sb-vm:word))
(defun make-cvector (size &key (element-type t)
(initial-element nil initial-element?)
(initial-contents nil initial-contents?))
(declare (type fixnum size)
(type symbol element-type))
(let1 v (%make-cvector)
(setf (cvector-data v)
(cond
(initial-contents?
(make-array size :element-type element-type :initial-contents initial-contents))
(initial-element?
(make-array size :element-type element-type :initial-element initial-element))
(t
(make-array size :element-type element-type))))
v))
(declaim (inline vref))
(defun vref (vector subscript)
"AREF for concurrent vectors"
(declare (type cvector vector)
(type fixnum subscript))
(let1 data (cvector-data vector)
(declare (type simple-vector data))
(aref data subscript)))
(defun setf-vref-slow-path (element vector subscript)
(declare (ignore element vector subscript))
(error "setf-vref-slow-path invoked"))
(declaim (inline (setf vref)))
(defun (setf vref) (element vector subscript)
"(SETF AREF) for concurrent vectors"
(declare (type cvector vector)
(type fixnum subscript))
;;(incf (the fixnum (cvector-mod-hi vector)))
(sb-ext:atomic-incf (cvector-mod-hi vector))
(let1 data (cvector-data vector)
(declare (type simple-vector data))
(setf (aref data subscript) element))
(sb-ext:atomic-incf (cvector-mod-lo vector))
;;(incf (the fixnum (cvector-mod-lo vector)))
element)
(defun incf-vref-slow-path (vector subscript delta)
(declare (ignore vector subscript delta))
(error "incf-vref-slow-path invoked"))
(declaim (inline incf-vref))
(defun incf-vref (vector subscript)
"INCF AREF for concurrent vectors"
(declare (type cvector vector)
(type fixnum subscript))
;;(incf (the fixnum (cvector-mod-hi vector)))
;;(sb-ext:atomic-incf (cvector-mod-hi vector))
(let1 data (cvector-data vector)
(declare (type simple-vector data))
(let1 new-value
(incf (the fixnum (aref data subscript)))
;;(sb-ext:atomic-incf (cvector-mod-lo vector))
;;(incf (the fixnum (cvector-mod-lo vector)))
new-value)))
(defmacro 1m (&rest body)
`(time (dotimes (,(gensym) 1000000)
,@body)))
(defmacro 1g (&rest body)
`(time (dotimes (,(gensym) 1000000000)
,@body)))
(defun test-cvector-fixnum (&optional (i 0))
(declare (type fixnum i))
(let ((v (make-cvector 10 :element-type 'fixnum)))
(declare (type cvector v))
;; 9.82 nanoseconds per (vref v i) ;; 10.33 nanoseconds with (if (< subscript (length data))
;; 9.85 nanoseconds per (vref v 0)
(1g (incf (the fixnum (vref v i))))))
(defun test-cvector-incf-fixnum (&optional (i 0))
(declare (type fixnum i))
(let ((v (make-cvector 10 :element-type 'fixnum)))
(declare (type cvector v))
;; 10.10 nanoseconds per (incf-vref v i)
(1g (incf-vref v i))))
(defun test-cvector (&optional (i 0))
(declare (type fixnum i))
(let ((v (make-cvector 10)))
(declare (type cvector v))
;; 11.37 nanoseconds per (vref v i)
;; 11.11 nanoseconds per (vref v 0)
(1g (incf (vref v i)))))
(defun test-simple-vector-fixnum (&optional (i 0))
(declare (type fixnum i))
(let ((v (make-array 10 :element-type 'fixnum)))
(declare (type simple-array v))
;; 1.77 nanoseconds per (aref v i)
;; 1.52 nanoseconds per (aref v 0)
(1g (incf (the fixnum (aref v i))))))
(defun test-simple-vector (&optional (i 0))
(declare (type fixnum i))
(let ((v (make-array 10)))
(declare (type simple-vector v))
;; 2.73 nanoseconds per (aref v i)
;; 2.28 nanoseconds per (aref v 0)
(1g (incf (aref v i)))))
(defun test-vector-fixnum (&optional (i 0))
(declare (type fixnum i))
(let ((v (make-array 10 :adjustable t)))
(declare (type (and vector (not simple-array)) v))
;; 13.64 nanoseconds per (aref v i)
;; 13.86 nanoseconds per (aref v 0)
(1g (incf (the fixnum (aref v i))))))
(defun test-vector (&optional (i 0))
(declare (type fixnum i))
(let ((v (make-array 10 :adjustable t)))
(declare (type (and vector (not simple-array)) v))
;; 16.72 nanoseconds per (aref v i)
;; 15.97 nanoseconds per (aref v 0)
(1g (incf (aref v i)))))
(defun test-hash10-fixnum (&optional (i 0))
(declare (type fixnum i))
(let ((h (make-hash-table :test 'eql :size 10)))
(dotimes (j 10)
(setf (gethash (the fixnum j) h) (the fixnum 0)))
;; 15.44 nanoseconds per (gethash i h)
;; 15.08 nanoseconds per (gethash 0 h)
(1g (incf (the fixnum (gethash i h))))))
(defun test-hash100-fixnum (&optional (i 0))
(declare (type fixnum i))
(let ((h (make-hash-table :test 'eql :size 100)))
(dotimes (j 100)
(setf (gethash (the fixnum j) h) (the fixnum 0)))
;; 15.45 nanoseconds per (gethash i h)
;; 15.06 nanoseconds per (gethash 0 h)
(1g (incf (the fixnum (gethash i h))))))
(defun test-get-set-hash100-fixnum (&optional (i 0))
(declare (type fixnum i))
(let ((h (make-hash-table :test 'eql :size 100)))
(dotimes (j 100)
(set-hash h (the fixnum j) (the fixnum 0)))
;; 15.44 nanoseconds per (gethash i h)
;; 15.08 nanoseconds per (gethash 0 h)
(1g (incf (the fixnum (gethash i h))))))
(defun test-recycle-hash (&optional (n 16))
(declare (type fixnum n))
(let ((h (make-hash-table :test 'eq)))
;; 0.672 microseconds (n = 16)
;; 1.383 microseconds (n = 32)
;; 2.763 microseconds (n = 64)
;; 5.502 microseconds (n = 128)
;; 10.940 microseconds (n = 256)
;; 21.905 microseconds (n = 512)
(1m
(dotimes (j n) (setf (gethash j h) t))
(clrhash h))))
(defun test-recycle-hash2 (&optional (n 16))
(declare (type fixnum n))
(let ((h (make-hash-table :test 'eq)))
(dotimes (j n) (setf (gethash j h) t))
(clrhash h)
;; 0.663 microseconds (n = 16)
;; 0.790 microseconds (n = 32)
;; 0.789 microseconds (n = 63)
;; 0.974 microseconds (n = 64)
;; 0.979 microseconds (n = 127)
;; 1.339 microseconds (n = 128)
;; 2.062 microseconds (n = 256)
;; 3.496 microseconds (n = 512)
(1m
(dotimes (j 16) (setf (gethash j h) t))
(clrhash h))))
(defun test-new-hash (&optional (n 16))
(declare (type fixnum n))
;; 0.876 microseconds (n = 16)
;; 2.775 microseconds (n = 32)
;; 5.543 microseconds (n = 64)
;; 11.158 microseconds (n = 128)
;; microseconds (n = 256)
;; 46.594 microseconds (n = 512)
(1m
(let ((h (make-hash-table :test 'eq)))
(dotimes (j n) (setf (gethash j h) t)))))
(defun test-lock-unlock (&optional i)
(declare (ignore i))
(let ((lock (bt:make-lock)))
;; 30.06 nanoseconds per acquire + release
(1g (bt:acquire-lock lock)
(bt:release-lock lock))))
#+sbcl
(defun test-sbcl-lock-unlock (&optional i)
(declare (ignore i))
(let ((lock (sb-thread:make-mutex)))
;; 27.01 nanoseconds per get + release
(1g (sb-thread:get-mutex lock)
(sb-thread:release-mutex lock))))