From 086b9f62d436006b0c485dc6b9c6e2a0fade7bf4 Mon Sep 17 00:00:00 2001 From: Valon Mamudi Date: Fri, 6 Jun 2025 23:55:15 +0200 Subject: [PATCH] Improve NuDB block size configuration with early return pattern and 32K support - Implement early return pattern in parseBlockSize function to reduce nesting - Fix unqualified assignment by using properly scoped const variable - Decreased maximum block size limit from 64K to 32K (32768 bytes) - Update configuration documentation to reflect correct 32K maximum - Add guidance for 32K block size usage in high-performance scenarios - Apply clang-format fixes to resolve CI formatting checks This enhances NuDB performance configurability while maintaining code quality and following modern C++ best practices. The 32K limit reflects the actual maximum supported by NuDB as confirmed by testing. --- cfg/rippled-example.cfg | 10 ++-- src/xrpld/nodestore/backend/NuDBFactory.cpp | 62 +++++++++++---------- 2 files changed, 37 insertions(+), 35 deletions(-) diff --git a/cfg/rippled-example.cfg b/cfg/rippled-example.cfg index a884e01a0a..2146b93506 100644 --- a/cfg/rippled-example.cfg +++ b/cfg/rippled-example.cfg @@ -978,7 +978,7 @@ # Optional keys for NuDB only: # # nudb_block_size Block size in bytes for NuDB storage. -# Must be a power of 2 between 4096 and 65536. +# Must be a power of 2 between 4096 and 32768. # Default is 4096. # # This parameter controls the fundamental storage unit @@ -997,10 +997,10 @@ # alignment. Can reduce metadata overhead for large # databases. # -# - 32768-65536 bytes: Suitable for specialized -# high-throughput scenarios with very fast storage. -# May increase memory usage and reduce efficiency -# for smaller databases. +# - 32768 bytes (32K): Maximum supported block size +# for high-performance scenarios with very fast +# storage. May increase memory usage and reduce +# efficiency for smaller databases. # # Note: This setting cannot be changed after database # creation without rebuilding the entire database. diff --git a/src/xrpld/nodestore/backend/NuDBFactory.cpp b/src/xrpld/nodestore/backend/NuDBFactory.cpp index b0f2a26141..54371387be 100644 --- a/src/xrpld/nodestore/backend/NuDBFactory.cpp +++ b/src/xrpld/nodestore/backend/NuDBFactory.cpp @@ -370,38 +370,40 @@ private: { std::size_t blockSize = 4096; // Default 4K std::string blockSizeStr; - - if (get_if_exists(keyValues, "nudb_block_size", blockSizeStr)) + + if (!get_if_exists(keyValues, "nudb_block_size", blockSizeStr)) { - try - { - blockSize = beast::lexicalCastThrow(blockSizeStr); - - // Validate: must be power of 2 between 4K and 64K - if (blockSize < 4096 || blockSize > 65536 || - (blockSize & (blockSize - 1)) != 0) - { - JLOG(journal.warn()) - << "Invalid nudb_block_size: " << blockSize - << ". Must be power of 2 between 4096 and 65536. Using default 4096."; - blockSize = 4096; - } - else - { - JLOG(journal.info()) - << "Using custom NuDB block size: " << blockSize << " bytes"; - } - } - catch (std::exception const& e) - { - JLOG(journal.warn()) - << "Invalid nudb_block_size value: " << blockSizeStr - << ". Using default 4096. Error: " << e.what(); - blockSize = 4096; - } + return blockSize; // Early return with default + } + + try + { + std::size_t const parsedBlockSize = + beast::lexicalCastThrow(blockSizeStr); + + // Validate: must be power of 2 between 4K and 32K + if (parsedBlockSize < 4096 || parsedBlockSize > 32768 || + (parsedBlockSize & (parsedBlockSize - 1)) != 0) + { + JLOG(journal.warn()) + << "Invalid nudb_block_size: " << parsedBlockSize + << ". Must be power of 2 between 4096 and 32768. Using " + "default 4096."; + return 4096; + } + + JLOG(journal.info()) + << "Using custom NuDB block size: " << parsedBlockSize + << " bytes"; + return parsedBlockSize; + } + catch (std::exception const& e) + { + JLOG(journal.warn()) + << "Invalid nudb_block_size value: " << blockSizeStr + << ". Using default 4096. Error: " << e.what(); + return 4096; } - - return blockSize; } };