Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not intern extension type strings #260

Merged
merged 1 commit into from
Feb 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 6 additions & 10 deletions ext/msgpack/unpacker.c
Original file line number Diff line number Diff line change
Expand Up @@ -291,21 +291,17 @@ static inline int read_raw_body_begin(msgpack_unpacker_t* uk, int raw_type)
VALUE symbol = msgpack_buffer_read_top_as_symbol(UNPACKER_BUFFER_(uk), length, raw_type != RAW_TYPE_BINARY);
ret = object_complete_symbol(uk, symbol);
} else {
/* don't use zerocopy for hash keys but get a frozen string directly
* because rb_hash_aset freezes keys and it causes copying */
bool will_freeze = uk->freeze || is_reading_map_key(uk);
VALUE string = msgpack_buffer_read_top_as_string(UNPACKER_BUFFER_(uk), length, will_freeze, raw_type == RAW_TYPE_STRING);
bool will_freeze = uk->freeze;
if(raw_type == RAW_TYPE_STRING || raw_type == RAW_TYPE_BINARY) {
/* don't use zerocopy for hash keys but get a frozen string directly
* because rb_hash_aset freezes keys and it causes copying */
will_freeze = will_freeze || is_reading_map_key(uk);
VALUE string = msgpack_buffer_read_top_as_string(UNPACKER_BUFFER_(uk), length, will_freeze, raw_type == RAW_TYPE_STRING);
ret = object_complete(uk, string);
} else {
VALUE string = msgpack_buffer_read_top_as_string(UNPACKER_BUFFER_(uk), length, false, false);
ret = object_complete_ext(uk, raw_type, string);
}

# if !HASH_ASET_DEDUPE
if(will_freeze) {
rb_obj_freeze(string);
}
# endif
}
uk->reading_raw_remaining = 0;
return ret;
Expand Down
12 changes: 10 additions & 2 deletions spec/factory_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,8 @@ def to_msgpack_ext
end

describe 'the special treatment of symbols with ext type' do
def roundtrip(object)
subject.load(subject.dump(object))
def roundtrip(object, options = nil)
subject.load(subject.dump(object), options)
end

context 'using the optimized symbol unpacker' do
Expand Down Expand Up @@ -333,6 +333,14 @@ def roundtrip(object)
expect(roundtrip(:symbol)).to be :symbol
end

it 'works with hash keys' do
expect(roundtrip(symbol: 1)).to be == { symbol: 1 }
end

it 'works with frozen: true option' do
expect(roundtrip(:symbol, freeze: true)).to be :symbol
end

it 'preserves encoding for ASCII symbols' do
expect(:symbol.encoding).to be Encoding::US_ASCII
expect(roundtrip(:symbol)).to be :symbol
Expand Down