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

Optimize literal checks in untagged variants #6393

Open
zth opened this issue Sep 7, 2023 · 2 comments
Open

Optimize literal checks in untagged variants #6393

zth opened this issue Sep 7, 2023 · 2 comments

Comments

@zth
Copy link
Collaborator

zth commented Sep 7, 2023

No description provided.

Copy link

github-actions bot commented Sep 2, 2024

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@github-actions github-actions bot added the stale Old issues that went stale label Sep 2, 2024
@zth
Copy link
Collaborator Author

zth commented Sep 2, 2024

This is still a thing I'd like us to have a look at eventually. It's about untagged variants sometimes producing excessive code. Example:

let v = JSON.Array([String("hello")])

let x = x =>
  switch x {
  | JSON.Array(arr) =>
    arr
    ->Array.filterMap(a =>
      switch a {
      | String(s) => Some(s)
      | _ => None
      }
    )
    ->Array.join(",")
  | _ => "-"
  }

Produces this JS:

import * as Core__Array from "./stdlib/core__Array.js";

var v = ["hello"];

function x(x$1) {
  if (!Array.isArray(x$1) && (x$1 === null || typeof x$1 !== "object") && typeof x$1 !== "number" && typeof x$1 !== "string" && typeof x$1 !== "boolean" || !Array.isArray(x$1)) {
    return "-";
  } else {
    return Core__Array.filterMap(x$1, (function (a) {
                    if (!Array.isArray(a) && (a === null || typeof a !== "object") && typeof a !== "number" && typeof a !== "string" && typeof a !== "boolean" || typeof a !== "string") {
                      return ;
                    } else {
                      return a;
                    }
                  })).join(",");
  }
}

2 main things:

  1. The if that checks the array: if (!Array.isArray(x$1) && (x$1 === null || typeof x$1 !== "object") && typeof x$1 !== "number" && typeof x$1 !== "string" && typeof x$1 !== "boolean" || !Array.isArray(x$1)). This checks all the ways it can't be an array, but it could just do Array.isArray(x$1).
  2. Checking each element f (!Array.isArray(a) && (a === null || typeof a !== "object") && typeof a !== "number" && typeof a !== "string" && typeof a !== "boolean" || typeof a !== "string") it's looking for a string, so it could just check that it's a string via typeof a === "string". But instead it checks all of the negative cases.

This might not be trivial to fix and is probably a property of how the pattern matching engine is written, but I figured I'd log it anyway so we have it in the tracker.

Playground link: https://rescript-lang.org/try?version=v11.1.3&code=DYUwLgBAbhC8ECkDKB5AcgOgIICccEMBPACgG0kwcBLAOwHNiAiACxGGAHtGBKAXW4BQA0JAAecCONgA+ARAgBnAO5UwAY2aSIAbzkQAPolSZcBEvjzc4s+fIs498gLTTTRDADMqwMCBwBZfAAHYnxrR1tlVQ0IMN1bW0MKanpiBSsZCCQOAFsQNMEE+UMAfWsINA4aEAj5AF8IwtsXN0IMACsOWiYAGh49UvLGJ0Y9BqA

@github-actions github-actions bot removed the stale Old issues that went stale label Sep 9, 2024
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

No branches or pull requests

1 participant