buffer reuse strategies

master
Jordan Orelli 8 years ago
parent de8f42be96
commit c52e77dbad

@ -21,12 +21,13 @@ func (g dataGram) String() string {
return fmt.Sprintf("{dataGram cmd: %v tick: %v size: %d body: %x}", g.cmd, g.tick, len(g.body), g.body) return fmt.Sprintf("{dataGram cmd: %v tick: %v size: %d body: %x}", g.cmd, g.tick, len(g.body), g.body)
} }
func (g *dataGram) Open(m *messageFactory) (proto.Message, error) { func (g *dataGram) Open(m *messageFactory, pbuf *proto.Buffer) (proto.Message, error) {
msg, err := m.BuildDatagram(g.cmd) msg, err := m.BuildDatagram(g.cmd)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if err := proto.Unmarshal(g.body, msg); err != nil { pbuf.SetBuf(g.body)
if err := pbuf.Unmarshal(msg); err != nil {
return nil, err return nil, err
} }
return msg, nil return msg, nil

@ -17,11 +17,14 @@ type parser struct {
dumpDatagrams bool dumpDatagrams bool
dumpPackets bool dumpPackets bool
scratch []byte
pbuf *proto.Buffer
} }
func newParser(r io.Reader) *parser { func newParser(r io.Reader) *parser {
br := bufio.NewReaderSize(r, 1<<16) br := bufio.NewReaderSize(r, 1<<16)
return &parser{source: br} return &parser{source: br, scratch: make([]byte, 1<<17), pbuf: new(proto.Buffer)}
} }
func (p *parser) start() error { func (p *parser) start() error {
@ -57,7 +60,7 @@ func (p *parser) handleDataGram(d *dataGram) error {
return nil return nil
} }
msg, err := d.Open(&messages) msg, err := d.Open(&messages, p.pbuf)
if err != nil { if err != nil {
fmt.Printf("datagram open error: %v\n", err) fmt.Printf("datagram open error: %v\n", err)
return nil return nil
@ -75,8 +78,9 @@ func (p *parser) handleDemoPacket(packet *dota.CDemoPacket) error {
for { for {
t := entityType(br.ReadUBitVar()) t := entityType(br.ReadUBitVar())
s := br.ReadVarInt() s := br.ReadVarInt()
b := make([]byte, s) b := p.scratch[:s]
br.Read(b) br.Read(b)
p.pbuf.SetBuf(b)
switch err := br.Err(); err { switch err := br.Err(); err {
case nil: case nil:
break break
@ -93,7 +97,7 @@ func (p *parser) handleDemoPacket(packet *dota.CDemoPacket) error {
fmt.Printf("\tskipping entity of size %d, type %s: %v\n", len(b), t, err) fmt.Printf("\tskipping entity of size %d, type %s: %v\n", len(b), t, err)
continue continue
} }
if err := proto.Unmarshal(b, e); err != nil { if err := p.pbuf.Unmarshal(e); err != nil {
fmt.Printf("entity unmarshal error: %v\n", err) fmt.Printf("entity unmarshal error: %v\n", err)
} }
} }

Loading…
Cancel
Save