Repository: quic-go/quic-go
Pull request #4794 (2024-12-24 04:46:51)

wire: don't allocate an empty data slice for empty STREAM frames
Minor optimization, but it simplifies testing in some cases.

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/quic-go/quic-go/pull/4794?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=quic-go) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 83.91%. Comparing base [(`7d54aa4`)](https://app.codecov.io/gh/quic-go/quic-go/commit/7d54aa41a34484cb18dbbfc09b4d5db564af8f48?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=quic-go) to head [(`89c212b`)](https://app.codecov.io/gh/quic-go/quic-go/commit/89c212b35455e2b8fd1dd6af0168c60ce5daa648?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=quic-go).

<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff             @@
##           master    #4794      +/-   ##
==========================================
- Coverage   84.00%   83.91%   -0.10%     
==========================================
  Files         149      146       -3     
  Lines       16055    15713     -342     
==========================================
- Hits        13487    13184     -303     
+ Misses       2039     2018      -21     
+ Partials      529      511      -18     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/quic-go/quic-go/pull/4794?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=quic-go).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=quic-go).


Patch:
diff --git a/internal/wire/stream_frame.go b/internal/wire/stream_frame.go
index f9470ecd..cdc32722 100644
--- a/internal/wire/stream_frame.go
+++ b/internal/wire/stream_frame.go
@@ -58,7 +58,10 @@ func parseStreamFrame(b []byte, typ uint64, _ protocol.Version) (*StreamFrame, i
 
 	var frame *StreamFrame
 	if dataLen < protocol.MinStreamFrameBufferSize {
-		frame = &StreamFrame{Data: make([]byte, dataLen)}
+		frame = &StreamFrame{}
+		if dataLen > 0 {
+			frame.Data = make([]byte, dataLen)
+		}
 	} else {
 		frame = GetStreamFrame()
 		// The STREAM frame can't be larger than the StreamFrame we obtained from the buffer,
@@ -74,7 +77,7 @@ func parseStreamFrame(b []byte, typ uint64, _ protocol.Version) (*StreamFrame, i
 	frame.Fin = fin
 	frame.DataLenPresent = hasDataLen
 
-	if dataLen != 0 {
+	if dataLen > 0 {
 		copy(frame.Data, b)
 	}
 	if frame.Offset+frame.DataLen() > protocol.MaxByteCount {


Test patch:
diff --git a/internal/wire/stream_frame_test.go b/internal/wire/stream_frame_test.go
index 986e7893..b6177571 100644
--- a/internal/wire/stream_frame_test.go
+++ b/internal/wire/stream_frame_test.go
@@ -55,7 +55,7 @@ func TestParseStreamFrameAllowsEmpty(t *testing.T) {
 	require.NoError(t, err)
 	require.Equal(t, protocol.StreamID(0x1337), f.StreamID)
 	require.Equal(t, protocol.ByteCount(0x12345), f.Offset)
-	require.Empty(t, f.Data)
+	require.Nil(t, f.Data)
 	require.False(t, f.Fin)
 	require.Equal(t, len(data), l)
 }