@@ -39,7 +39,6 @@ func newCompilation(opts Options) *compilation {
39
39
sourcesByIndex : make (map [int ]* sources.Source ),
40
40
outputsByIndex : make (map [int ]struct {}),
41
41
astsByIndex : make (map [int ]* ast.Stylesheet ),
42
- lockersByIndex : make (map [int ]* sync.Mutex ),
43
42
result : newResult (),
44
43
reporter : logging .DefaultReporter ,
45
44
transforms : opts .Transforms ,
@@ -66,12 +65,14 @@ type compilation struct {
66
65
sources map [string ]int
67
66
nextIndex int
68
67
69
- sourcesByIndex map [int ]* sources.Source
70
- astsByIndex map [int ]* ast.Stylesheet
68
+ sourcesByIndexMu sync.RWMutex
69
+ sourcesByIndex map [int ]* sources.Source
70
+
71
+ astsByIndexMu sync.RWMutex
72
+ astsByIndex map [int ]* ast.Stylesheet
71
73
72
74
// outputsByIndex is the set of sources to write outputs for.
73
75
outputsByIndex map [int ]struct {}
74
- lockersByIndex map [int ]* sync.Mutex
75
76
76
77
result * Result
77
78
@@ -110,13 +111,11 @@ func (c *compilation) addSource(path string) (int, error) {
110
111
c .sourcesMu .Lock ()
111
112
i := c .nextIndex
112
113
c .sources [abs ] = i
113
- locker := & sync.Mutex {}
114
- c .lockersByIndex [i ] = locker
115
114
c .sourcesMu .Unlock ()
116
115
117
- locker .Lock ()
118
- defer locker .Unlock ()
116
+ c .sourcesByIndexMu .Lock ()
119
117
c .sourcesByIndex [i ] = source
118
+ c .sourcesByIndexMu .Unlock ()
120
119
121
120
c .nextIndex ++
122
121
return i , nil
@@ -159,16 +158,16 @@ func (c *compilation) parseFile(file string, hasOutput bool) *ast.Stylesheet {
159
158
c .outputsByIndex [idx ] = struct {}{}
160
159
}
161
160
162
- // Grab the lock for this source, since multiple callers might try
163
- // to parse the same file.
164
- locker := c .lockersByIndex [idx ]
165
- locker .Lock ()
166
- defer locker .Unlock ()
161
+ c .astsByIndexMu .RLock ()
167
162
if ss , ok := c .astsByIndex [idx ]; ok {
163
+ c .astsByIndexMu .RUnlock ()
168
164
return ss
169
165
}
166
+ c .astsByIndexMu .RUnlock ()
170
167
168
+ c .sourcesByIndexMu .RLock ()
171
169
source := c .sourcesByIndex [idx ]
170
+ c .sourcesByIndexMu .RUnlock ()
172
171
ss , err := parser .Parse (source )
173
172
if err != nil {
174
173
c .addError (err )
@@ -213,7 +212,9 @@ func (c *compilation) parseFile(file string, hasOutput bool) *ast.Stylesheet {
213
212
}
214
213
215
214
ss = transformer .Transform (ss , opts )
215
+ c .astsByIndexMu .Lock ()
216
216
c .astsByIndex [idx ] = ss
217
+ c .astsByIndexMu .Unlock ()
217
218
return ss
218
219
}
219
220
0 commit comments