kou commented on code in PR #50469: URL: https://github.com/apache/arrow/pull/50469#discussion_r3567660836
########## cpp/cmake_modules/FindsimdjsonAlt.cmake: ########## @@ -0,0 +1,99 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +if(SimdjsonAlt_FOUND) + return() +endif() + +# Try to find simdjson manually first, as some system packages have broken +# CMake configs that reference non-existent library files (e.g., Alpine Linux) Review Comment: Could you share the error message on Alpine Linux? Should we install `simdjson-static` too? ########## cpp/cmake_modules/ThirdpartyToolchain.cmake: ########## @@ -137,6 +138,10 @@ if(ARROW_DEPENDENCY_SOURCE STREQUAL "CONDA") if("${GTest_SOURCE}" STREQUAL "") set(GTest_SOURCE "AUTO") endif() + # simdjson is not commonly available in conda, so we allow auto fallback. + if("${simdjson_SOURCE}" STREQUAL "") + set(simdjson_SOURCE "AUTO") + endif() Review Comment: It seems that simdjson is available: https://anaconda.org/channels/conda-forge/packages/simdjson/overview ########## cpp/src/arrow/json/object_parser.cc: ########## @@ -16,72 +16,115 @@ // under the License. #include "arrow/json/object_parser.h" -#include "arrow/json/rapidjson_defs.h" // IWYU pragma: keep -#include <rapidjson/document.h> +#include <simdjson.h> namespace arrow { namespace json { namespace internal { -namespace rj = arrow::rapidjson; - class ObjectParser::Impl { public: Status Parse(std::string_view json) { - document_.Parse(reinterpret_cast<const rj::Document::Ch*>(json.data()), - static_cast<size_t>(json.size())); + // Copy into padded buffer + padded_json_ = simdjson::padded_string(json); + + // Parse + auto result = parser_.parse(padded_json_); - if (document_.HasParseError()) { - return Status::Invalid("Json parse error (offset ", document_.GetErrorOffset(), - "): ", document_.GetParseError()); + // Handle parse errors + if (result.error()) { + return Status::Invalid("Json parse error: ", Review Comment: ```suggestion return Status::Invalid("JSON parse error: ", ``` ########## cpp/src/arrow/json/object_parser.cc: ########## @@ -16,72 +16,115 @@ // under the License. #include "arrow/json/object_parser.h" -#include "arrow/json/rapidjson_defs.h" // IWYU pragma: keep -#include <rapidjson/document.h> +#include <simdjson.h> namespace arrow { namespace json { namespace internal { -namespace rj = arrow::rapidjson; - class ObjectParser::Impl { public: Status Parse(std::string_view json) { - document_.Parse(reinterpret_cast<const rj::Document::Ch*>(json.data()), - static_cast<size_t>(json.size())); + // Copy into padded buffer + padded_json_ = simdjson::padded_string(json); + + // Parse + auto result = parser_.parse(padded_json_); - if (document_.HasParseError()) { - return Status::Invalid("Json parse error (offset ", document_.GetErrorOffset(), - "): ", document_.GetParseError()); + // Handle parse errors + if (result.error()) { + return Status::Invalid("Json parse error: ", + simdjson::error_message(result.error())); } - if (!document_.IsObject()) { + + // Store parsed document + document_ = std::move(result).value(); + + // Validate root is an object + if (!document_.is_object()) { return Status::TypeError("Not a json object"); Review Comment: ```suggestion return Status::TypeError("Not a JSON object"); ``` ########## cpp/cmake_modules/ThirdpartyToolchain.cmake: ########## @@ -2782,6 +2801,61 @@ if(ARROW_BUILD_BENCHMARKS) FALSE) endif() +macro(build_simdjson) + message(STATUS "Building simdjson from source") + set(SIMDJSON_PREFIX "${CMAKE_CURRENT_BINARY_DIR}/simdjson_ep/src/simdjson_ep-install") + set(SIMDJSON_INCLUDE_DIR "${SIMDJSON_PREFIX}/include") + set(SIMDJSON_LIB_DIR "${SIMDJSON_PREFIX}/lib") + + set(SIMDJSON_CMAKE_ARGS + ${EP_COMMON_CMAKE_ARGS} + -DSIMDJSON_BUILD_STATIC_LIB=ON + -DSIMDJSON_DEVELOPER_MODE=OFF + -DSIMDJSON_ENABLE_THREADS=ON + -DBUILD_SHARED_LIBS=OFF + "-DCMAKE_INSTALL_PREFIX=${SIMDJSON_PREFIX}") + + set(SIMDJSON_STATIC_LIB + "${SIMDJSON_LIB_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}simdjson${CMAKE_STATIC_LIBRARY_SUFFIX}" + ) + + externalproject_add(simdjson_ep + ${EP_COMMON_OPTIONS} + PREFIX "${CMAKE_BINARY_DIR}" + URL ${SIMDJSON_SOURCE_URL} + URL_HASH "SHA256=${ARROW_SIMDJSON_BUILD_SHA256_CHECKSUM}" + CMAKE_ARGS ${SIMDJSON_CMAKE_ARGS} + BUILD_BYPRODUCTS "${SIMDJSON_STATIC_LIB}") Review Comment: Could you use FetchContent not ExternalProject like other bundled dependencies? ########## cpp/src/arrow/json/object_parser.cc: ########## @@ -16,72 +16,115 @@ // under the License. #include "arrow/json/object_parser.h" -#include "arrow/json/rapidjson_defs.h" // IWYU pragma: keep -#include <rapidjson/document.h> +#include <simdjson.h> namespace arrow { namespace json { namespace internal { -namespace rj = arrow::rapidjson; - class ObjectParser::Impl { public: Status Parse(std::string_view json) { - document_.Parse(reinterpret_cast<const rj::Document::Ch*>(json.data()), - static_cast<size_t>(json.size())); + // Copy into padded buffer + padded_json_ = simdjson::padded_string(json); + + // Parse + auto result = parser_.parse(padded_json_); Review Comment: Can we use `parser_.iterate()` not `parser_.parse()`? https://github.com/simdjson/simdjson/blob/master/doc/basics.md#the-basics-loading-and-parsing-json-documents -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
