Example #1
0
// Put will return a used buffer back to the free-list. If the capacity of the used buffer grew
// beyond the max buffer size, it will be discarded and its memory returned to the runtime.
func (bp *SyncPool) Put(bb *bytes.Buffer) {
	if bb.Cap() > bp.pc.maxKeep {
		return // drop buffer on floor if too big
	}
	bb.Reset()
	bp.pool.Put(bb)
}
Example #2
0
func (f *FreeList) Put(b *bytes.Buffer) {
	if b.Cap() > f.threshold {
		return
	}
	select {
	case f.ch <- b:
	default:
	}
}
Example #3
0
// Put will return a used buffer back to the free-list. If the capacity of the used buffer grew
// beyond the max buffer size, it will be discarded and its memory returned to the runtime.
func (bp *ChanPool) Put(bb *bytes.Buffer) {
	if bb.Cap() > bp.pc.maxKeep {
		return // drop buffer on floor if too big
	}
	bb.Reset()
	select {
	case bp.ch <- bb: // queue buffer for reuse
	default: // drop on floor if channel full
	}
}
Example #4
0
func main() {
	b := new(bytes.Buffer)
	fmt.Printf("%d %d \n", b.Len(), b.Cap())
	for i := 1; i < 66; i++ {
		b.WriteByte(byte(i))
		fmt.Printf("\t %d %d \n", b.Len(), b.Cap())
	}
	bs, _ := b.ReadByte()
	fmt.Printf("%v \n", bs)
}
Example #5
0
func (p *preAllocated) Put(bb *bytes.Buffer) {
	if bb.Cap() > p.maxKeep {
		if newBB, err := p.factory(); err == nil {
			p.pool.Put(newBB.(*bytes.Buffer))
			// drop bb on the floor for GC
			return
		}
		// if err creating new buffer, then just re-use bb
	}
	bb.Reset()
	p.pool.Put(bb)
}
Example #6
0
func (bp *SizedBufferPool) Put(b *bytes.Buffer) {
	b.Reset()

	if b.Cap() > bp.a {
		b = bytes.NewBuffer(make([]byte, 0, bp.a))
	}

	select {
	case bp.c <- b:
	default:
	}
}
Example #7
0
// Put returns the given Buffer to the SizedBufferPool.
func (bp *SizedBufferPool) Put(b *bytes.Buffer) {
	// If the pool is full opportunistically throw the buffer away
	if len(bp.c) == cap(bp.c) {
		return
	}

	// Release buffers over our maximum capacity and re-create a pre-sized
	// buffer to replace it.
	if b.Cap() > bp.a {
		b = bytes.NewBuffer(make([]byte, 0, bp.a))
	} else {
		b.Reset()
	}

	select {
	case bp.c <- b:
	default: // Discard the buffer if the pool is full.
	}
}
func TestEncodeDecode(t *testing.T) {

	var s1, s2 [5]byte
	copy(s1[:], "start")
	copy(s2[:], "  end")
	p1 := Point{s1, 3.14, 1, [2]uint8{66, 77}, Truthval1.NullValue, Truthval2.T}
	p2 := Point{s2, 0.31, 2, [2]uint8{77, 88}, Truthval1.T, Truthval2.F}
	c := Composite{p1, p2}

	var cbuf = new(bytes.Buffer)
	if err := c.Encode(cbuf, binary.LittleEndian); err != nil {
		t.Log("Composite Encoding Error", err)
		t.Fail()
	}
	t.Log(c, " -> ", cbuf.Bytes())
	t.Log("Cap() = ", cbuf.Cap(), "Len() = \n", cbuf.Len())

	m := MessageHeader{c.SbeBlockLength(), c.SbeTemplateId(), c.SbeSchemaId(), c.SbeSchemaVersion()}
	var mbuf = new(bytes.Buffer)
	if err := m.Encode(mbuf, binary.LittleEndian); err != nil {
		t.Log("MessageHeader Encoding Error", err)
		t.Fail()
	}
	t.Log(m, " -> ", mbuf.Bytes())
	t.Log("Cap() = ", mbuf.Cap(), "Len() = \n", mbuf.Len())

	// Create a new empty MessageHeader and Composite
	m = *new(MessageHeader)
	var c2 Composite = *new(Composite)

	if err := m.Decode(mbuf, binary.LittleEndian, c.SbeSchemaVersion(), true); err != nil {
		t.Log("MessageHeader Decoding Error", err)
		t.Fail()
	}
	t.Log("MessageHeader Decodes as: ", m)
	t.Log("Cap() = ", mbuf.Cap(), "Len() = \n", mbuf.Len())

	if err := c2.Decode(cbuf, binary.LittleEndian, c.SbeSchemaVersion(), true); err != nil {
		t.Log("Composite Decoding Error", err)
		t.Fail()
	}
	t.Log("Composite decodes as: ", c2)
	t.Log("Cap() = ", cbuf.Cap(), "Len() = \n", cbuf.Len())

	// c2.End.I = 18
	if c != c2 {
		t.Logf("c != c2\n%s\n%s", c, c2)
		t.Fail()
	}
}
func TestEncodeDecode(t *testing.T) {
	s := Simple0{2863311530, 123456, 7890, 63, -8, -16, -32, -64, 3.14, -3.14e7, [6]byte{'a', 'b', 'c', 'd', 'e', 'f'}, 'A', [2]int32{254, 255}}

	var sbuf = new(bytes.Buffer)
	if err := s.Encode(sbuf, binary.LittleEndian); err != nil {
		t.Log("Simple0 Encoding Error", err)
		t.Fail()
	}
	t.Log(s, " -> ", sbuf.Bytes())
	t.Log("Cap() = ", sbuf.Cap(), "Len() = ", sbuf.Len())

	m := MessageHeader{s.SbeBlockLength(), s.SbeTemplateId(), s.SbeSchemaId(), s.SbeSchemaVersion()}
	var mbuf = new(bytes.Buffer)
	if err := m.Encode(mbuf, binary.LittleEndian); err != nil {
		t.Log("MessageHeader Encoding Error", err)
		t.Fail()
	}
	t.Log(m, " -> ", mbuf.Bytes())
	t.Log("Cap() = ", mbuf.Cap(), "Len() = ", mbuf.Len())

	// Create a new empty MessageHeader and Simple0
	m = *new(MessageHeader)
	var s2 Simple0 = *new(Simple0)

	if err := m.Decode(mbuf, binary.LittleEndian, 0, true); err != nil {
		t.Log("MessageHeader Decoding Error", err)
		t.Fail()
	}
	t.Log("MessageHeader Decodes as: ", m)
	t.Log("Cap() = ", mbuf.Cap(), "Len() = ", mbuf.Len())

	if err := s2.Decode(sbuf, binary.LittleEndian, 0, true); err != nil {
		t.Log("Simple0 Decoding Error", err)
		t.Fail()
	}
	t.Log("Simple0 decodes as: ", s2)
	t.Log("Cap() = ", sbuf.Cap(), "Len() = ", sbuf.Len())

	// s2.Int2[0] = 18
	if s != s2 {
		t.Logf("s != s2\n%s\n%s", s, s2)
		t.Fail()
	}
}