-
-
Notifications
You must be signed in to change notification settings - Fork 779
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
Lift literal expressions to top-level constants in JavaScript code generation #4090
Comments
just for reference, the It happens because the |
glad i wasn't hullcinating thank you |
The goal here would be to identify side-effect free expressions that do not depend on any function parameter or other variable data and to lift them to the top level as a JS const. For example: import { toList } from "./gleam.mjs";
import * as $io from "./gleam/io.mjs";
export function main() {
let x = toList([1, 2, 3]);
return $io.debug(x);
} would become import { toList } from "./gleam.mjs";
import * as $io from "./gleam/io.mjs";
const x = toList([1, 2, 3]);
export function main() {
return $io.debug(x);
} We would also need to decide on some scheme for renaming the variables as they are no longer scoped to just the function, so they could collide if left as-is. |
How do we decide which expressions to lift? Because something like: import { toList } from "./gleam.mjs";
import * as $io from "./gleam/io.mjs";
const one = 1;
const two = 2;
const three = 3;
const x = toList([one, two, three]);
export function main() {
return $io.debug(x);
} Seems a little over the top. Also, should we ensure we don't have duplicate constant definitions? So if there were two literal lists, |
Lifting everything seems fine to me. Deduplication could be a nice future addition. |
Won't that result in a large increase in generated code size? Is that something we care about at all? |
No because every expression added to the top level will have an identical expression removed from a function. It will be the same amount of code. |
you can use gleam csv package to turn a csv file to gleam list then do
io.debug(parssed_csv)
make the list be
let list = [ // here will be the long list ]
do some computation to the data for me it was getting a specific element from the list
change the list from
let list =
toconst list
(make sure its a process that would take 1 or more seconds so you can notice the difference visually)
i noticed this in a lustre application i needed to get the element number 34 from list number 77 this took 4 seconds just by changing the list to const it was instant
( the version this happened on was 1.5 iirc it good to test if it still happens now )
The text was updated successfully, but these errors were encountered: