FAQ
Is there a way to write a string to a writer without allocating memory?

func foo(w io.Writer) { w.Write([]byte("hello")) }

The []byte cast does an allocation. When you want to write many small
strings, these unnecessary allocations can become very significant.

In this particular case, we need to write to a tcpconn, so a case specific
solution would be fine.


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Ian Lance Taylor at Sep 24, 2013 at 7:12 pm

    On Tue, Sep 24, 2013 at 11:55 AM, hstimer wrote:
    Is there a way to write a string to a writer without allocating memory?

    func foo(w io.Writer) { w.Write([]byte("hello")) }

    The []byte cast does an allocation. When you want to write many small
    strings, these unnecessary allocations can become very significant.

    In this particular case, we need to write to a tcpconn, so a case specific
    solution would be fine.
    In general, no, there is no way to avoid the allocation.

    In some cases you will avoid the copy by using io.WriteString, which
    will avoid the copy of the Writer in question provides a WriteString
    method. But TCPConn does not provide such a method, so there is no
    way to avoid the allocation in that case.

    (It might be an interesting exercise to plumb WriteString all the way
    through the net and syscall packages to make this more efficient, but
    I make no guarantee that such a change would be accepted.)

    Ian

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Hstimer at Sep 24, 2013 at 7:37 pm
    That was my first inclination, but I thought I would see if anyone had a
    better idea.
    On Tuesday, September 24, 2013 12:12:52 PM UTC-7, Ian Lance Taylor wrote:

    On Tue, Sep 24, 2013 at 11:55 AM, hstimer wrote:

    (It might be an interesting exercise to plumb WriteString all the way
    through the net and syscall packages to make this more efficient, but
    I make no guarantee that such a change would be accepted.)

    Ian
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Nikita at Sep 24, 2013 at 9:29 pm

    On Wednesday, September 25, 2013 12:55:25 AM UTC+6, hstimer wrote:
    Is there a way to write a string to a writer without allocating memory?

    func foo(w io.Writer) { w.Write([]byte("hello")) }

    The []byte cast does an allocation. When you want to write many small
    strings, these unnecessary allocations can become very significant.

    In this particular case, we need to write to a tcpconn, so a case specific
    solution would be fine.
    If you don't mind tying yourself to a particular implementation of Go
    runtime,
    try this function: https://gist.github.com/nnemkin/6690751.
    Declare it as "func stringSlice(s string) []byte;" in some .go file and
    add stringslice.c to your package.

    NO guarantees that it will work properly, use at your own risk!

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Benjamin Measures at Sep 24, 2013 at 11:29 pm

    On Tuesday, 24 September 2013 19:55:25 UTC+1, hstimer wrote:

    When you want to write many small strings, these unnecessary allocations
    can become very significant.
    How did you get these strings in the first place? If it's read from
    somewhere, then you can pre-avoid a string allocation by just using byte
    slices (or readers).

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Michael Henke 42 at Sep 25, 2013 at 1:27 pm
    There is a proposal that could solve your problem:
    https://code.google.com/p/go/issues/detail?id=5376

    Am Dienstag, 24. September 2013 20:55:25 UTC+2 schrieb hstimer:
    Is there a way to write a string to a writer without allocating memory?

    func foo(w io.Writer) { w.Write([]byte("hello")) }

    The []byte cast does an allocation. When you want to write many small
    strings, these unnecessary allocations can become very significant.

    In this particular case, we need to write to a tcpconn, so a case specific
    solution would be fine.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedSep 24, '13 at 6:55p
activeSep 25, '13 at 1:27p
posts6
users5
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase