From 8fdd4af8b39464fda5f0e9b0116497c3aaa44582 Mon Sep 17 00:00:00 2001 From: raisul Date: Mon, 13 Jan 2025 11:37:16 +0600 Subject: [PATCH 1/4] fix: unify receiver methods to avoid conflicts between value and pointer types --- allocator.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/allocator.go b/allocator.go index f2925e7..682a0fc 100644 --- a/allocator.go +++ b/allocator.go @@ -42,7 +42,7 @@ func newAllocator(low, high int) *allocator { // "allocator[low..high] reserved..until" // // O(N) where N is high-low -func (a allocator) String() string { +func (a *allocator) String() string { b := &bytes.Buffer{} fmt.Fprintf(b, "allocator[%d..%d]", a.low, a.high) From 8ab81c83359e1df0cc0e7fa7cc1c7f753e9b08b1 Mon Sep 17 00:00:00 2001 From: raisul Date: Mon, 13 Jan 2025 11:55:52 +0600 Subject: [PATCH 2/4] perf: replace bytes.Buffer with strings.Builder in allocator.go for improved performance --- allocator.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/allocator.go b/allocator.go index 682a0fc..e4aa900 100644 --- a/allocator.go +++ b/allocator.go @@ -6,9 +6,9 @@ package amqp091 import ( - "bytes" "fmt" "math/big" + "strings" ) const ( @@ -43,8 +43,8 @@ func newAllocator(low, high int) *allocator { // // O(N) where N is high-low func (a *allocator) String() string { - b := &bytes.Buffer{} - fmt.Fprintf(b, "allocator[%d..%d]", a.low, a.high) + var b strings.Builder + fmt.Fprintf(&b, "allocator[%d..%d]", a.low, a.high) for low := a.low; low <= a.high; low++ { high := low @@ -53,9 +53,9 @@ func (a *allocator) String() string { } if high > low+1 { - fmt.Fprintf(b, " %d..%d", low, high-1) + fmt.Fprintf(&b, " %d..%d", low, high-1) } else if high > low { - fmt.Fprintf(b, " %d", high-1) + fmt.Fprintf(&b, " %d", high-1) } low = high From 254b68f3a6603783d78169093357a8f8ee722b2e Mon Sep 17 00:00:00 2001 From: raisul Date: Mon, 13 Jan 2025 12:13:16 +0600 Subject: [PATCH 3/4] refactor: improve error handling with descriptive messages and error wrapping --- write.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/write.go b/write.go index a65514d..5a51e64 100644 --- a/write.go +++ b/write.go @@ -10,6 +10,7 @@ import ( "bytes" "encoding/binary" "errors" + "fmt" "io" "math" "time" @@ -414,12 +415,16 @@ func writeTable(w io.Writer, table Table) (err error) { for key, val := range table { if err = writeShortstr(&buf, key); err != nil { - return + return fmt.Errorf("writing key %q: %w", key, err) } if err = writeField(&buf, val); err != nil { - return + return fmt.Errorf("writing value for key %q: %w", key, err) } } - return writeLongstr(w, buf.String()) + if err := writeLongstr(w, buf.String()); err != nil { + return fmt.Errorf("writing final long string: %w", err) + } + + return nil } From cf0d910f94cf363d17f8aabe3644a44a9e166999 Mon Sep 17 00:00:00 2001 From: raisul Date: Mon, 13 Jan 2025 12:14:20 +0600 Subject: [PATCH 4/4] refactor: added function comment explaining its purpose --- write.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/write.go b/write.go index 5a51e64..dbe5906 100644 --- a/write.go +++ b/write.go @@ -410,6 +410,8 @@ func writeField(w io.Writer, value interface{}) (err error) { return } +// writeTable serializes a Table to the given writer. +// It writes each key-value pair and returns the serialized data as a longstr. func writeTable(w io.Writer, table Table) (err error) { var buf bytes.Buffer