You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iggy.apache.org by "matanper (via GitHub)" <gi...@apache.org> on 2026/06/08 21:40:24 UTC

[PR] perf(foreign/go/tcp): pool the request-path wire payload [iggy]

matanper opened a new pull request, #3442:
URL: https://github.com/apache/iggy/pull/3442

   ## Which issue does this PR address?
   
   Closes #3441 
   
   ## Rationale
   
   Reduces per-RPC allocation pressure on the Go client's TCP poll path
   
   ## What changed?
   
   
   Before: Every poll RPC freshly allocated the request wire buffer, re-encoded
   immutable `Identifier` values (stream, topic, consumer, partition), and
   read the 8-byte response status header into a heap allocation
   
   Now: `Identifier`  caches its wire bytes at construction (`NewIdentifier`)
   and exposes `MarshalledSize` + `AppendBinary`; the same pair is added
   to `command.PollMessages`. `IggyTcpClient.do` encodes the full wire
   payload directly into a `sync.Pool`-backed buffer through the new
   `AppendBinary` path, and the response status header reads into a
   stack-local array via a small `readInto` helper.
   
   ## Local Execution
   
   Passed
   
   ## AI Usage
   
   1. Claude 
   2. entire implementation
   3. run it locally on iggy server
   4. yes


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@iggy.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] perf(go): pool the request-path wire payload [iggy]

Posted by "chengxilo (via GitHub)" <gi...@apache.org>.
chengxilo commented on code in PR #3442:
URL: https://github.com/apache/iggy/pull/3442#discussion_r3408962777


##########
foreign/go/contracts/identifier.go:
##########


Review Comment:
   `Identifier.MarshalBinary` and `AppendBinary` have different internal logic: MarshalBinary reads from Kind/Length/Value fields, while AppendBinary reads from the encoded cache when available. If someone mutates the exported fields after construction, they produce different bytes.
   
   Since `PollMessages.MarshalBinary` already delegates to `AppendBinary,` I'd suggest doing the same here for consistency:
   
   ```go
   func (id Identifier) MarshalBinary() ([]byte, error) {
       return id.AppendBinary(nil)
   }
   ```
   
   As a follow-up, it might be worth making `Kind/Length/Value` unexported with read-only accessors, i.e. `Kind()/Length()/Value()`. This can make sure we remove the inconsisitency.
   
   Additionally, I'm not sure if it worth fixing, but currently everytime when we create an Identifier, we do the encoding. I'm thinking if we can make it lazy encoding, only encode it when  `MarshalBinary` or `AppendBinary ` is called. And, with current approach, I also don't understand why we still have the logic for encoding in both functions. Isn't it enough to just return `encoded`? For `Identifier{}`, we can just return empty bytes slice. 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@iggy.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] perf(go): pool the request-path wire payload [iggy]

Posted by "chengxilo (via GitHub)" <gi...@apache.org>.
chengxilo commented on code in PR #3442:
URL: https://github.com/apache/iggy/pull/3442#discussion_r3408962777


##########
foreign/go/contracts/identifier.go:
##########


Review Comment:
   `Identifier.MarshalBinary` and `AppendBinary` have different internal logic: MarshalBinary reads from Kind/Length/Value fields, while AppendBinary reads from the encoded cache when available. If someone mutates the exported fields after construction, they produce different bytes.
   
   Since `PollMessages.MarshalBinary` already delegates to `AppendBinary,` I'd suggest doing the same here for consistency:
   
   ```go
   func (id Identifier) MarshalBinary() ([]byte, error) {
       return id.AppendBinary(nil)
   }
   ```
   
   As a follow-up, it might be worth making `Kind/Length/Value` unexported with read-only accessors, i.e. `Kind()/Length()/Value()`. This can make sure we remove the inconsisitency.
   
   Additionally, I'm not sure if it worth fixing, but currently everytime when we create an Identifier, we do the encoding. I'm thinking if we can make it lazy encoding, only encode it when  `MarshalBinary` or `AppendBinary ` is called. And, with current approach, I also don't understand why we still have the logic for encoding in both functions. Isn't it enough to just return `encoded`?



##########
foreign/go/client/tcp/tcp_core.go:
##########
@@ -356,6 +414,8 @@ func (c *IggyTcpClient) invalidateConnLocked() {
 	c.state = iggcon.StateDisconnected
 }
 
+// createPayload builds wire-format request bytes; the hot path uses
+// encodeWireRequest with a pooled buffer instead.
 func createPayload(message []byte, command command.Code) []byte {

Review Comment:
   If we want to keep it. We probably should mark it as deprecated. In my opinion, it won't hurt to remove it after code review tho.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@iggy.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] perf(go): pool the request-path wire payload [iggy]

Posted by "matanper (via GitHub)" <gi...@apache.org>.
matanper commented on code in PR #3442:
URL: https://github.com/apache/iggy/pull/3442#discussion_r3379363581


##########
foreign/go/client/tcp/tcp_core.go:
##########
@@ -250,16 +284,64 @@ func (c *IggyTcpClient) write(payload []byte) (int, error) {
 	return totalWritten, nil
 }
 
-// do sends the command to the Iggy server and returns the response.
+// do sends the command and returns the response body. Commands implementing
+// the appender interface encode directly into a pooled buffer.
 func (c *IggyTcpClient) do(ctx context.Context, cmd command.Command) ([]byte, error) {
-	data, err := cmd.MarshalBinary()
+	bp := acquireRequestBuf()
+	buf, err := encodeWireRequest(*bp, cmd)
 	if err != nil {
+		releaseRequestBuf(bp)
 		return nil, err
 	}
-	return c.sendAndFetchResponse(ctx, data, cmd.Code())
+	*bp = buf
+
+	resp, err := c.sendWireAndFetchResponse(ctx, buf)
+	releaseRequestBuf(bp)
+	return resp, err
 }
 
+// encodeWireRequest writes the wire-format request (4-byte length, 4-byte
+// code, then body) into buf, growing it as needed.
+func encodeWireRequest(buf []byte, cmd command.Command) ([]byte, error) {
+	buf = buf[:0]
+	if a, ok := cmd.(appender); ok {
+		bodySize := a.MarshalledSize()
+		total := 8 + bodySize
+		if cap(buf) < total {
+			buf = make([]byte, 8, total)
+		} else {
+			buf = buf[:8]
+		}
+		// total wire message length = body + 4-byte command code
+		binary.LittleEndian.PutUint32(buf[0:4], uint32(bodySize+4))

Review Comment:
   `MarshalledSize` is only a hint for the pool buffer, and it seems the `PollMessages` is 58 bytes while the pool returns buffer with initial size of 256.
   In practice hint never actually fired, so removing it for simplicity and getting rid of the drift risk



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@iggy.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] perf(go): pool the request-path wire payload [iggy]

Posted by "matanper (via GitHub)" <gi...@apache.org>.
matanper commented on code in PR #3442:
URL: https://github.com/apache/iggy/pull/3442#discussion_r3379700600


##########
foreign/go/contracts/identifier.go:
##########
@@ -89,6 +100,14 @@ func (id Identifier) String() (string, error) {
 	return string(id.Value), nil
 }
 
+// MarshalledSize returns the byte length of the AppendBinary encoding.
+func (id Identifier) MarshalledSize() int {
+	if len(id.encoded) > 0 {
+		return len(id.encoded)
+	}
+	return 2 + id.Length

Review Comment:
   removing `MarshalledSize`



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@iggy.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] perf(foreign/go/tcp): pool the request-path wire payload [iggy]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #3442:
URL: https://github.com/apache/iggy/pull/3442#issuecomment-4653793870

   <!-- iggy-pr-triage-welcome -->
   Thanks for the PR. It is labeled `S-waiting-on-review` and queued for review.
   
   Slash commands (own line, regular comment) move it around the queue:
   
   - `/ready` - back to `S-waiting-on-review` after addressing feedback
   - `/author` - flip to `S-waiting-on-author` while you finish changes
   - `/request-review @user-or-team` - request a reviewer
   
   See [CONTRIBUTING.md](https://github.com/apache/iggy/blob/master/CONTRIBUTING.md#pr-triage-commands) for details.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@iggy.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] perf(go): pool the request-path wire payload [iggy]

Posted by "hubcio (via GitHub)" <gi...@apache.org>.
hubcio merged PR #3442:
URL: https://github.com/apache/iggy/pull/3442


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@iggy.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] perf(go): pool the request-path wire payload [iggy]

Posted by "matanper (via GitHub)" <gi...@apache.org>.
matanper commented on code in PR #3442:
URL: https://github.com/apache/iggy/pull/3442#discussion_r3409224033


##########
foreign/go/contracts/identifier.go:
##########


Review Comment:
   I delegated `Identifier.MarshalBinary` to `AppendBinary`
   
   I'd be happy to also unexport `Kind/Length/Value`, after doing that `NewIdentifier` becomes single entry point and `encoded` the source of truth, so we can drop the fallback in `AppendBinary`. The only downside is breaking the public API - let me know if you want it in this PR or in separate one with a changelog / deprecation note 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@iggy.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] perf(go): pool the request-path wire payload [iggy]

Posted by "hubcio (via GitHub)" <gi...@apache.org>.
hubcio commented on code in PR #3442:
URL: https://github.com/apache/iggy/pull/3442#discussion_r3378913861


##########
foreign/go/client/tcp/tcp_core.go:
##########
@@ -312,34 +394,28 @@ func (c *IggyTcpClient) sendAndFetchResponse(ctx context.Context, message []byte
 	return result, nil
 }
 
-func (c *IggyTcpClient) sendLocked(message []byte, command command.Code) ([]byte, error) {
-	payload := createPayload(message, command)
-	if _, err := c.write(payload); err != nil {
+func (c *IggyTcpClient) sendLocked(wirePayload []byte) ([]byte, error) {
+	if _, err := c.write(wirePayload); err != nil {
 		c.invalidateConnLocked()
 		return nil, err
 	}
 
-	readBytes, buffer, err := c.read(ResponseInitialBytesLength)
-	if err != nil {
+	var header [ResponseInitialBytesLength]byte

Review Comment:
   small thing - the commit message says this reads into a stack-local buffer to drop an allocation, but `header[:]` is passed to `c.conn.Read` (an interface call) so escape analysis moves it to the heap anyway (`go build -gcflags=-m` shows `moved to heap: header`). no regression since the old `read(8)` also heap-allocated 8 bytes - just worth fixing the wording so it doesn't read as a win it isn't.



##########
foreign/go/client/tcp/tcp_core.go:
##########
@@ -250,16 +284,64 @@ func (c *IggyTcpClient) write(payload []byte) (int, error) {
 	return totalWritten, nil
 }
 
-// do sends the command to the Iggy server and returns the response.
+// do sends the command and returns the response body. Commands implementing
+// the appender interface encode directly into a pooled buffer.
 func (c *IggyTcpClient) do(ctx context.Context, cmd command.Command) ([]byte, error) {
-	data, err := cmd.MarshalBinary()
+	bp := acquireRequestBuf()
+	buf, err := encodeWireRequest(*bp, cmd)
 	if err != nil {
+		releaseRequestBuf(bp)
 		return nil, err
 	}
-	return c.sendAndFetchResponse(ctx, data, cmd.Code())
+	*bp = buf
+
+	resp, err := c.sendWireAndFetchResponse(ctx, buf)
+	releaseRequestBuf(bp)
+	return resp, err
 }
 
+// encodeWireRequest writes the wire-format request (4-byte length, 4-byte
+// code, then body) into buf, growing it as needed.
+func encodeWireRequest(buf []byte, cmd command.Command) ([]byte, error) {
+	buf = buf[:0]
+	if a, ok := cmd.(appender); ok {
+		bodySize := a.MarshalledSize()
+		total := 8 + bodySize
+		if cap(buf) < total {
+			buf = make([]byte, 8, total)
+		} else {
+			buf = buf[:8]
+		}
+		// total wire message length = body + 4-byte command code
+		binary.LittleEndian.PutUint32(buf[0:4], uint32(bodySize+4))

Review Comment:
   the 4-byte length prefix here is written from `MarshalledSize()` (a predicted size) before `AppendBinary` fills the body, and the two are never reconciled. if a command's `MarshalledSize()` ever disagrees with the bytes `AppendBinary` actually writes, the prefix mis-sizes the frame and the single persistent tcp stream desyncs permanently - no req-id, no resync, every later request misframed. the old `createPayload` derived the prefix from the realized body length so it couldn't drift.
   
   today only `PollMessages` takes this branch and it's correct, but the guarantee is now test-enforced instead of structural. cheap fix - realize the body first, then set the prefix from the actual length:
   
   ```go
   out, err := a.AppendBinary(buf)
   if err != nil {
       return nil, err
   }
   binary.LittleEndian.PutUint32(out[0:4], uint32(len(out)-4))
   return out, nil
   ```
   
   that's `len(out)-4` (code + body), still little-endian. makes `MarshalledSize` a pure capacity hint - if it's wrong you get a realloc, never a corrupt frame.



##########
foreign/go/contracts/identifier.go:
##########
@@ -89,6 +100,14 @@ func (id Identifier) String() (string, error) {
 	return string(id.Value), nil
 }
 
+// MarshalledSize returns the byte length of the AppendBinary encoding.
+func (id Identifier) MarshalledSize() int {
+	if len(id.encoded) > 0 {
+		return len(id.encoded)
+	}
+	return 2 + id.Length

Review Comment:
   this fallback returns `2 + id.Length` but `AppendBinary` below writes `2 + len(id.Value)`. they only agree when `Length == len(Value)`. `NewIdentifier` always ties them, but the fields are exported so a hand-built `iggcon.Identifier{Kind, Length, Value}` with a mismatched `Length` is publicly constructible, and `PollMessages` feeds it straight to the encode path with no validation. pre-PR that input was harmless (wire header and body both keyed off `Length`); now the predicted size and the realized append diverge and corrupt the stream.
   
   make this `2 + len(id.Value)` so it can't disagree with `AppendBinary`, and route `MarshalBinary` through `AppendBinary` too (`out := make([]byte, 0, MarshalledSize()); return AppendBinary(out)`) so there's a single source of truth. byte-identical output, same alloc count.



##########
foreign/go/client/tcp/tcp_core.go:
##########
@@ -250,16 +284,64 @@ func (c *IggyTcpClient) write(payload []byte) (int, error) {
 	return totalWritten, nil
 }
 
-// do sends the command to the Iggy server and returns the response.
+// do sends the command and returns the response body. Commands implementing
+// the appender interface encode directly into a pooled buffer.
 func (c *IggyTcpClient) do(ctx context.Context, cmd command.Command) ([]byte, error) {
-	data, err := cmd.MarshalBinary()
+	bp := acquireRequestBuf()
+	buf, err := encodeWireRequest(*bp, cmd)
 	if err != nil {
+		releaseRequestBuf(bp)
 		return nil, err
 	}
-	return c.sendAndFetchResponse(ctx, data, cmd.Code())
+	*bp = buf
+
+	resp, err := c.sendWireAndFetchResponse(ctx, buf)
+	releaseRequestBuf(bp)
+	return resp, err
 }
 
+// encodeWireRequest writes the wire-format request (4-byte length, 4-byte
+// code, then body) into buf, growing it as needed.
+func encodeWireRequest(buf []byte, cmd command.Command) ([]byte, error) {
+	buf = buf[:0]
+	if a, ok := cmd.(appender); ok {
+		bodySize := a.MarshalledSize()
+		total := 8 + bodySize
+		if cap(buf) < total {
+			buf = make([]byte, 8, total)
+		} else {
+			buf = buf[:8]
+		}
+		// total wire message length = body + 4-byte command code
+		binary.LittleEndian.PutUint32(buf[0:4], uint32(bodySize+4))
+		binary.LittleEndian.PutUint32(buf[4:8], uint32(cmd.Code()))
+		return a.AppendBinary(buf)
+	}
+	body, err := cmd.MarshalBinary()
+	if err != nil {
+		return nil, err
+	}
+	total := 8 + len(body)
+	if cap(buf) < total {
+		buf = make([]byte, 0, total)
+	}
+	buf = buf[:8]
+	binary.LittleEndian.PutUint32(buf[0:4], uint32(len(body)+4))
+	binary.LittleEndian.PutUint32(buf[4:8], uint32(cmd.Code()))
+	buf = append(buf, body...)
+	return buf, nil
+}
+
+// sendAndFetchResponse wraps a bare command body with the wire header and
+// sends it. Kept for callers/tests that build payloads from a body + code.
 func (c *IggyTcpClient) sendAndFetchResponse(ctx context.Context, message []byte, command command.Code) ([]byte, error) {

Review Comment:
   after this PR `do()` routes everything through `sendWireAndFetchResponse`, so `sendAndFetchResponse` has no production callers left - only the ctx tests use it. could drop it and have those tests call `sendWireAndFetchResponse(ctx, createPayload(...))` directly, keeping `createPayload` as the test oracle. one less method to maintain.



##########
foreign/go/benchmarks/poll_encode_benchmark_test.go:
##########
@@ -0,0 +1,104 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package benchmarks
+
+import (
+	"testing"
+
+	iggcon "github.com/apache/iggy/foreign/go/contracts"
+	"github.com/apache/iggy/foreign/go/internal/command"
+)
+
+func newPollRequest(b *testing.B) *command.PollMessages {
+	b.Helper()
+	consumerId, _ := iggcon.NewIdentifier(uint32(42))
+	streamId, _ := iggcon.NewIdentifier("test_stream_id")
+	topicId, _ := iggcon.NewIdentifier("test_topic_id")
+	pid := uint32(1)
+	return &command.PollMessages{
+		Consumer:    iggcon.NewSingleConsumer(consumerId),
+		StreamId:    streamId,
+		TopicId:     topicId,
+		PartitionId: &pid,
+		Strategy:    iggcon.NextPollingStrategy(),
+		Count:       1000,
+		AutoCommit:  true,
+	}
+}
+
+// BenchmarkPollMessagesMarshalBinary measures the legacy hot path: a fresh
+// []byte allocated per call.
+func BenchmarkPollMessagesMarshalBinary(b *testing.B) {
+	req := newPollRequest(b)
+	b.ReportAllocs()
+	b.ResetTimer()
+	for i := 0; i < b.N; i++ {
+		out, err := req.MarshalBinary()
+		if err != nil {
+			b.Fatal(err)
+		}
+		_ = out
+	}
+}
+
+// BenchmarkPollMessagesAppendBinary measures the new hot path: encoding into
+// a caller-owned, reusable buffer.
+func BenchmarkPollMessagesAppendBinary(b *testing.B) {
+	req := newPollRequest(b)
+	buf := make([]byte, 0, req.MarshalledSize())
+	b.ReportAllocs()
+	b.ResetTimer()
+	for i := 0; i < b.N; i++ {
+		out, err := req.AppendBinary(buf[:0])
+		if err != nil {
+			b.Fatal(err)
+		}
+		buf = out
+	}
+}
+
+// BenchmarkIdentifierMarshalBinary measures the still-allocating
+// MarshalBinary path on the constant Identifier values used per poll.
+func BenchmarkIdentifierMarshalBinary(b *testing.B) {
+	id, _ := iggcon.NewIdentifier("test_stream_id")
+	b.ReportAllocs()
+	b.ResetTimer()
+	for i := 0; i < b.N; i++ {
+		out, err := id.MarshalBinary()
+		if err != nil {
+			b.Fatal(err)
+		}
+		_ = out

Review Comment:
   `_ = out` lets the compiler eliminate the allocation - this benchmark reports 0 allocs even though the comment above calls it the "still-allocating" path. assign to a package-level sink (`var sink []byte` then `sink = out`) so the result escapes and the real cost (1 alloc) shows up. the `PollMessages` pair above doesn't have this problem because its body slice genuinely escapes.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@iggy.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] perf(go): pool the request-path wire payload [iggy]

Posted by "matanper (via GitHub)" <gi...@apache.org>.
matanper commented on code in PR #3442:
URL: https://github.com/apache/iggy/pull/3442#discussion_r3409205621


##########
foreign/go/client/tcp/tcp_core.go:
##########
@@ -356,6 +414,8 @@ func (c *IggyTcpClient) invalidateConnLocked() {
 	c.state = iggcon.StateDisconnected
 }
 
+// createPayload builds wire-format request bytes; the hot path uses
+// encodeWireRequest with a pooled buffer instead.
 func createPayload(message []byte, command command.Code) []byte {

Review Comment:
   Agree, I'll remove it and fix tests 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@iggy.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] perf(go): pool the request-path wire payload [iggy]

Posted by "hubcio (via GitHub)" <gi...@apache.org>.
hubcio commented on PR #3442:
URL: https://github.com/apache/iggy/pull/3442#issuecomment-4715548411

   @matanper once PR is ready for review please always flip the label using
   /ready
   (put it somewhere in the top level comment body beginning line)
   this will change label to `S-waiting-for-review` :) and it's info for maintainers to start checking again.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@iggy.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] perf(go): pool the request-path wire payload [iggy]

Posted by "codecov[bot] (via GitHub)" <gi...@apache.org>.
codecov[bot] commented on PR #3442:
URL: https://github.com/apache/iggy/pull/3442#issuecomment-4653981127

   ## [Codecov](https://app.codecov.io/gh/apache/iggy/pull/3442?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) Report
   :x: Patch coverage is `54.62963%` with `49 lines` in your changes missing coverage. Please review.
   :white_check_mark: Project coverage is 73.26%. Comparing base ([`dfa2afd`](https://app.codecov.io/gh/apache/iggy/commit/dfa2afd435ebef41abf4ea9df4b41f554f9aa59d?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)) to head ([`2ccfc65`](https://app.codecov.io/gh/apache/iggy/commit/2ccfc659a3a511c1f102f123adb85e9e23761b54?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)).
   
   | [Files with missing lines](https://app.codecov.io/gh/apache/iggy/pull/3442?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | Patch % | Lines |
   |---|---|---|
   | [foreign/go/client/tcp/tcp\_core.go](https://app.codecov.io/gh/apache/iggy/pull/3442?src=pr&el=tree&filepath=foreign%2Fgo%2Fclient%2Ftcp%2Ftcp_core.go&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-Zm9yZWlnbi9nby9jbGllbnQvdGNwL3RjcF9jb3JlLmdv) | 29.23% | [44 Missing and 2 partials :warning: ](https://app.codecov.io/gh/apache/iggy/pull/3442?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
   | [foreign/go/internal/command/message.go](https://app.codecov.io/gh/apache/iggy/pull/3442?src=pr&el=tree&filepath=foreign%2Fgo%2Finternal%2Fcommand%2Fmessage.go&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-Zm9yZWlnbi9nby9pbnRlcm5hbC9jb21tYW5kL21lc3NhZ2UuZ28=) | 86.95% | [0 Missing and 3 partials :warning: ](https://app.codecov.io/gh/apache/iggy/pull/3442?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) |
   
   <details><summary>Additional details and impacted files</summary>
   
   
   
   ```diff
   @@             Coverage Diff              @@
   ##             master    #3442      +/-   ##
   ============================================
   - Coverage     74.31%   73.26%   -1.06%     
     Complexity      943      943              
   ============================================
     Files          1245     1245              
     Lines        121653   121705      +52     
     Branches      97959    97959              
   ============================================
   - Hits          90410    89165    -1245     
   - Misses        28288    29678    +1390     
   + Partials       2955     2862      -93     
   ```
   
   | [Components](https://app.codecov.io/gh/apache/iggy/pull/3442/components?src=pr&el=components&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | Coverage Δ | |
   |---|---|---|
   | [Rust Core](https://app.codecov.io/gh/apache/iggy/pull/3442/components?src=pr&el=component&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `75.41% <ø> (ø)` | |
   | [Java SDK](https://app.codecov.io/gh/apache/iggy/pull/3442/components?src=pr&el=component&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `58.44% <ø> (ø)` | |
   | [C# SDK](https://app.codecov.io/gh/apache/iggy/pull/3442/components?src=pr&el=component&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `69.92% <ø> (ø)` | |
   | [Python SDK](https://app.codecov.io/gh/apache/iggy/pull/3442/components?src=pr&el=component&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `81.06% <ø> (ø)` | |
   | [PHP SDK](https://app.codecov.io/gh/apache/iggy/pull/3442/components?src=pr&el=component&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `83.57% <ø> (ø)` | |
   | [Node SDK](https://app.codecov.io/gh/apache/iggy/pull/3442/components?src=pr&el=component&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `91.35% <ø> (ø)` | |
   | [Go SDK](https://app.codecov.io/gh/apache/iggy/pull/3442/components?src=pr&el=component&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `14.61% <54.62%> (-25.59%)` | :arrow_down: |
   
   | [Files with missing lines](https://app.codecov.io/gh/apache/iggy/pull/3442?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | Coverage Δ | |
   |---|---|---|
   | [foreign/go/contracts/identifier.go](https://app.codecov.io/gh/apache/iggy/pull/3442?src=pr&el=tree&filepath=foreign%2Fgo%2Fcontracts%2Fidentifier.go&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-Zm9yZWlnbi9nby9jb250cmFjdHMvaWRlbnRpZmllci5nbw==) | `70.14% <100.00%> (-9.10%)` | :arrow_down: |
   | [foreign/go/internal/command/message.go](https://app.codecov.io/gh/apache/iggy/pull/3442?src=pr&el=tree&filepath=foreign%2Fgo%2Finternal%2Fcommand%2Fmessage.go&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-Zm9yZWlnbi9nby9pbnRlcm5hbC9jb21tYW5kL21lc3NhZ2UuZ28=) | `72.07% <86.95%> (-3.15%)` | :arrow_down: |
   | [foreign/go/client/tcp/tcp\_core.go](https://app.codecov.io/gh/apache/iggy/pull/3442?src=pr&el=tree&filepath=foreign%2Fgo%2Fclient%2Ftcp%2Ftcp_core.go&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-Zm9yZWlnbi9nby9jbGllbnQvdGNwL3RjcF9jb3JlLmdv) | `23.20% <29.23%> (-31.00%)` | :arrow_down: |
   
   ... and [39 files with indirect coverage changes](https://app.codecov.io/gh/apache/iggy/pull/3442/indirect-changes?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)
   </details>
   <details><summary> :rocket: New features to boost your workflow: </summary>
   
   - :snowflake: [Test Analytics](https://docs.codecov.com/docs/test-analytics): Detect flaky tests, report on failures, and find test suite problems.
   - :package: [JS Bundle Analysis](https://docs.codecov.com/docs/javascript-bundle-analysis): Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
   </details>


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@iggy.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] perf(go): pool the request-path wire payload [iggy]

Posted by "chengxilo (via GitHub)" <gi...@apache.org>.
chengxilo commented on code in PR #3442:
URL: https://github.com/apache/iggy/pull/3442#discussion_r3409731337


##########
foreign/go/client/tcp/tcp_core_test.go:
##########
@@ -27,7 +27,6 @@ import (
 
 	iggcon "github.com/apache/iggy/foreign/go/contracts"
 	ierror "github.com/apache/iggy/foreign/go/errors"
-	"github.com/apache/iggy/foreign/go/internal/command"
 )
 

Review Comment:
   ```suggestion
   var emptyWireReq = []byte{4, 0, 0, 0, 0, 0, 0, 0}  
   ```
   nit: use a constant to replace hardcoded literal  `[]byte{4, 0, 0, 0, 0, 0, 0, 0} ` 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@iggy.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] perf(go): pool the request-path wire payload [iggy]

Posted by "matanper (via GitHub)" <gi...@apache.org>.
matanper commented on code in PR #3442:
URL: https://github.com/apache/iggy/pull/3442#discussion_r3379745966


##########
foreign/go/client/tcp/tcp_core.go:
##########
@@ -312,34 +394,28 @@ func (c *IggyTcpClient) sendAndFetchResponse(ctx context.Context, message []byte
 	return result, nil
 }
 
-func (c *IggyTcpClient) sendLocked(message []byte, command command.Code) ([]byte, error) {
-	payload := createPayload(message, command)
-	if _, err := c.write(payload); err != nil {
+func (c *IggyTcpClient) sendLocked(wirePayload []byte) ([]byte, error) {
+	if _, err := c.write(wirePayload); err != nil {
 		c.invalidateConnLocked()
 		return nil, err
 	}
 
-	readBytes, buffer, err := c.read(ResponseInitialBytesLength)
-	if err != nil {
+	var header [ResponseInitialBytesLength]byte

Review Comment:
   I can add response header field on `IggyTcpClient`, this will resolve the `make([]byte, 8)` per RPC



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@iggy.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] perf(go): pool the request-path wire payload [iggy]

Posted by "matanper (via GitHub)" <gi...@apache.org>.
matanper commented on code in PR #3442:
URL: https://github.com/apache/iggy/pull/3442#discussion_r3379780564


##########
foreign/go/client/tcp/tcp_core.go:
##########
@@ -250,16 +284,64 @@ func (c *IggyTcpClient) write(payload []byte) (int, error) {
 	return totalWritten, nil
 }
 
-// do sends the command to the Iggy server and returns the response.
+// do sends the command and returns the response body. Commands implementing
+// the appender interface encode directly into a pooled buffer.
 func (c *IggyTcpClient) do(ctx context.Context, cmd command.Command) ([]byte, error) {
-	data, err := cmd.MarshalBinary()
+	bp := acquireRequestBuf()
+	buf, err := encodeWireRequest(*bp, cmd)
 	if err != nil {
+		releaseRequestBuf(bp)
 		return nil, err
 	}
-	return c.sendAndFetchResponse(ctx, data, cmd.Code())
+	*bp = buf
+
+	resp, err := c.sendWireAndFetchResponse(ctx, buf)
+	releaseRequestBuf(bp)
+	return resp, err
 }
 
+// encodeWireRequest writes the wire-format request (4-byte length, 4-byte
+// code, then body) into buf, growing it as needed.
+func encodeWireRequest(buf []byte, cmd command.Command) ([]byte, error) {
+	buf = buf[:0]
+	if a, ok := cmd.(appender); ok {
+		bodySize := a.MarshalledSize()
+		total := 8 + bodySize
+		if cap(buf) < total {
+			buf = make([]byte, 8, total)
+		} else {
+			buf = buf[:8]
+		}
+		// total wire message length = body + 4-byte command code
+		binary.LittleEndian.PutUint32(buf[0:4], uint32(bodySize+4))
+		binary.LittleEndian.PutUint32(buf[4:8], uint32(cmd.Code()))
+		return a.AppendBinary(buf)
+	}
+	body, err := cmd.MarshalBinary()
+	if err != nil {
+		return nil, err
+	}
+	total := 8 + len(body)
+	if cap(buf) < total {
+		buf = make([]byte, 0, total)
+	}
+	buf = buf[:8]
+	binary.LittleEndian.PutUint32(buf[0:4], uint32(len(body)+4))
+	binary.LittleEndian.PutUint32(buf[4:8], uint32(cmd.Code()))
+	buf = append(buf, body...)
+	return buf, nil
+}
+
+// sendAndFetchResponse wraps a bare command body with the wire header and
+// sends it. Kept for callers/tests that build payloads from a body + code.
 func (c *IggyTcpClient) sendAndFetchResponse(ctx context.Context, message []byte, command command.Code) ([]byte, error) {

Review Comment:
   done



##########
foreign/go/benchmarks/poll_encode_benchmark_test.go:
##########
@@ -0,0 +1,104 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package benchmarks
+
+import (
+	"testing"
+
+	iggcon "github.com/apache/iggy/foreign/go/contracts"
+	"github.com/apache/iggy/foreign/go/internal/command"
+)
+
+func newPollRequest(b *testing.B) *command.PollMessages {
+	b.Helper()
+	consumerId, _ := iggcon.NewIdentifier(uint32(42))
+	streamId, _ := iggcon.NewIdentifier("test_stream_id")
+	topicId, _ := iggcon.NewIdentifier("test_topic_id")
+	pid := uint32(1)
+	return &command.PollMessages{
+		Consumer:    iggcon.NewSingleConsumer(consumerId),
+		StreamId:    streamId,
+		TopicId:     topicId,
+		PartitionId: &pid,
+		Strategy:    iggcon.NextPollingStrategy(),
+		Count:       1000,
+		AutoCommit:  true,
+	}
+}
+
+// BenchmarkPollMessagesMarshalBinary measures the legacy hot path: a fresh
+// []byte allocated per call.
+func BenchmarkPollMessagesMarshalBinary(b *testing.B) {
+	req := newPollRequest(b)
+	b.ReportAllocs()
+	b.ResetTimer()
+	for i := 0; i < b.N; i++ {
+		out, err := req.MarshalBinary()
+		if err != nil {
+			b.Fatal(err)
+		}
+		_ = out
+	}
+}
+
+// BenchmarkPollMessagesAppendBinary measures the new hot path: encoding into
+// a caller-owned, reusable buffer.
+func BenchmarkPollMessagesAppendBinary(b *testing.B) {
+	req := newPollRequest(b)
+	buf := make([]byte, 0, req.MarshalledSize())
+	b.ReportAllocs()
+	b.ResetTimer()
+	for i := 0; i < b.N; i++ {
+		out, err := req.AppendBinary(buf[:0])
+		if err != nil {
+			b.Fatal(err)
+		}
+		buf = out
+	}
+}
+
+// BenchmarkIdentifierMarshalBinary measures the still-allocating
+// MarshalBinary path on the constant Identifier values used per poll.
+func BenchmarkIdentifierMarshalBinary(b *testing.B) {
+	id, _ := iggcon.NewIdentifier("test_stream_id")
+	b.ReportAllocs()
+	b.ResetTimer()
+	for i := 0; i < b.N; i++ {
+		out, err := id.MarshalBinary()
+		if err != nil {
+			b.Fatal(err)
+		}
+		_ = out

Review Comment:
   done



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@iggy.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] perf(go): pool the request-path wire payload [iggy]

Posted by "hubcio (via GitHub)" <gi...@apache.org>.
hubcio commented on PR #3442:
URL: https://github.com/apache/iggy/pull/3442#issuecomment-4659689999

   alright, from my side this change reasonable and it's merge-able. lets wait for @changhc for couple of days - he's our main golang guy, and nowadays he does most of go sdk work.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@iggy.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org