Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

encoding/json: can add json.NewDecoderWithBytes function #7709

Closed
gopherbot opened this issue Apr 4, 2014 · 3 comments

Comments

Projects
None yet
2 participants
@gopherbot
Copy link

commented Apr 4, 2014

by runner.mei:

can add json.NewDecoderWithBytes function, use it like this:

  var buffer bytes.Buffer
  ch := make(chan io.Reader)
  // .....

  for _, reader := range ch {
    buffer.Reset()    // buffer is reused, redue gc
    ioutil.Copy(buffer, reader)
    dec := json.NewDecoderWithBytes(data)
    dec.UseNumber()
    dec.Decode(&m) 
  }
@gopherbot

This comment has been minimized.

Copy link
Author

commented Apr 6, 2014

Comment 1 by runner.mei:

Sorry, the above code was wrong.
  var buffer bytes.Buffer
  ch := make(chan io.Reader)
  // .....
  for _, reader := range ch {
    buffer.Reset()    // buffer is reused, redue gc
    ioutil.Copy(buffer, reader)
    dec := json.NewDecoderWithBytes(buffer.Bytes()) // This line of code above was wrong.
    dec.UseNumber()
    dec.Decode(&m) 
  }
@bradfitz

This comment has been minimized.

Copy link
Member

commented Apr 6, 2014

Comment 2:

You can use bytes.NewReader with json.NewDecoder.

Status changed to WorkingAsIntended.

@gopherbot

This comment has been minimized.

Copy link
Author

commented Apr 6, 2014

Comment 3 by runner.mei:

use json.NewDecoder with bytes.NewReader, should copy all bytes to Decoder, it is what I
want to avoid。I want to reuse buffer and zero copy, Reducing the number of GC

This issue was closed.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.