Files
strager 5510089d45 Improve build times for debug builds (#1859)
* Rename simdjson_really_inline -> simdjson_inline

I want to change the simdjson_really_inline macro to sometimes not force
inlining. After that upcoming change, the name simdjson_really_inline
will no longer makes sense.

Rename simdjson_really_inline to simdjson_inline. This patch should not
change semantics; simdjson_inline still forces inlining as before.

Some functions still need to be really inlined for ABI reasons.
(GCC's -Wpsabi complains otherwise.) Leave those functions marked as
simdjson_really_inline.

* Improve build times for debug builds

simdjson_inline is used for most simdjson functions. It forces inlining.
In unoptimized/debug builds, this can lead to a lot of machine code
being generated (especially with Address Sanitizer), causing slow
compilation.

Change simdjson_inline to force inlining only for optimized builds.

Sometimes, the programmer might want a slightly-optimized build and want
fast compilation (e.g. GCC's -Og mode). Allow simdjson users to define
the simdjson_inline macro themselves (e.g. on the command line:
-Dsimdjson_inline=inline) in cases where the default behavior is
undesired.

This patch reduced build times by over 75% for ondemand_object_tests.cpp
with GCC 9.4.0 and CMAKE_BUILD_TYPE=Debug on my AMD 5950X:

Before: 6.885 6.683 6.971 6.957 6.949 seconds (5 samples)
After:  1.492 1.551 1.494 1.490 1.531 seconds (5 samples)
2022-07-19 15:14:33 -04:00

139 lines
5.2 KiB
C++

namespace simdjson {
namespace SIMDJSON_IMPLEMENTATION {
namespace ondemand {
/**
* Iterates through JSON tokens (`{` `}` `[` `]` `,` `:` `"<string>"` `123` `true` `false` `null`)
* detected by stage 1.
*
* @private This is not intended for external use.
*/
class token_iterator {
public:
/**
* Create a new invalid token_iterator.
*
* Exists so you can declare a variable and later assign to it before use.
*/
simdjson_inline token_iterator() noexcept = default;
simdjson_inline token_iterator(token_iterator &&other) noexcept = default;
simdjson_inline token_iterator &operator=(token_iterator &&other) noexcept = default;
simdjson_inline token_iterator(const token_iterator &other) noexcept = default;
simdjson_inline token_iterator &operator=(const token_iterator &other) noexcept = default;
/**
* Advance to the next token (returning the current one).
*/
simdjson_inline const uint8_t *return_current_and_advance() noexcept;
/**
* Reports the current offset in bytes from the start of the underlying buffer.
*/
simdjson_inline uint32_t current_offset() const noexcept;
/**
* Get the JSON text for a given token (relative).
*
* This is not null-terminated; it is a view into the JSON.
*
* @param delta The relative position of the token to retrieve. e.g. 0 = current token,
* 1 = next token, -1 = prev token.
*
* TODO consider a string_view, assuming the length will get stripped out by the optimizer when
* it isn't used ...
*/
simdjson_inline const uint8_t *peek(int32_t delta=0) const noexcept;
/**
* Get the maximum length of the JSON text for a given token.
*
* The length will include any whitespace at the end of the token.
*
* @param delta The relative position of the token to retrieve. e.g. 0 = current token,
* 1 = next token, -1 = prev token.
*/
simdjson_inline uint32_t peek_length(int32_t delta=0) const noexcept;
/**
* Get the JSON text for a given token.
*
* This is not null-terminated; it is a view into the JSON.
*
* @param position The position of the token.
*
*/
simdjson_inline const uint8_t *peek(token_position position) const noexcept;
/**
* Get the maximum length of the JSON text for a given token.
*
* The length will include any whitespace at the end of the token.
*
* @param position The position of the token.
*/
simdjson_inline uint32_t peek_length(token_position position) const noexcept;
/**
* Return the current index.
*/
simdjson_inline token_position position() const noexcept;
/**
* Reset to a previously saved index.
*/
simdjson_inline void set_position(token_position target_position) noexcept;
// NOTE: we don't support a full C++ iterator interface, because we expect people to make
// different calls to advance the iterator based on *their own* state.
simdjson_inline bool operator==(const token_iterator &other) const noexcept;
simdjson_inline bool operator!=(const token_iterator &other) const noexcept;
simdjson_inline bool operator>(const token_iterator &other) const noexcept;
simdjson_inline bool operator>=(const token_iterator &other) const noexcept;
simdjson_inline bool operator<(const token_iterator &other) const noexcept;
simdjson_inline bool operator<=(const token_iterator &other) const noexcept;
protected:
simdjson_inline token_iterator(const uint8_t *buf, token_position position) noexcept;
/**
* Get the index of the JSON text for a given token (relative).
*
* This is not null-terminated; it is a view into the JSON.
*
* @param delta The relative position of the token to retrieve. e.g. 0 = current token,
* 1 = next token, -1 = prev token.
*/
simdjson_inline uint32_t peek_index(int32_t delta=0) const noexcept;
/**
* Get the index of the JSON text for a given token.
*
* This is not null-terminated; it is a view into the JSON.
*
* @param position The position of the token.
*
*/
simdjson_inline uint32_t peek_index(token_position position) const noexcept;
const uint8_t *buf{};
token_position _position{};
friend class json_iterator;
friend class value_iterator;
friend class object;
friend simdjson_inline void logger::log_line(const json_iterator &iter, const char *title_prefix, const char *title, std::string_view detail, int delta, int depth_delta) noexcept;
friend simdjson_inline void logger::log_line(const json_iterator &iter, token_position index, depth_t depth, const char *title_prefix, const char *title, std::string_view detail) noexcept;
};
} // namespace ondemand
} // namespace SIMDJSON_IMPLEMENTATION
} // namespace simdjson
namespace simdjson {
template<>
struct simdjson_result<SIMDJSON_IMPLEMENTATION::ondemand::token_iterator> : public SIMDJSON_IMPLEMENTATION::implementation_simdjson_result_base<SIMDJSON_IMPLEMENTATION::ondemand::token_iterator> {
public:
simdjson_inline simdjson_result(SIMDJSON_IMPLEMENTATION::ondemand::token_iterator &&value) noexcept; ///< @private
simdjson_inline simdjson_result(error_code error) noexcept; ///< @private
simdjson_inline simdjson_result() noexcept = default;
simdjson_inline ~simdjson_result() noexcept = default; ///< @private
};
} // namespace simdjson