Skip to content

fmt: pp should implement WriteString() #20786

Closed

Description

@pierrre

What version of Go are you using (go version)?

1.9beta1

What did you do?

In my code, I'm implementing fmt.Formatter.
In order to write a string to the fmt.State, I'm calling io.WriteString().
I know that this function has an optimization that allows to call WriteString() if w implements it.
Sadly, in my case the "real" type of fmt.State is fmt.pp.
fmt.pp only has Write().

What did you expect to see?

I think we should add:

func (p *pp) WriteString(s string) (ret int, err error) {
	p.buf.WriteString(s)
	return len(s), nil
}

in my case, it greatly improve performance and reduces memory allocation.
We could also implement WriteByte() and WriteRune(), since fmt.buffer has these methods.

Activity

  1. added
    NeedsFixThe path to resolution is known, but the work has not been done.
    on Jun 24, 2017
  2. added this to the Go1.10 milestone on Jun 24, 2017
  3. bradfitz commented on Jun 24, 2017

    @bradfitz
    Contributor

    Sounds reasonable.

  4. nikhita commented on Jun 25, 2017

    @nikhita

    I want to contribute to Go and noticed the Help Wanted label. If no one is working on it, I'd like to take it! :)

  5. martisch commented on Jun 25, 2017

    @martisch
    Contributor

    io.WriteString() uses a runtime check so it looks like it should work without changing the State Interface definition. For go2 we could then think about wether it makes sense to update the State Interface.

    @nikhita go ahead if you want too. Please add r@golang.org and moehrmann@google.com as reviewers. We can review before go1.10 but we can only submit when the code tree opens for go1.10 start of August.

  6. nikhita commented on Jun 25, 2017

    @nikhita

    @martisch will do that, thank you.

  7. SCKelemen commented on Jul 7, 2017

    @SCKelemen
    Contributor
  8. martisch commented on Jul 10, 2017

    @martisch
    Contributor

    @nikhita
    did you have time to start your first CL for go?
    Any blockers encountered that we can help with?

  9. nikhita commented on Jul 10, 2017

    @nikhita

    @martisch
    sorry, was busy at work - didn't have time to work on it.
    But will try working on it on my flight to GopherCon now. :)
    Maybe I can even ask any questions I have during the Go contributor workshop at GopherCon? :)

  10. martisch commented on Jul 10, 2017

    @martisch
    Contributor

    no problem. Asking at the contributor workshop sounds good.

  11. gopherbot commented on Jul 15, 2017

    @gopherbot
    Contributor

    CL https://golang.org/cl/48871 mentions this issue.

  12. rajathagasthya commented on Sep 1, 2017

    @rajathagasthya
    Contributor

    Just checking if this is still active. I see in the CL that it needs tests. I can help with that, if need be.

  13. gopherbot commented on Sep 4, 2017

    @gopherbot
    Contributor

    Change https://golang.org/cl/61430 mentions this issue: fmt: Implement pp.WriteString() method

  14. stemar94 commented on Sep 4, 2017

    @stemar94
    Contributor

    I mailed another way on how to resolve this. gopherbot will notice soon.

  15. gopherbot commented on Sep 4, 2017

    @gopherbot
    Contributor

    Change https://golang.org/cl/61450 mentions this issue: fmt: make buffer satisfy io interfaces

  16. gopherbot commented on Sep 4, 2017

    @gopherbot
    Contributor

    Change https://golang.org/cl/61451 mentions this issue: fmt: make pp a composition with buffer

  17. robpike commented on Sep 7, 2017

    @robpike
    Contributor

    Adding WriteString makes some sense, but please let's not open up an internal type to the full pressure of external I/O interfaces.

  18. martisch commented on Sep 7, 2017

    @martisch
    Contributor

    Following up here instead of on the CL discussion.

    I would also favor to leave the internal buffer methods unexposed and only implement WriteString on pp as the proposal suggested. As far as i know there has been not yet been data or a request to add more io methods beyond WriteString. Keeping the api footprint low imposes less constraints on internals for future changes and avoids needing to implement them explicitly on pp later if buffer is replaced by something with different interfaces for internal optimization.

    If it is decided to expose all the io methods from the buffer it would be good to make sure that there are no regressions due to the compiler not optimizing all the overhead away for the internal use of those methods (e.g. due to inline budget changes).

  19. locked and limited conversation to collaborators on Sep 20, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions