UniqueIdGenerator icon indicating copy to clipboard operation
UniqueIdGenerator copied to clipboard

SpinToNextSequence might cause excessive OS calls for small sequences.

Open diputssi opened this issue 1 year ago • 0 comments

Might not be the best solution, but since I am using smaller sequence sizes and this could add significant os calls and context switches, it might be prudent to do something like this. 2 new 'spin' lines to SpinToNextSequence().

private void SpinToNextSequence()
{
    var spin = new SpinWait();   // create spin object
    var time = GetTime();

    while (time == _previousTime && _sequence >= _maxSequence)
    {
        spin.SpinOnce();    // spin locally in user space instead of forcing excess os calls to get next time.
        // Or maybe Thread.Sleep(0)?
        time = GetTime();
    }
    _sequence = time == _previousTime ? (short)(_sequence + 1) : (short)0;
    _previousTime = time;
 }

@mschuler

diputssi avatar Jun 22 '23 14:06 diputssi