Advertisement
psockali

FastFlow Java Buffer implementation (GPL)

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