threepointone / bs-nice

css-in-reason
180 stars 14 forks source link

Use bucklescript string interpolation instead of ocaml stdlib #3

Closed thangngoc89 closed 6 years ago

thangngoc89 commented 6 years ago

As I can see, nice is a string concatenation library and you used ocaml function like string_of_int a lot. But calling these function should have some performance cost.

Example:

832cfaa2-9f50-40e7-8d5c-1015cc888181

Link here https://reasonml.github.io/try/?reason=DYUwLgBAZg9jEF4IAoAeBKRA+CBnMATgJYB2A5gPoxQWlhqYDUjEARAA6qsDcAUKJABGAQwKIUqAFwQ6mBDgDeAKwA+AEgYqlAX25A

Bucklescript docs on string interpolation https://bucklescript.github.io/docs/en/common-data-types.html#interpolation

thangngoc89 commented 6 years ago

I created a benchmark here https://rebench.github.io/?rebench-data=NoIgNgpgLgBAHjAvDALAJgNwHoBUMDOAFgIYBOEAJjAMYD2FEMA5rRPjIROTDliADTBQAZgEgAVvjEIA1DAA6IAA5xFIALqCQaMeTEBvcQB8AJAAo4AShVHxAXw1aAjLohj8UUgEsAdkwD6tABm-r5QFpYwMnKKKmrqCUA

Since I’m on mobile, the result maybe unreliable. Could any of you run it on a laptop and report it back here?

3e7d91ce-6b27-4b77-bd9f-bbcaef7995d4

threepointone commented 6 years ago

thanks! I'm only just learning reason, so this is good to know. I'm trying to keep this to pure reason, so any idea how this will compile to native?

I also checked with rollup + closure(simple), and string_of_int gets inlined, making it as fast as the 3rd one.

I also messed with the benchmark, and it appears the advantage disappears when you're actually doing something with the value (note that the 3rd output doesn't return anything). eg - adding a console.log(value) brings them all closer together.

ulrikstrid commented 6 years ago

If you use the bucklescript specific implementation I don't think it will compile to native. Maybe bucklescript should compile string_of_int down to the second example if possible. cc @bobzhang

thangngoc89 commented 6 years ago

After posting this issue, it appears that this is string_of_int:

function string_of_int (x) {
  return "" + x
}

So it's actually what I would write in JS. Sorry for the noises. So the only advantage of using {j I can think of is it's much easier to use

threepointone commented 6 years ago

I'll close this out for now, but feel free to carry on