[#222] common: Optimize `RemoveVote`

Use single `REMOVE` instead of multiple `APPEND` opcodes.

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
support/v0.16
Evgenii Stratonikov 2022-03-21 14:07:06 +03:00 committed by Alex Vanin
parent 53795324dc
commit aee1a5d77c
1 changed files with 7 additions and 5 deletions

View File

@ -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.