Advertisement
psockali

FastFlow Java Buffer (Queue) implementation (GPL)

Aug 1st, 2013
822
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.23 KB | None | 0 0
  1. import java.util.AbstractQueue;
  2. import java.util.Iterator;
  3. import java.util.Queue;
  4. import java.util.concurrent.CountDownLatch;
  5.  
  6. @SuppressWarnings("unused")
  7. final public class FFBuffer<T> extends AbstractQueue<T> implements Queue<T> {
  8.     private int _pad000,_pad001,_pad002,_pad003,_pad004,_pad005,_pad006,_pad007;
  9.     private int _pad008,_pad009,_pad00a,_pad00b,_pad00c,_pad00d,_pad00e,_pad00f;
  10.     private int pread, _pread0;
  11.     private int _pad100,_pad101,_pad102,_pad103,_pad104,_pad105,_pad106,_pad107;
  12.     private int _pad108,_pad109,_pad10a,_pad10b,_pad10c,_pad10d,_pad10e,_pad10f;
  13.     private int pwrite, _pwrite0;
  14.     private int _pad200,_pad201,_pad202,_pad203,_pad204,_pad205,_pad206,_pad207;
  15.     private int _pad208,_pad209,_pad20a,_pad20b,_pad20c,_pad20d,_pad20e,_pad20f;
  16.     private final int size; private int size0;
  17.     private final int POW;  private int POW0;
  18.     private final int mask; private int mask0;
  19.     private T _pad300,_pad301,_pad302,_pad303,_pad304,_pad305,_pad306,_pad307;
  20.     private T _pad308,_pad309,_pad30a,_pad30b,_pad30c,_pad30d,_pad30e,_pad30f;
  21.     private final T data[];
  22.     private T _pad400,_pad401,_pad402,_pad403,_pad404,_pad405,_pad406,_pad407;
  23.     private T _pad408,_pad409,_pad40a,_pad40b,_pad40c,_pad40d,_pad40e,_pad40f;
  24.  
  25.     public FFBuffer(int sizeByPowerOfTwo, int pow) {
  26.         this.size = 1 << sizeByPowerOfTwo;
  27.         this.mask = size - 1;
  28.         this.POW = pow;
  29.         this.data = (T[]) new Object[size << POW];
  30.     }
  31.  
  32.     int id(int n) { return (n & mask) << POW; }
  33.  
  34.     public boolean offer(T obj) {
  35.         if (null == obj) throw new IllegalArgumentException("elem is null");
  36.         int id = id(pwrite);
  37.         if (null!=data[id]) return false;
  38.         data[id] = obj; pwrite++;
  39.         return true;
  40.     }
  41.  
  42.     public T poll() {
  43.         int id = id(pread);
  44.         T rc = data[id];
  45.         if (null == rc) return null;
  46.         data[id] = null; pread++;
  47.         return rc;
  48.     }
  49.    
  50.     @Override
  51.     public int size() {
  52.         throw new UnsupportedOperationException();
  53.     }
  54.    
  55.     @Override
  56.     public Iterator<T> iterator() {
  57.         throw new UnsupportedOperationException();
  58.     }
  59.    
  60.     public T peek() {
  61.         return null;
  62.     }
  63.    
  64.     public static void test(FFBuffer<Long> buf, int run, long count) throws Exception {
  65.         // one might have to tweak the buffer size and data placement delta for target env
  66.         final FFBuffer<Long> queue = buf;
  67.         final Long data = 1L;
  68.        
  69.         final long max = count;
  70.        
  71.         final CountDownLatch counter = new CountDownLatch(1);
  72.         Thread consumer = new Thread(new Runnable() {
  73.            
  74.             public void run() {
  75.                 long count = 0;
  76.                 for (int i=0; i<max; i++) {
  77.                     Long n = null;
  78.                     while (null==(n=queue.poll())) Thread.yield();
  79.                     count += n;
  80.                 }
  81.                
  82.                 if (count!=max)
  83.                     System.out.println("Ooops...guru failed");
  84.                    
  85.                 counter.countDown();
  86.             }
  87.         });
  88.        
  89.         long start = System.nanoTime();
  90.         consumer.start();
  91.  
  92.         for (int i=0; i<max; i++)
  93.             while (!queue.offer(data)) Thread.yield();
  94.        
  95.         counter.await();
  96.         long end = System.nanoTime();
  97.        
  98.         long delta = end-start;
  99.         System.out.format("run[%02d]: %,d ops in %,d ns (%,f ns per op) {%,d ops per second}\n", run, max, delta, (1.0*delta/max), (max*1000000000)/delta);
  100.     }
  101.    
  102.     public static void main(String[] args) throws Exception {
  103.         FFBuffer<Long> buf = new FFBuffer<Long>(15, 2);
  104.         for (int i=0; i<25; i++)
  105.             test(buf, i, 1L<<30);
  106.     }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement