- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
see the single-producer/single consumer FIFO code below,seems code1 is compact,and only have 1 if-else branch.
time: the former cost about avg. 160ns per insert (by clock_gettime) latter cost about 20 ns per insert(surprised me! ).
the CPI result:former is about 30,latter is about 8.
I don't check other events
the reason might be the code1 has an expensive insruct "div",but how to prove it?
code1:
bool insert( const T &rtItem )
{
uint32_t ww = (uint32_t) atomic_read( &w );
uint32_t rr = (uint32_t) atomic_read( &r );
if (predict_true(ww - rr != size))
{
f[ ww % size ] = rtItem;
atomic_inc( &w );
return true;
}
return false;
}
/////////////////////
code2:
bool insert( const T &rtItem )
{
uint32_t nw;
bool tRet = false;
uint32_t ww = (uint32_t) atomic_read( &w );
uint32_t rr = (uint32_t) atomic_read( &r );
if ( predict_true( ( ww + 1 ) < size ) )
nw = ww + 1;
else
nw = 0;
if ( predict_true( nw != rr ) )
{
f[ ww ] = rtItem;
atomic_set( &w, nw );
tRet = true;
}
return tRet;
}
time: the former cost about avg. 160ns per insert (by clock_gettime) latter cost about 20 ns per insert(surprised me! ).
the CPI result:former is about 30,latter is about 8.
I don't check other events
the reason might be the code1 has an expensive insruct "div",but how to prove it?
code1:
bool insert( const T &rtItem )
{
uint32_t ww = (uint32_t) atomic_read( &w );
uint32_t rr = (uint32_t) atomic_read( &r );
if (predict_true(ww - rr != size))
{
f[ ww % size ] = rtItem;
atomic_inc( &w );
return true;
}
return false;
}
/////////////////////
code2:
bool insert( const T &rtItem )
{
uint32_t nw;
bool tRet = false;
uint32_t ww = (uint32_t) atomic_read( &w );
uint32_t rr = (uint32_t) atomic_read( &r );
if ( predict_true( ( ww + 1 ) < size ) )
nw = ww + 1;
else
nw = 0;
if ( predict_true( nw != rr ) )
{
f[ ww ] = rtItem;
atomic_set( &w, nw );
tRet = true;
}
return tRet;
}
Link Copied
1 Reply
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Quoting - softarts
the reason might be the code1 has an expensive insruct "div",but how to prove it?

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page