private void optimizeSets() {
    // make sure there are at least two get operations in a row before
    // attempting to optimize them.
    optimizedOp = writeQ.remove();
    if (writeQ.peek() instanceof CASOperation) {
      OptimizedSetImpl og = new OptimizedSetImpl((CASOperation) optimizedOp);
      optimizedOp = og;

      while (writeQ.peek() instanceof StoreOperation
          && og.size() < MAX_SET_OPTIMIZATION_COUNT
          && og.bytes() < MAX_SET_OPTIMIZATION_BYTES) {
        CASOperation o = (CASOperation) writeQ.remove();
        if (!o.isCancelled()) {
          og.addOperation(o);
        }
      }

      // Initialize the new mega set
      optimizedOp.initialize();
      assert optimizedOp.getState() == OperationState.WRITE_QUEUED;
    }
  }
Implementation of MemcachedNode for speakers of the binary protocol.
src/main/java/net/spy/memcached/protocol/binary/BinaryMemcachedNodeImpl.java