@@ -383,35 +383,19 @@ module RuntimeHelpers =
383383
384384
385385 let EnumerateTryWith ( source : seq < 'T >) ( exceptionFilter : exn -> int ) ( exceptionHandler : exn -> seq < 'T >) =
386- // Manual thread-safe one-time initialization cell to replace lazy (avoids IL2091 trimming warnings)
387- let mutable originalSourceValue = None
388- let originalSourceInitLock = obj()
389- let getOriginalSource () =
390- match originalSourceValue with
391- | Some v -> v
392- | None ->
393- // Double-checked locking pattern: first check avoids lock overhead after initialization,
394- // second check inside lock ensures only one thread initializes the value
395- lock originalSourceInitLock ( fun () ->
396- match originalSourceValue with
397- | Some v -> v
398- | None ->
399- let v = source.GetEnumerator()
400- originalSourceValue <- Some v
401- v)
402-
386+ let originalSource = lazy source.GetEnumerator()
403387 let mutable shouldDisposeOriginalAtTheEnd = true
404388 let mutable exceptionalSource : IEnumerator < 'T > option = None
405389
406390 let current () =
407391 match exceptionalSource with
408392 | Some es -> es.Current
409- | None -> getOriginalSource () .Current
393+ | None -> originalSource.Value .Current
410394
411395 let disposeOriginal () =
412396 if shouldDisposeOriginalAtTheEnd then
413397 shouldDisposeOriginalAtTheEnd <- false
414- getOriginalSource () .Dispose()
398+ originalSource.Value .Dispose()
415399
416400 let moveExceptionHandler exn =
417401 exceptionalSource <- Some (( exceptionHandler exn) .GetEnumerator())
@@ -437,7 +421,7 @@ module RuntimeHelpers =
437421 | Some es -> es.MoveNext()
438422 | None ->
439423 try
440- let hasNext = getOriginalSource () .MoveNext()
424+ let hasNext = originalSource.Value .MoveNext()
441425 if not hasNext then
442426 // What if Moving does not fail, but Disposing does?
443427 // In that case, the 'when' guards could actually produce new elements to by yielded
0 commit comments