From 53e446961b7435768951eb238ee87644a41484c4 Mon Sep 17 00:00:00 2001 From: mmsqe Date: Wed, 3 Jul 2024 09:30:40 +0800 Subject: [PATCH] Problem: redundant copy in check validate (#1502) free key update doc cleanup --- CHANGELOG.md | 1 + versiondb/tsrocksdb/iterator.go | 12 +++++++----- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a96a851caf..382008c54f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ * (versiondb) [#1379](https://github.com/crypto-org-chain/cronos/pull/1379) Flush versiondb when graceful shutdown, make rocksdb upgrade smooth. * (versiondb) [#1491](https://github.com/crypto-org-chain/cronos/pull/1491) Free slice data in HasAtVersion. +* (versiondb) [#1498](https://github.com/crypto-org-chain/cronos/pull/1498) Reduce scope of copying slice data in iterator. ### Bug Fixes diff --git a/versiondb/tsrocksdb/iterator.go b/versiondb/tsrocksdb/iterator.go index c6089d330b..f4c4b3c777 100644 --- a/versiondb/tsrocksdb/iterator.go +++ b/versiondb/tsrocksdb/iterator.go @@ -23,8 +23,9 @@ func newRocksDBIterator(source *grocksdb.Iterator, prefix, start, end []byte, is } else { source.Seek(end) if source.Valid() { - eoakey := moveSliceToBytes(source.Key()) // end or after key - if bytes.Compare(end, eoakey) <= 0 { + eoakey := source.Key() // end or after key + defer eoakey.Free() + if bytes.Compare(end, eoakey.Data()) <= 0 { source.Prev() } } else { @@ -75,14 +76,15 @@ func (itr *rocksDBIterator) Valid() bool { // If key is end or past it, invalid. start := itr.start end := itr.end - key := moveSliceToBytes(itr.source.Key()) + key := itr.source.Key() + defer key.Free() if itr.isReverse { - if start != nil && bytes.Compare(key, start) < 0 { + if start != nil && bytes.Compare(key.Data(), start) < 0 { itr.isInvalid = true return false } } else { - if end != nil && bytes.Compare(end, key) <= 0 { + if end != nil && bytes.Compare(end, key.Data()) <= 0 { itr.isInvalid = true return false }