Skip to content

Interlocked.Read x100 slowdown in some condition #24119

@nshibano

Description

@nshibano

Interlocked.Read usually takes ~10 nanoseconds, but in some unconfirmed condition it takes ~1000 nanoseconds.

This is F# code I used to measure Interlocked.Read speed. (Executing this code on your machine will not reproduce the issue. Please read below section.)

open System
open System.Diagnostics
open System.Threading
// Opening other proprietary module

[<EntryPoint>]
let main argv =

    // Other proprietary definitions here

    let counter = ref 0L
    while true do
        let sw = Stopwatch.StartNew()
        let n = 1000000
        for i = 1 to n do
            Interlocked.Read(counter) |> ignore
        sw.Stop()
        let t = int (1e9 * float sw.ElapsedTicks / float Stopwatch.Frequency / float n)
        Console.WriteLine(sprintf "%d (ns)" t)
        Console.ReadKey() |> ignore

    // Other proprietary definitions here

    0

This is output from process in normal state.

9 (ns)
9 (ns)
9 (ns)
9 (ns)

This is output from process in erroneous state.

762 (ns)
734 (ns)
728 (ns)

Difficulty in reproducing

I couldn't reproduce this issue in small code. Currently this issue only reproduced in following condition.

  • One individual machine (Ryzen 5 1600X CPU, Windows 10)
  • Linked with other proprietary codes (I can not share it here. Sorry.)
  • Release build
  • Without debugger attached
  • With some random factor (reproduces with ~70% possibility for process launch)
  • Once process enters erroneous state, it continues until process terminates

Even trivial change in code (removing unused code, changing length of unused string constant) makes not able to reproduce this issue. So I suspect this issue is related to low-level things such as memory layout in the process.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions