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

A lot of memory allocations #295

Closed
MichaelMonashev opened this issue Nov 5, 2017 · 3 comments
Closed

A lot of memory allocations #295

MichaelMonashev opened this issue Nov 5, 2017 · 3 comments

Comments

@MichaelMonashev
Copy link

(pprof) list websocket.compressNoContextTakeover
Total: 2923046
ROUTINE ======================== github.com/gorilla/websocket.compressNoContextTakeover in /xxx/go/src/github.com/gorilla/websocket/compression.go
    278536     307901 (flat, cum) 10.53% of Total
         .          .     41:   return minCompressionLevel <= level && level <= maxCompressionLevel
         .          .     42:}
         .          .     43:
         .          .     44:func compressNoContextTakeover(w io.WriteCloser, level int) io.WriteCloser {
         .          .     45:   p := &flateWriterPools[level-minCompressionLevel]
    131076     131076     46:   tw := &truncWriter{w: w}
         .          .     47:   fw, _ := p.Get().(*flate.Writer)
         .          .     48:   if fw == nil {
         .      29365     49:           fw, _ = flate.NewWriter(tw, level)
         .          .     50:   } else {
         .          .     51:           fw.Reset(tw)
         .          .     52:   }
    147460     147460     53:   return &flateWriteWrapper{fw: fw, tw: tw, p: p}
         .          .     54:}
         .          .     55:
         .          .     56:// truncWriter is an io.Writer that writes all but the last four bytes of the
         .          .     57:// stream to another io.Writer.
         .          .     58:type truncWriter struct {
(pprof) list websocket.decompressNoContextTakeover
Total: 2923046
ROUTINE ======================== github.com/gorilla/websocket.decompressNoContextTakeover in /xxx/go/src/github.com/gorilla/websocket/client.go
    114691     114691 (flat, cum)  3.92% of Total
         .          .     13:   "io"
         .          .     14:   "io/ioutil"
         .          .     15:   "net"
         .          .     16:   "net/http"
         .          .     17:   "net/url"
    114691     114691     18:   "strings"
         .          .     19:   "time"
         .          .     20:)
         .          .     21:
         .          .     22:// ErrBadHandshake is returned when the server response to opening handshake is
         .          .     23:// invalid.
ROUTINE ======================== github.com/gorilla/websocket.decompressNoContextTakeover in /xxx/go/src/github.com/gorilla/websocket/compression.go
     65537     337942 (flat, cum) 11.56% of Total
         .          .     30:   // Add four bytes as specified in RFC
         .          .     31:   "\x00\x00\xff\xff" +
         .          .     32:           // Add final block to squelch unexpected EOF error from flate reader.
         .          .     33:           "\x01\x00\x00\xff\xff"
         .          .     34:
         .       4278     35:   fr, _ := flateReaderPool.Get().(io.ReadCloser)
         .     268127     36:   fr.(flate.Resetter).Reset(io.MultiReader(r, strings.NewReader(tail)), nil)
     65537      65537     37:   return &flateReadWrapper{fr}
         .          .     38:}
         .          .     39:
         .          .     40:func isValidCompressionLevel(level int) bool {
         .          .     41:   return minCompressionLevel <= level && level <= maxCompressionLevel
         .          .     42:}
(pprof) list NextWriter
Total: 2923046
ROUTINE ======================== github.com/gorilla/websocket.(*Conn).NextWriter in /xxx/go/src/github.com/gorilla/websocket/conn.go
    240309     548210 (flat, cum) 18.75% of Total
         .          .    490:   }
         .          .    491:
         .          .    492:   mw := &messageWriter{
         .          .    493:           c:         c,
         .          .    494:           frameType: messageType,
    240309     240309    495:           pos:       maxFrameHeaderSize,
         .          .    496:   }
         .          .    497:   c.writer = mw
         .          .    498:   if c.newCompressionWriter != nil && c.enableWriteCompression && isData(messageType) {
         .     307901    499:           w := c.newCompressionWriter(c.writer, c.compressionLevel)
         .          .    500:           mw.compress = true
         .          .    501:           c.writer = w
         .          .    502:   }
         .          .    503:   return c.writer, nil
         .          .    504:}
(pprof) list NextReader
Total: 2923046
ROUTINE ======================== github.com/gorilla/websocket.(*Conn).NextReader in /xxx/go/src/github.com/gorilla/websocket/conn.go
     65536     518681 (flat, cum) 17.74% of Total
         .          .    925:// permanent. Once this method returns a non-nil error, all subsequent calls to
         .          .    926:// this method return the same error.
         .          .    927:func (c *Conn) NextReader() (messageType int, r io.Reader, err error) {
         .          .    928:   // Close previous reader, only relevant for decompression.
         .          .    929:   if c.reader != nil {
         .        512    930:           c.reader.Close()
         .          .    931:           c.reader = nil
         .          .    932:   }
         .          .    933:
         .          .    934:   c.messageReader = nil
         .          .    935:   c.readLength = 0
         .          .    936:
         .          .    937:   for c.readErr == nil {
         .          .    938:           frameType, err := c.advanceFrame()
         .          .    939:           if err != nil {
         .          .    940:                   c.readErr = hideTempErr(err)
         .          .    941:                   break
         .          .    942:           }
         .          .    943:           if frameType == TextMessage || frameType == BinaryMessage {
     65536      65536    944:                   c.messageReader = &messageReader{c}
         .          .    945:                   c.reader = c.messageReader
         .          .    946:                   if c.readDecompress {
         .     452633    947:                           c.reader = c.newDecompressionReader(c.reader)
         .          .    948:                   }
         .          .    949:                   return frameType, c.reader, nil
         .          .    950:           }
         .          .    951:   }
         .          .    952:
@garyburd
Copy link
Contributor

garyburd commented Nov 5, 2017

Is this a duplicate of #203?

@MichaelMonashev
Copy link
Author

May bee.

@garyburd
Copy link
Contributor

garyburd commented Nov 6, 2017

Closing as duplicate of #203.

@garyburd garyburd closed this as completed Nov 6, 2017
@gorilla gorilla locked and limited conversation to collaborators Feb 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants