Skip to content

Commit

Permalink
Use rb_itern for object keys if the string is ASCII-only
Browse files Browse the repository at this point in the history
  • Loading branch information
byroot committed Jul 21, 2020
1 parent 5a0de2f commit c14d0e7
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions ext/fast_jsonparser/fast_jsonparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ VALUE rb_eFastJsonparserUnknownError, rb_eFastJsonparserParseError;

using namespace simdjson;

static inline bool is_ascii(const std::string_view &v) {
for (size_t i = 0; i < v.size(); i++) {
if (static_cast<unsigned char>(v[i]) >= 128) {
return false;
}
}
return true;
}


// Convert tape to Ruby's Object
static VALUE make_ruby_object(dom::element element)
{
Expand All @@ -27,9 +37,14 @@ static VALUE make_ruby_object(dom::element element)
for (dom::key_value_pair field : dom::object(element))
{
std::string_view view(field.key);
VALUE k = rb_intern_str(rb_utf8_str_new(view.data(), view.size()));
VALUE k;
if (is_ascii(view)) {
k = ID2SYM(rb_intern2(view.data(), view.size()));
} else {
k = ID2SYM(rb_intern_str(rb_utf8_str_new(view.data(), view.size())));
}
VALUE v = make_ruby_object(field.value);
rb_hash_aset(hash, ID2SYM(k), v);
rb_hash_aset(hash, k, v);
}
return hash;
}
Expand Down

0 comments on commit c14d0e7

Please sign in to comment.