From 874ab3411d87e9f4c00a633a36772380f96daa42 Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Mon, 22 Jul 2024 16:25:06 -0300 Subject: [PATCH] more of that. --- negentropy/negentropy.go | 23 ++++++++++++----------- negentropy/types.go | 1 - negentropy/vector.go | 3 +-- 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/negentropy/negentropy.go b/negentropy/negentropy.go index 86979c5..81d3302 100644 --- a/negentropy/negentropy.go +++ b/negentropy/negentropy.go @@ -79,8 +79,8 @@ func (n *Negentropy) Reconcile(query []byte) (output []byte, haveIds []string, n func (n *Negentropy) reconcileAux(reader *bytes.Reader, haveIds, needIds *[]string) ([]byte, error) { n.lastTimestampIn, n.lastTimestampOut = 0, 0 // Reset for each message - var fullOutput []byte - fullOutput = append(fullOutput, protocolVersion) + fullOutput := bytes.NewBuffer(make([]byte, 0, 5000)) + fullOutput.WriteByte(protocolVersion) pv, err := reader.ReadByte() if err != nil { @@ -94,7 +94,7 @@ func (n *Negentropy) reconcileAux(reader *bytes.Reader, haveIds, needIds *[]stri if n.IsInitiator { return nil, fmt.Errorf("unsupported negentropy protocol version requested") } - return fullOutput, nil + return fullOutput.Bytes(), nil } var prevBound Bound @@ -199,7 +199,7 @@ func (n *Negentropy) reconcileAux(reader *bytes.Reader, haveIds, needIds *[]stri endBound := currBound n.storage.Iterate(lower, upper, func(item Item, index int) bool { - if n.ExceededFrameSizeLimit(len(fullOutput) + len(*responseIdsPtr)) { + if n.ExceededFrameSizeLimit(fullOutput.Len() + len(*responseIdsPtr)) { endBound = Bound{item} upper = index return false @@ -222,7 +222,7 @@ func (n *Negentropy) reconcileAux(reader *bytes.Reader, haveIds, needIds *[]stri partialOutput.Write(encodeVarInt(numResponseIds)) partialOutput.Write(responseIds) - fullOutput = append(fullOutput, partialOutput.Bytes()...) + partialOutput.WriteTo(fullOutput) partialOutput.Reset() } @@ -231,7 +231,7 @@ func (n *Negentropy) reconcileAux(reader *bytes.Reader, haveIds, needIds *[]stri } // Check if the frame size limit is exceeded - if n.ExceededFrameSizeLimit(len(fullOutput) + partialOutput.Len()) { + if n.ExceededFrameSizeLimit(fullOutput.Len() + partialOutput.Len()) { // Frame size limit exceeded, handle by encoding a boundary and fingerprint for the remaining range remainingFingerprint, err := n.storage.Fingerprint(upper, n.storage.Size()) if err != nil { @@ -242,21 +242,22 @@ func (n *Negentropy) reconcileAux(reader *bytes.Reader, haveIds, needIds *[]stri if err != nil { panic(err) } - fullOutput = append(fullOutput, encodedBound...) - fullOutput = append(fullOutput, encodeVarInt(FingerprintMode)...) - fullOutput = append(fullOutput, remainingFingerprint.SV()...) + + fullOutput.Write(encodedBound) + fullOutput.WriteByte(FingerprintMode) + fullOutput.Write(remainingFingerprint.SV()) break // Stop processing further } else { // Append the constructed output for this iteration - fullOutput = append(fullOutput, partialOutput.Bytes()...) + partialOutput.WriteTo(fullOutput) } prevIndex = upper prevBound = currBound } - return fullOutput, nil + return fullOutput.Bytes(), nil } func (n *Negentropy) SplitRange(lower, upper int, upperBound Bound, output *bytes.Buffer) { diff --git a/negentropy/types.go b/negentropy/types.go index 78af76d..567d3c1 100644 --- a/negentropy/types.go +++ b/negentropy/types.go @@ -22,7 +22,6 @@ type Storage interface { Insert(nostr.Timestamp, string) error Seal() error - IDSize() int Size() int Iterate(begin, end int, cb func(item Item, i int) bool) error FindLowerBound(begin, end int, value Bound) (int, error) diff --git a/negentropy/vector.go b/negentropy/vector.go index 170f340..0e73471 100644 --- a/negentropy/vector.go +++ b/negentropy/vector.go @@ -44,8 +44,7 @@ func (v *Vector) Seal() error { return nil } -func (v *Vector) Size() int { return len(v.items) } -func (v *Vector) IDSize() int { return v.idSize } +func (v *Vector) Size() int { return len(v.items) } func (v *Vector) Iterate(begin, end int, cb func(Item, int) bool) error { for i := begin; i < end; i++ {