Skip to content

Remove LEVELS() accessor #1726

Closed
Closed

Description

@lionel-

Part of #1706

LEVELS() was added in #1187

Used to detect non-utf8 strings in r_obj_encode_utf8() and optimise the default case of no encoding conversion needed:

  • Avoid cloning a STRSXP if no translation is needed.
  • Avoid allocating a CHARSXP if no translation is needed (in case of mixed encodings in the vector, less important I guess?).

We could just call Encoding(x) <- "UTF8" from R. This will unconditionally clone the input vector unless it doesn't have any references. To preserve our optimisation, either of these would work:

  • Add a predicate to the C API to determine if a CHARSXP is encoded in UTF8

  • Improve do_setencoding() to only duplicate if needed so that it's a noop in the common case. Bonus points if exported on C side, e.g. as Rf_EnsureUtf8()?

My sense is that these new unconditional allocs would be bad for performance in dplyr. @DavisVaughan you added these optimisations for vctrs, could you confirm please?

Activity

  1. lionel- commented on Jul 2, 2024

    @lionel-
    MemberAuthor

    There's now wch/r-source@0c753e4 so we should be able to preserve our optimisations.

  2. added a commit that references this issue on Jul 2, 2024
    a4bbdf2
  3. added 3 commits that reference this issue on Apr 2, 2025
    b9b2616
    534a928
    8ef43af
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions