Skip to content

Commit

Permalink
Problem: redundant copy in check validate
Browse files Browse the repository at this point in the history
  • Loading branch information
mmsqe committed Jul 2, 2024
1 parent e877151 commit 2e396d7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
* [#1488](https://github.com/crypto-org-chain/cronos/pull/1488) Enable optimistic execution.
* [#1490](https://github.com/crypto-org-chain/cronos/pull/1490) Update cometbft to v0.38.8.
* [#1491](https://github.com/crypto-org-chain/cronos/pull/1491) Free slice data in HasAtVersion.
* [#1498](https://github.com/crypto-org-chain/cronos/pull/1498) Check validity with extractSliceBytes to avoid copying slice data.

### Bug Fixes

Expand Down
14 changes: 12 additions & 2 deletions versiondb/tsrocksdb/iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ 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
eoakey := extractSliceBytes(source.Key()) // end or after key
if bytes.Compare(end, eoakey) <= 0 {
source.Prev()
}
Expand Down Expand Up @@ -75,7 +75,7 @@ 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 := extractSliceBytes(itr.source.Key())
if itr.isReverse {
if start != nil && bytes.Compare(key, start) < 0 {
itr.isInvalid = true
Expand Down Expand Up @@ -143,3 +143,13 @@ func moveSliceToBytes(s *grocksdb.Slice) []byte {
copy(v, s.Data())
return v
}

// extractSliceBytes returns the underlying []byte from the given *Slice
// without copying the data. Use this function with caution as the *Slice
// should not be used after calling this function, as it is not marked as freed.
func extractSliceBytes(s *grocksdb.Slice) []byte {
if !s.Exists() {
return nil

Check warning on line 152 in versiondb/tsrocksdb/iterator.go

View check run for this annotation

Codecov / codecov/patch

versiondb/tsrocksdb/iterator.go#L152

Added line #L152 was not covered by tests
}
return s.Data()
}

0 comments on commit 2e396d7

Please sign in to comment.