+
Skip to content

Optimize FlatChainStore and improve tests #493

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 21, 2025

Conversation

JoseSK999
Copy link
Contributor

What is the purpose of this pull request?

  • Bug fix
  • Documentation update
  • New feature
  • Test
  • Other:

Which crates are being modified?

  • floresta-chain
  • floresta-cli
  • floresta-common
  • floresta-compact-filters
  • floresta-electrum
  • floresta-watch-only
  • floresta-wire
  • floresta
  • florestad
  • Other:

Description

Previously we were doing:

    unsafe fn get_header_by_hash(
        &self,
        hash: BlockHash,
    ) -> Result<Option<DiskBlockHeader>, FlatChainstoreError> {
        let header = self
            .block_index
            .get_index_for_hash(hash, |height| self.get_block_header_by_index(height))?
            .map(|height| self.get_block_header_by_index(height)); // DUPLICATED CALL

        Ok(header.transpose()?.map(|x| x.header))
    }

Which we can optimize/simplify by making the get_index_for_hash function directly return the header as well, which is free since it's already fetched.

To make this easier I made the IndexBucket enum contain the header if occupied (I think this is good as it's data related to the entry we searched for), and named the fields for clarity.

pub enum IndexBucket {
    Empty { ptr: *mut Index },
    Occupied {
        ptr: *mut Index,
        header: DiskBlockHeader,
    },
}

In the ChainStore::get_block_hash impl we handle the NotFound case. Then I have improved the headers tests by verifying our assumptions are holding.

Previously we were doing:

```rust
    unsafe fn get_header_by_hash(
        &self,
        hash: BlockHash,
    ) -> Result<Option<DiskBlockHeader>, FlatChainstoreError> {
        let header = self
            .block_index
            .get_index_for_hash(hash, |height| self.get_block_header_by_index(height))?
            .map(|height| self.get_block_header_by_index(height));

        Ok(header.transpose()?.map(|x| x.header))
    }
```

Which we can optimize/simplify by making the `get_index_for_hash` function directly return the header as well, which is free since it's already fetched.

To make this easier I make the `IndexBucket` enum contain the header if occupied, and named the fields for clarity.

In the `ChainStore::get_block_hash` impl we handle the `NotFound` case. Then I have improved the headers tests by verifying our assumptions are holding.
@Davidson-Souza Davidson-Souza added code quality Generally improves code readability and maintainability performance This is a performance-related issue labels May 20, 2025
Copy link
Member

@Davidson-Souza Davidson-Souza left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 2eb173f

Pretty nice what you did in the tests. Testing edge cases is something that's somewhat lacking inside our unit tests

@JoseSK999
Copy link
Contributor Author

ACK 2eb173f

Pretty nice what you did in the tests. Testing edge cases is something that's somewhat lacking inside our unit tests

Thanks! I wanted to actually verify that we get zero hashes if it is uninitialized data

@Davidson-Souza Davidson-Souza merged commit 5f74a9d into vinteumorg:master May 21, 2025
10 checks passed
@JoseSK999 JoseSK999 deleted the optimize-get-header branch May 21, 2025 21:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
code quality Generally improves code readability and maintainability performance This is a performance-related issue
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载