Skip to content

Commit

Permalink
perf: add constexpr for json::unicode
Browse files Browse the repository at this point in the history
  • Loading branch information
MistEO committed Dec 29, 2023
1 parent 5be715d commit fec0bb8
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 21 deletions.
30 changes: 13 additions & 17 deletions include/json5.hpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#pragma once

#include <algorithm>
#include <cctype>
#include <cmath>
#include <iomanip>
#include <regex>
#include <sstream>
#include <stack>
#include <vector>
#include <algorithm>

#include "json.hpp"
#include "unicode.h"
Expand Down Expand Up @@ -102,7 +102,7 @@ class parser5
static bool isHexDigit(u8char ch);
static u8char toUnicode(u8char ch);
template <typename ArrT>
static bool findInRange(const ArrT &range, /* unicode code point */ u8char codePoint);
static bool findInRange(const ArrT& range, /* unicode code point */ u8char codePoint);
};

enum class LexState
Expand Down Expand Up @@ -175,9 +175,7 @@ class parser5
static std::optional<value> parse(const StringT& content, std::string* error = nullptr);

private:
parser5(StringIterT cbegin, StringIterT cend) noexcept
: _cur(cbegin), _end(cend), _line_begin_cur(cbegin)
{}
parser5(StringIterT cbegin, StringIterT cend) noexcept : _cur(cbegin), _end(cend), _line_begin_cur(cbegin) {}
std::optional<value> parse();

private:
Expand Down Expand Up @@ -313,32 +311,30 @@ inline uint64_t parser5<StringT>::unicode::toUnicode(u8char ch)
u8char charcode = 0;
uint8_t t = coded.top();
coded.pop();
if (t < 128)
{
if (t < 128) {
return t;
}
uint8_t high_bit_mask = (1 << 6) -1;
uint8_t high_bit_mask = (1 << 6) - 1;
uint8_t high_bit_shift = 0;
int total_bits = 0;
const int other_bits = 6;
while((t & 0xC0) == 0xC0)
{
while ((t & 0xC0) == 0xC0) {
t <<= 1;
t &= 0xff;
total_bits += 6;
high_bit_mask >>= 1;
high_bit_mask >>= 1;
high_bit_shift++;
charcode <<= other_bits;
charcode |= coded.top() & ((1 << other_bits)-1);
charcode |= coded.top() & ((1 << other_bits) - 1);
coded.pop();
}
}
charcode |= ((t >> high_bit_shift) & high_bit_mask) << total_bits;
return charcode;
}

template <typename StringT>
template <typename ArrT>
inline bool parser5<StringT>::unicode::findInRange(const ArrT &range, u8char codePoint)
inline bool parser5<StringT>::unicode::findInRange(const ArrT& range, u8char codePoint)
{
const auto begin = std::begin(range);
const auto end = std::end(range);
Expand Down Expand Up @@ -626,7 +622,7 @@ inline typename parser5<StringT>::Token parser5<StringT>::lex()
return token.value();
}
}
return Token{ TokenType::eof, value(), _line, _col };
return Token { TokenType::eof, value(), _line, _col };
}

template <typename StringT>
Expand Down Expand Up @@ -1057,7 +1053,7 @@ inline std::optional<typename parser5<StringT>::Token> parser5<StringT>::lex_dec
_buffer += StringFromCharCode(read());
return std::nullopt;
}

std::string number = _sign == -1 ? ("-" + _buffer) : _buffer;
return newToken(TokenType::numeric, value(value::value_type::number, std::move(number)));
}
Expand All @@ -1080,7 +1076,7 @@ inline std::optional<typename parser5<StringT>::Token> parser5<StringT>::lex_hex
_buffer += StringFromCharCode(read());
return std::nullopt;
}

std::string number = _sign == -1 ? ("-" + _buffer) : _buffer;
return newToken(TokenType::numeric, value(value::value_type::number, std::move(number)));
}
Expand Down
6 changes: 3 additions & 3 deletions include/unicode.h

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion tools/unicode.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ header += '#include <array>\n'
header += 'namespace json::unicode {\n'

header += Object.keys(data).map(key => `
std::array<uint64_t, ${data[key].toArray().length}> ${key}
constexpr std::array<uint64_t, ${data[key].toArray().length}> ${key}
{${data[key].toArray().map(v => '0x' + Number(v).toString(16)).join(', ')}}
;
`).join('')
Expand Down

0 comments on commit fec0bb8

Please sign in to comment.