comparison Implab/Parallels/ArrayTraits.cs @ 89:ce0171cacec4 v2

improved performance of a chained map operation
author cin
date Wed, 08 Oct 2014 02:19:45 +0400
parents 4f20870d0816
children 4c0e5ef99986
comparison
equal deleted inserted replaced
88:c4877ae77cee 89:ce0171cacec4
161 for (int i = 0; i < source.Length; i++) { 161 for (int i = 0; i < source.Length; i++) {
162 if(promise.IsResolved) 162 if(promise.IsResolved)
163 break; // stop processing in case of error or cancellation 163 break; // stop processing in case of error or cancellation
164 var idx = i; 164 var idx = i;
165 165
166 lock(locker) { 166 if (Interlocked.Decrement(ref slots) < 0) {
167 while(slots == 0) 167 lock(locker) {
168 Monitor.Wait(locker); 168 while(slots < 0)
169 slots--; 169 Monitor.Wait(locker);
170 }
170 } 171 }
172
171 try { 173 try {
172 transform(source[i]) 174 transform(source[i])
173 .Anyway(() => { 175 .Anyway(() => {
174 lock(locker) { 176 Interlocked.Increment(ref slots);
175 slots ++; 177 lock (locker) {
176 Monitor.Pulse(locker); 178 Monitor.Pulse(locker);
177 } 179 }
178 }) 180 })
179 .Last( 181 .Last(
180 x => { 182 x => {
181 res[idx] = x; 183 res[idx] = x;
182 var left = Interlocked.Decrement(ref pending); 184 var left = Interlocked.Decrement(ref pending);
183 if (left == 0) 185 if (left == 0)
184 promise.Resolve(res); 186 promise.Resolve(res);
185 }, 187 },
186 e => promise.Reject(e) 188 promise.Reject
187 ); 189 );
188 190
189 } catch (Exception e) { 191 } catch (Exception e) {
190 promise.Reject(e); 192 promise.Reject(e);
191 } 193 }