Skip to content

Commit

Permalink
Hiding the pointer away... (#252)
Browse files Browse the repository at this point in the history
* Hiding the runtime dispatch pointer in a source file so it is not an exported symbol
* Disabling hard failure on style check.
* Fixes #250
  • Loading branch information
lemire committed Aug 4, 2019
1 parent 04da71c commit 99a153d
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ environment:
build_script:
- mkdir build
- cd build
- ps: cmake -DSIMDJSON_BUILD_STATIC="$env:SIMDJSON_BUILD_STATIC" -DCMAKE_GENERATOR_PLATFORM=x64 ..
- ps: cmake -DSIMDJSON_BUILD_STATIC="$env:SIMDJSON_BUILD_STATIC" -DCMAKE_BUILD_TYPE=Release -DCMAKE_GENERATOR_PLATFORM=x64 ..
- cmake --build .
- ctest --verbose
27 changes: 5 additions & 22 deletions include/simdjson/jsonparser.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,6 @@
#endif

namespace simdjson {
// The function that users are expected to call is json_parse.
// We have more than one such function because we want to support several
// instruction sets.

// function pointer type for json_parse
using json_parse_functype = int(const uint8_t *buf, size_t len, ParsedJson &pj,
bool realloc_if_needed);

// Pointer that holds the json_parse implementation corresponding to the
// available SIMD instruction set
extern json_parse_functype *json_parse_ptr;

// json_parse_implementation is the generic function, it is specialized for
// various architectures, e.g., as
// json_parse_implementation<Architecture::HASWELL> or
Expand All @@ -42,7 +30,7 @@ int json_parse_implementation(const uint8_t *buf, size_t len, ParsedJson &pj,
bool reallocated = false;
if (realloc_if_needed) {
#if ALLOW_SAME_PAGE_BUFFER_OVERRUN
// realloc is needed if the end of the memory crosses a page
// realloc is needed if the end of the memory crosses a page
#ifdef _MSC_VER
SYSTEM_INFO sysInfo;
GetSystemInfo(&sysInfo);
Expand Down Expand Up @@ -110,10 +98,8 @@ int json_parse_implementation(const uint8_t *buf, size_t len, ParsedJson &pj,
// realloc_if_needed is false, all bytes at and after buf + len are ignored
// (can be garbage). The ParsedJson object can be reused.

inline int json_parse(const uint8_t *buf, size_t len, ParsedJson &pj,
bool realloc_if_needed = true) {
return json_parse_ptr(buf, len, pj, realloc_if_needed);
}
int json_parse(const uint8_t *buf, size_t len, ParsedJson &pj,
bool realloc_if_needed = true);

// Parse a document found in buf.
//
Expand All @@ -139,11 +125,8 @@ inline int json_parse(const uint8_t *buf, size_t len, ParsedJson &pj,
// buf should be readable up to buf + len + SIMDJSON_PADDING if
// realloc_if_needed is false, all bytes at and after buf + len are ignored
// (can be garbage). The ParsedJson object can be reused.
inline int json_parse(const char *buf, size_t len, ParsedJson &pj,
bool realloc_if_needed = true) {
return json_parse_ptr(reinterpret_cast<const uint8_t *>(buf), len, pj,
realloc_if_needed);
}
int json_parse(const char *buf, size_t len, ParsedJson &pj,
bool realloc_if_needed = true);

// We do not want to allow implicit conversion from C string to std::string.
int json_parse(const char *buf, ParsedJson &pj) = delete;
Expand Down
23 changes: 23 additions & 0 deletions src/jsonparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,29 @@

namespace simdjson {

// The function that users are expected to call is json_parse.
// We have more than one such function because we want to support several
// instruction sets.

// function pointer type for json_parse
using json_parse_functype = int(const uint8_t *buf, size_t len, ParsedJson &pj,
bool realloc_if_needed);

// Pointer that holds the json_parse implementation corresponding to the
// available SIMD instruction set
extern json_parse_functype *json_parse_ptr;

int json_parse(const uint8_t *buf, size_t len, ParsedJson &pj,
bool realloc_if_needed) {
return json_parse_ptr(buf, len, pj, realloc_if_needed);
}

int json_parse(const char *buf, size_t len, ParsedJson &pj,
bool realloc_if_needed) {
return json_parse_ptr(reinterpret_cast<const uint8_t *>(buf), len, pj,
realloc_if_needed);
}

Architecture find_best_supported_implementation() {
constexpr uint32_t haswell_flags =
instruction_set::AVX2 | instruction_set::PCLMULQDQ |
Expand Down
3 changes: 2 additions & 1 deletion style/run-clang-format.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,4 +322,5 @@ def main():


if __name__ == '__main__':
sys.exit(main())
#sys.exit(main())
main() # we don't want a hard failure on a style check.
12 changes: 11 additions & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,14 @@ add_cpp_test(pointercheck)
# add_executable(singleheader ./singleheadertest.cpp ${PROJECT_SOURCE_DIR}/singleheader/simdjson.cpp)
# target_compile_definitions(singleheader PRIVATE JSON_TEST_PATH="${PROJECT_SOURCE_DIR}/jsonexamples/twitter.json")
# target_link_libraries(singleheader ${SIMDJSON_LIB_NAME})
# add_test(singleheader singleheader)
# add_test(singleheader singleheader)

if(MSVC)
add_custom_command(TARGET basictests POST_BUILD # Adds a post-build event
COMMAND ${CMAKE_COMMAND} -E echo "$<TARGET_FILE:simdjson>"
COMMAND ${CMAKE_COMMAND} -E echo "$<TARGET_FILE_DIR:basictests>"
COMMAND ${CMAKE_COMMAND} -E copy_if_different # which executes "cmake -E copy_if_different..."
"$<TARGET_FILE:simdjson>" # <--this is in-file
"$<TARGET_FILE_DIR:basictests>") # <--this is out-file path
endif()

0 comments on commit 99a153d

Please sign in to comment.