Skip to content

Remove get_actual_encoding() and the dynamic endian detection for dummy UTF-16/UTF-32 - #6322

Merged
eregon merged 1 commit into
ruby:masterfrom
eregon:deprecate_replicate_dummy_encodings
Sep 12, 2022
Merged

eregon merged 1 commit into
ruby:masterfrom
eregon:deprecate_replicate_dummy_encodings

Conversation

@eregon

@eregon eregon commented Sep 3, 2022

Copy link
Copy Markdown
Member

@eregon
eregon requested a review from nobu September 3, 2022 14:06
@eregon
eregon force-pushed the deprecate_replicate_dummy_encodings branch from 3d3243d to 2d42bf9 Compare September 3, 2022 14:53
@nobu

nobu commented Sep 4, 2022

Copy link
Copy Markdown
Member

At that comment, you wrote:

For 'UTF-16' and 'UTF-32', we agreed that there is no point in getting the endianness dynamically every time the string is accessed. We will try to change the implementation so that the endianness is evaluated only once (string creation time) and that information can then be reused. This will evaluate some checks in 'hot' code.

Why deprecating these encodings themselves?

@eregon

eregon commented Sep 4, 2022 •

Copy link
Copy Markdown
Member Author

Why deprecating these encodings themselves?

From https://github.com/ruby/dev-meeting-log/blob/master/DevMeeting-2022-08-18.md#feature-18949-deprecate-and-remove-replicate-and-dummy-encodings-eregon :
2022-08-25

  • UTF-16/32
    • akr: why such dynamic endian detection is needed?
    • naruse: Implemented for https://bugs.ruby-lang.org/issues/8940, but it doesn't have actual use case
    • naruse: the first 2/4 byte of a UTF-16/32 string is not sure whether it is a BOM or ZWNBSP. When people want to handle the string, they first need to decide it is BOM or ZWNBSP
    • akr: if so, they need to convert them first. What is the benefit of the feature? Maybe we can just remove the dynamic detection if we explain how to convert UTF/32 string into other encoding.
    • naruse: For example str.encode("UTF-8", "UTF16"), it assumes the first 2 bytes is a BOM, detect its endian, and convert it into UTF-8
    • Conclusion: Just remove the dynamic UTF-16/32 endian detection. People should convert the string first.

My understanding of this, is we don't need the dummy UTF-16/32 encodings (unnecessary, no actual use case), i.e., we don't need that feature and users can specify the endian explicitly from the start instead ("People should convert the string first.").
Also the feature is kind of broken, since we cannot decide BOM or ZWNBSP. So the user needs to decide anyway, and at that point there is no value to have this automatic detection with the dummy UTF-16/32 encodings.
The fact literally no test/spec ever uses that BOM detection besides the very few tests actually testing dummy UTF-16/32 directly also seems a sign nobody uses or needs this feature (i.e., no higher-level test ever uses the automatic detection of these encodings). All the higher-level usages I could find in tests/specs were simply mistakes, they should have been BE/LE explicitly (e.g., test/ruby/test_io_m17n.rb, test/json/json_parser_test.rb, etc).
But even if we find one real usage of the BOM detection we could easily replace it by a check of the first 2/4 bytes, or a helper method.

This will also remove a lot of unnecessary complexity.

@eregon

eregon commented Sep 10, 2022

Copy link
Copy Markdown
Member Author

@akr @nurse Could you confirm whether my understanding is correct in the above comment?

@nurse

nurse commented Sep 10, 2022

Copy link
Copy Markdown
Member

My understanding of this, is we don't need the dummy UTF-16/32 encodings (unnecessary, no actual use case), i.e., we don't need that feature and users can specify the endian explicitly from the start instead ("People should convert the string first.").

My comment ("it doesn't have actual use case") is about " the dynamic UTF-16/32 endian detection". Removing UTF-16/32 dummy encoding itself is not accepted.

Also the feature is kind of broken, since we cannot decide BOM or ZWNBSP. So the user needs to decide anyway, and at that point there is no value to have this automatic detection with the dummy UTF-16/32 encodings.

Your understanding of character encoding is based on Universal Character Set Normalization model. In the UCS model, all strings are converted into the UCS to handle as a string.
But Ruby use CSI (Character Set Independence) model. In this model strings have their own encoding and string processing reflect the encoding. Under this model one of the principle is "an input can be a string object without conversion". Without UTF-16/32 dummy encoding, we cannot achieve the principle.

I think you can read the log carefully. It says "Conclusion: Just remove the dynamic UTF-16/32 endian detection. People should convert the string first.". I think there are no ambiguously.

@eregon

eregon commented Sep 10, 2022

Copy link
Copy Markdown
Member Author

Oh, OK, thank you for clarifying.

I did read it carefully multiple times, for me "remove the dynamic UTF-16/32 endian detection" means remove dummy UTF encodings. I misunderstood the "dynamic" I think, where you mean "detecting it on each string operation vs once" and I thought it means "automatic, based on the string first bytes".

"People should convert the string first.". also clearly means for me they should "attach/decide" BE/LE when creating the string/when giving it an encoding (so not give it a dummy unicode encoding but a BE or LE encoding).
I guess you mean if they don't do that and use dummy unicode encodings, you still want the automatic detection as fallback?
I think such fallback is so rare that IMHO it is not worth keeping that behavior (and the significant complexity that comes with it).

Re UCS vs CSI, I am well aware Ruby is M17N and has strings of multiple encodings in the same process.
I don't think the special handling of "unknown endian UTF-16/32" is so important for that. They are literally the only 2 encodings with such magic behavior.
Removing those does not mean eager conversion, in fact the bytes would be untouched anyway, it just means you have to tell Ruby if it's BE or LE when giving an encoding to the string, not let it guess it and hope there is a BOM to guess correctly otherwise it will treat the string as binary which is very much broken and nobody wants.

I will deprecate and remove the dummy UTF-16/32 encodings in TruffleRuby (currently they behave the same as -BE variants), because I believe they are not worth the complexity and nobody uses them in the real world (or they could very easily use UTF-16/32-BE/LE instead). I wish we could do the same in CRuby for better compatibility between implementations, but it seems Ruby core is not ready for that yet, alright.

@nurse Could you clarify this:
So in CRuby we'll just do detection at String build time rather on each String operation then for dummy UTF-16/32? (which does mean String#encoding will never return dummy UTF-16/32, that's a good improvement)
And actually give it a BINARY encoding if there is no BOM then? Or raise an exception if no BOM?

@nurse

nurse commented Sep 10, 2022

Copy link
Copy Markdown
Member

@nurse Could you clarify this: So in CRuby we'll just do detection at String build time rather on each String operation then for dummy UTF-16/32? (which does mean String#encoding will never return dummy UTF-16/32, that's a good improvement) And actually give it a BINARY encoding if there is no BOM then? Or raise an exception if no BOM?

No. The current consensus is...

  • You can remove

    ruby/string.c

    Lines 363 to 384 in b0b9f72

    switch (encidx) {
    case ENCINDEX_UTF_16:
    if (RSTRING_LEN(str) < 2) break;
    q = (const unsigned char *)RSTRING_PTR(str);
    if (q[0] == 0xFE && q[1] == 0xFF) {
    return rb_enc_get_from_index(ENCINDEX_UTF_16BE);
    }
    if (q[0] == 0xFF && q[1] == 0xFE) {
    return rb_enc_get_from_index(ENCINDEX_UTF_16LE);
    }
    return rb_ascii8bit_encoding();
    case ENCINDEX_UTF_32:
    if (RSTRING_LEN(str) < 4) break;
    q = (const unsigned char *)RSTRING_PTR(str);
    if (q[0] == 0 && q[1] == 0 && q[2] == 0xFE && q[3] == 0xFF) {
    return rb_enc_get_from_index(ENCINDEX_UTF_32BE);
    }
    if (q[3] == 0 && q[2] == 0 && q[1] == 0xFE && q[0] == 0xFF) {
    return rb_enc_get_from_index(ENCINDEX_UTF_32LE);
    }
    return rb_ascii8bit_encoding();
    }
  • You cannot remove UTF-16/32 dummy encoding
  • We don't introduce build time encoding detection for UTF-16/32

@eregon

eregon commented Sep 10, 2022

Copy link
Copy Markdown
Member Author

Ah, so that basically (almost) means Encoding::UTF_32 = Encoding::UTF_32BE, except they remain separate Encoding instances. That's a nice simplification. Thanks for clarifying. I'll update the PR.

@nurse

nurse commented Sep 10, 2022 •

Copy link
Copy Markdown
Member

so that basically (almost) means Encoding::UTF_32 = Encoding::UTF_32BE

No. After the change, Encoding::UTF_32 won't provide any string operation like Encoding::ISO_2022_JP

@eregon

eregon commented Sep 10, 2022

Copy link
Copy Markdown
Member Author

Ah yes, it will be like "any other dummy encoding".

@eregon

eregon commented Sep 10, 2022 •

Copy link
Copy Markdown
Member Author

@nurse Because of

ruby/enc/utf_16_32.h

Lines 4 to 5 in c22a668

ENC_DUMMY_UNICODE("UTF-16");
ENC_DUMMY_UNICODE("UTF-32");
, Encoding::UTF_32 behaves essentially the same as Encoding::UTF_32BE (same for UTF-16).
For example:

irb(main):026:0> "\x00\x00\xFE\xFF".force_encoding("UTF-32BE").codepoints
=> [65279]
irb(main):027:0> "\x00\x00\xFE\xFF".force_encoding("UTF-32").codepoints
=> [65279]
irb(main):028:0> "\x00\x00\xFE\xFF".force_encoding("UTF-32LE").codepoints
=> [4294836224]
irb(main):029:0> "\x00\x00\xFE\xFF".force_encoding("BINARY").codepoints
=> [0, 0, 254, 255]
irb(main):030:0> "\x00\x00\xFE\xFF".force_encoding("ISO-2022-JP").codepoints
=> [0, 0, 254, 255]

Is that OK?
Or should it be like a regular "single-byte" dummy encoding like Encoding::ISO_2022_JP?

eregon added a commit to eregon/ruby that referenced this pull request Sep 10, 2022
…my UTF-16/UTF-32

* And simplify callers of get_actual_encoding().
* See [Feature ruby#18949].
* See ruby#6322 (comment)
@eregon
eregon force-pushed the deprecate_replicate_dummy_encodings branch from 2d42bf9 to de32eca Compare September 10, 2022 17:39
@eregon eregon changed the title Deprecate Encoding#replicate and the dummy Encoding::UTF_16 and Encoding::UTF_32 encodings Remove get_actual_encoding() and the dynamic endian detection for dummy UTF-16/UTF-32 Sep 10, 2022
@eregon

eregon commented Sep 10, 2022

Copy link
Copy Markdown
Member Author

I think it makes more sense to have them as regular dummy encodings and use ENC_DUMMY (i.e., undo 3a2aca5)

…my UTF-16/UTF-32

* And simplify callers of get_actual_encoding().
* See [Feature ruby#18949].
* See ruby#6322 (comment)
@eregon
eregon force-pushed the deprecate_replicate_dummy_encodings branch from de32eca to d9b88c7 Compare September 10, 2022 18:22
Comment thread enc/encdb.c
#define ENC_DEFINE(name) rb_encdb_declare(name)
#define ENC_SET_BASE(name, orig) rb_enc_set_base((name), (orig))
#define ENC_SET_DUMMY(name, orig) rb_enc_set_dummy(name)
#define ENC_DUMMY_UNICODE(name) rb_encdb_set_unicode(rb_enc_set_dummy(ENC_REPLICATE((name), name "BE")))

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing rb_encdb_set_unicode() is necessary here, so that:

> "\u3042".encode("UTF-32BE").force_encoding("UTF-32")
=> "\x00\x00\x30\x42"
> "\u3042".encode("UTF-32BE").force_encoding("ISO-2022-JP")
=> "\x00\x00\x30\x42"

and not:

s = "\u3042".encode("UTF-32BE")
s.dup.force_encoding("UTF-32").inspect
=> "\"\\u0000\\u00000B\""
irb(main):005:0> s.dup.force_encoding("UTF-16").inspect
=> "\"\\u0000\\u00000B\""

Since the latter seems quite wrong (interpreting single bytes as unicode/ASCII characters in UTF-16/32)

@eregon
eregon requested a review from nurse September 10, 2022 18:28
@eregon

eregon commented Sep 10, 2022

Copy link
Copy Markdown
Member Author

This is now ready for review. The dummy UTF-16 and UTF-32 encodings are now exactly like all other dummy encodings.

@nurse nurse left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Thank for the change!

@eregon
eregon merged commit 6525b6f into ruby:master Sep 12, 2022
@eregon
eregon deleted the deprecate_replicate_dummy_encodings branch January 6, 2023 14:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants