Skip to content

Speed up Slice::DecodeHex #15072

Description

@thatsafunnyname

This issue created for #15070

Speed up Slice::DecodeHex by:

  • Eliminated branching for lookup.
  • Single branch check using bitwise OR for invalid characters. resize() over reserve() + push_back(). Avoid push_back continually checking and incrementing the internal size counter.

There is a change in behaviour on failure:

  • In the original code if the function encounters an invalid hex character halfway through the string, it exits early and returns false. However, any bytes successfully decoded before the error remain inside result.
  • With this change if an error occurs halfway through, the result->clear() call inside the error branch completely empties the string, leaving it clean but losing the partial progress.

An example improvement in performance:

  • ~ 9x reduction in branches
  • ~ 120x reduction in branch misses
  • ~ 5.6x reduction in user CPU
  • ~ 3.66x reduction in wall time

Before:

rm -rf /tmp/test_hex ; cat /tmp/test_hex_dbdump.tmp | 
  perf stat -e branches,branch-misses tools/ldb --db=/tmp/test_hex load --hex --create_if_missing --disable_wal
 Performance counter stats for 'tools/ldb --db=/tmp/test_hex load --hex --create_if_missing --disable_wal':
    10,318,296,112      branches:u
       202,206,579      branch-misses:u           #    1.96% of all branches
       4.629428323 seconds time elapsed
       4.121290000 seconds user
       0.823500000 seconds sys

After:

rm -rf /tmp/test_hex ; cat /tmp/test_hex_dbdump.tmp | 
  perf stat -e branches,branch-misses tools/ldb --db=/tmp/test_hex load --hex --create_if_missing --disable_wal
 Performance counter stats for 'tools/ldb --db=/tmp/test_hex load --hex --create_if_missing --disable_wal':
     1,147,641,340      branches:u
         1,678,923      branch-misses:u           #    0.15% of all branches
       1.263189689 seconds time elapsed
       0.736994000 seconds user
       0.794229000 seconds sys

Testing with RocksDB v10.6.2 using gcc 8.5.0 DEBUG_LEVEL=0.

With the changes from #15045 for #15057 that added round trip hex tests to util/slice_test.cc .

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions