Skip to content

Commit

Permalink
Don't use string manip in speed-critical parts of mining algo
Browse files Browse the repository at this point in the history
- Also some cmake adjustments
  • Loading branch information
who-biz committed Nov 26, 2019
1 parent c1f89b5 commit 1bfc819
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 20 deletions.
20 changes: 18 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,11 @@ endif()
include_directories(${LIBUNWIND_INCLUDE})
link_directories(${LIBUNWIND_LIBRARY_DIRS})

if(CMAKE_BUILD_TYPE STREQUAL "Debug")
include_directories(${BACKWARD_INCLUDE})
include_directories(${BACKWARD_LIBRARY_DIR})
endif()

if(MSVC)
add_definitions("/bigobj /MP /W3 /GS- /D_CRT_SECURE_NO_WARNINGS /wd4996 /wd4345 /D_WIN32_WINNT=0x0600 /DWIN32_LEAN_AND_MEAN /DGTEST_HAS_TR1_TUPLE=0 /FIinline_c.h /D__SSE4_1__")
# set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /Dinline=__inline")
Expand Down Expand Up @@ -783,11 +788,12 @@ if(STATIC)
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_STATIC_RUNTIME ON)
endif()
find_package(Boost 1.58 QUIET REQUIRED COMPONENTS system filesystem thread date_time chrono regex serialization program_options locale)
find_package(Boost REQUIRED COMPONENTS system filesystem thread date_time chrono regex serialization program_options locale)
set(BOOST_INCLUDE_DIR /usr/lib/boost)

set(CMAKE_FIND_LIBRARY_SUFFIXES ${OLD_LIB_SUFFIXES})
if(NOT Boost_FOUND)
die("Could not find Boost libraries, please make sure you have installed Boost or libboost-all-dev (1.58) or the equivalent")
message(FATAL_ERROR "Could not find Boost libraries, please make sure you have installed Boost or libboost-all-dev (1.58) or the equivalent")
elseif(Boost_FOUND)
message(STATUS "Found Boost Version: ${Boost_VERSION}")
if (Boost_VERSION VERSION_LESS 10 AND Boost_VERSION VERSION_LESS 1.62.0 AND NOT (OPENSSL_VERSION VERSION_LESS 1.1))
Expand Down Expand Up @@ -853,6 +859,16 @@ if(CMAKE_C_COMPILER_ID STREQUAL "Clang" AND ARCH_WIDTH EQUAL "32" AND NOT IOS AN
endif()

if(CMAKE_BUILD_TYPE STREQUAL "Debug")
# find_library(DWARF_LIBRARY dwarf)
# if(DWARF_LIBRARY)
# set(DWARF_LIBRARY "${DWARF_LIBRARY}")
# message(STATUS "Found DWARF_LIBRARY located at ${DWARF_LIBRARY}")
# include_directories(DWARF_INCLUDE_DIR /usr/include/libdwarf)
# endif()
# if(NOT DWARF_LIBRARY)
# message(FATAL_ERROR "Could not find required libdwarf.")
# endif()

find_library(BFD_LIBRARY bfd)
if(BFD_LIBRARY)
set(BFD_LIBRARY "${BFD_LIBRARY}")
Expand Down
14 changes: 14 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,26 @@ function (monero_add_executable name)
FILES
${ARGN})

if (CMAKE_BUILD_TYPE STREQUAL Debug)
add_executable("${name}"
${ARGN}
${BACKWARD_ENABLE})
add_backward("${name}")
add_dependencies("${name}" check_git_repository)
target_link_libraries("${name}"
PRIVATE
${EXTRA_LIBRARIES}
PUBLIC
Backward::Backward)
else()
add_executable("${name}"
${ARGN})
add_dependencies("${name}" check_git_repository)
target_link_libraries("${name}"
PRIVATE
${EXTRA_LIBRARIES})
endif()

set_property(TARGET "${name}"
PROPERTY
FOLDER "prog")
Expand Down
29 changes: 18 additions & 11 deletions src/cryptonote_basic/cryptonote_format_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -944,25 +944,32 @@ namespace cryptonote
}
else if (b.major_version >= 10)
{
// get 6 char from previous hash
std::string subhash = string_tools::pod_to_hex(b.prev_id).substr(0,6);
uint32_t id_num = std::strtoul(subhash.c_str(), NULL, 16); // base10 int
id_num = id_num < 1 ? 1 : id_num; // guard against zero case
// get 6 char from previous hash as varint
epee::span<const uint8_t> span_bytes = epee::as_byte_span(b.prev_id);

This comment has been minimized.

Copy link
@who-biz

who-biz Nov 27, 2019

Author Contributor

there is almost certainly a much faster way to handle this than using epee's span type


uint8_t i = 0;
subhash sub; sub.uint = 0;
uint32_t id_num = 0;
for (const auto& byte : span_bytes) {
memcpy(&sub.bytes[i++], &byte, sizeof(byte));
id_num |= byte << (24 - (8*i));
if (i == 3)
break;
}

// guard against zero case
id_num = (id_num < 1) ? 1 : id_num;

// rest is just ((m_stamp % id_num) + height) % 32768
// ordered by branch most commonly taken
uint64_t m_stamp = b.timestamp;
bool two = id_num && !(id_num & (id_num - 1)); // id_num is pow of 2
if (!two) {
uint32_t m = add(id_num, 1);
uint32_t const m = add(id_num, 1);
bool mersenne = m && !(m & id_num); // id_num is (pow2 - 1)
if (!mersenne) {
/* if (!mod3(m_stamp) && !mod3(id_num)) { // multiples of 3
MWARNING(" so none of special cases");
its = add(cn_iters, (add(div3(div3(m_stamp) % div3(id_num)), height) & 0x7FFF));
} else {*/
its = add(cn_iters, (add(m_stamp % id_num, height) & 0x7FFF));
// }
// no special cases
its = add(cn_iters, (add(m_stamp % id_num, height) & 0x7FFF));
} else {
// pow2 - 1
its = add(cn_iters,(add(mod_mersenne(m_stamp,m), height) & 0x7FFF));
Expand Down
2 changes: 2 additions & 0 deletions src/cryptonote_basic/cryptonote_format_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ namespace epee

namespace cryptonote
{
union _subhash { uint8_t bytes[3]; uint32_t uint; };

This comment has been minimized.

Copy link
@who-biz

who-biz Nov 27, 2019

Author Contributor

this was also rendered pretty much pointless, I just went with using ROL. This might be better, but need to make sure we access union starting with the most recent byte written, when converting type

This comment has been minimized.

Copy link
@who-biz

who-biz Nov 27, 2019

Author Contributor

some behaviors need accounted for when writing and then reading ordered bits to/from unions, apparently

typedef union _subhash subhash;
//---------------------------------------------------------------
void get_transaction_prefix_hash(const transaction_prefix& tx, crypto::hash& h);
crypto::hash get_transaction_prefix_hash(const transaction_prefix& tx);
Expand Down
8 changes: 2 additions & 6 deletions src/daemon/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,9 @@ monero_add_executable(daemon
${daemon_sources}
${daemon_headers}
${daemon_private_headers}
${blocksdat}
${BACKWARD_ENABLE})
${blocksdat})
add_dependencies(daemon check_git_repository rpc daemonizer daemon_rpc_server)

add_backward(daemon)

target_link_libraries(daemon
PRIVATE
rpc
Expand All @@ -110,8 +107,7 @@ target_link_libraries(daemon
${GNU_READLINE_LIBRARY}
${SODIUM_LIBRARY}
${EXTRA_LIBRARIES}
PUBLIC
Backward::Backward)
PUBLIC)
else()
monero_add_executable(daemon
${daemon_sources}
Expand Down
1 change: 1 addition & 0 deletions src/serialization/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ monero_add_library(serialization
${serialization_sources}
${serialization_headers}
${serialization_private_headers})
add_dependencies(serialization cryptonote_core cryptonote_protocol)
target_link_libraries(serialization
LINK_PRIVATE
cryptonote_core
Expand Down
2 changes: 1 addition & 1 deletion src/wallet/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ monero_add_library(wallet
add_dependencies(wallet check_git_repository)
target_link_libraries(wallet
PUBLIC
multisig
common
cryptonote_core
mnemonics
multisig
${LMDB_LIBRARY}
${Boost_CHRONO_LIBRARY}
${Boost_SERIALIZATION_LIBRARY}
Expand Down

0 comments on commit 1bfc819

Please sign in to comment.