From aee1a5d77c0afb449646eaebb59fd36f3f7718b3 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Mon, 21 Mar 2022 14:07:06 +0300 Subject: [PATCH] [#222] common: Optimize `RemoveVote` Use single `REMOVE` instead of multiple `APPEND` opcodes. Signed-off-by: Evgenii Stratonikov --- common/vote.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/common/vote.go b/common/vote.go index a136c69..0e371e5 100644 --- a/common/vote.go +++ b/common/vote.go @@ -80,18 +80,20 @@ func Vote(ctx storage.Context, id, from []byte) int { // inner ring nodes. func RemoveVotes(ctx storage.Context, id []byte) { var ( - newCandidates []Ballot - candidates = getBallots(ctx) + candidates = getBallots(ctx) + index int ) for i := 0; i < len(candidates); i++ { cnd := candidates[i] - if !BytesEqual(cnd.ID, id) { - newCandidates = append(newCandidates, cnd) + if BytesEqual(cnd.ID, id) { + index = i + break } } - SetSerialized(ctx, voteKey, newCandidates) + util.Remove(candidates, index) + SetSerialized(ctx, voteKey, candidates) } // getBallots returns deserialized slice of vote ballots.