From aaf30ada072c752c2bf22a3be0290fe12b462353 Mon Sep 17 00:00:00 2001 From: George Date: Thu, 15 Aug 2024 15:45:09 -0700 Subject: [PATCH] Better string building is lit --- clients/stellarcore/client.go | 23 +++++++++++++------ .../stellarcore/getledgerentries_response.go | 2 +- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/clients/stellarcore/client.go b/clients/stellarcore/client.go index 02ef4eec18..15f8b37185 100644 --- a/clients/stellarcore/client.go +++ b/clients/stellarcore/client.go @@ -389,18 +389,27 @@ func (c *Client) getResponse(req *http.Request) (*http.Response, error) { return hresp, nil } +// buildMultiKeyRequest is a workaround helper because, unfortunately, +// url.Values does not support multiple keys via Set(), so we have to build our +// URL parameters manually. func buildMultiKeyRequest(keys ...xdr.LedgerKey) (string, error) { - // Unfortunately, url.Values does not support multiple keys via Set(), so we - // have to build our URL parameters manually. - q := "" + // The average ledger key length, according to a simple + // + // SELECT AVG(LENGTH(HEX(key))) / 2 FROM ledger_entries; + // + // is ~57.6. We can use this to preallocate a final string buffer for + // performance. + q := strings.Builder{} + q.Grow(50 * len(keys)) + for _, key := range keys { keyB64, err := key.MarshalBinaryBase64() if err != nil { - return q, errors.Wrapf(err, "failed to encode LedgerKey") + return q.String(), errors.Wrapf(err, "failed to encode LedgerKey") } - q += "key=" + url.QueryEscape(keyB64) + "&" + q.WriteString("key=" + url.QueryEscape(keyB64) + "&") } - q, _ = strings.CutSuffix(q, "&") - return q, nil + s, _ := strings.CutSuffix(q.String(), "&") // trim trailing & + return s, nil } diff --git a/protocols/stellarcore/getledgerentries_response.go b/protocols/stellarcore/getledgerentries_response.go index 9eb8577d52..b3f6322f8d 100644 --- a/protocols/stellarcore/getledgerentries_response.go +++ b/protocols/stellarcore/getledgerentries_response.go @@ -26,5 +26,5 @@ type GetLedgerEntriesResponse struct { type LedgerEntryResponse struct { Entry string `json:"e"` // base64-encoded xdr.LedgerEntry - State string `json:"state"` // one of: "live" | "new_entry_no_proof" | "new_entry_proof" | "archived_no_proof" | "archived_proof" + State string `json:"state"` // one of the above State constants }