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

Faster binary operations #388

Open
wants to merge 3 commits into
base: v3
Choose a base branch
from
Open

Faster binary operations #388

wants to merge 3 commits into from

Conversation

geseq
Copy link
Collaborator

@geseq geseq commented Jul 20, 2022

Improves BinaryOp performance significantly

Before:

-------------------------------------
fibonacci(35)
-------------------------------------
Result:  9227465
Go:      42.388955ms
Parser:  36.718µs
Compile: 109.024µs
VM:      2.787647146s
-------------------------------------
fibonacci(35) (tail-call #1)
-------------------------------------
Result:  9227465
Go:      42.789713ms
Parser:  21.297µs
Compile: 70.948µs
VM:      2.894222823s
-------------------------------------
fibonacci(35) (tail-call #2)
-------------------------------------
Result:  9227465
Go:      256ns
Parser:  17.864µs
Compile: 60.804µs
VM:      14.668µs 

After:

-------------------------------------
fibonacci(35)
-------------------------------------
Result:  9227465
Go:      41.610339ms
Parser:  33.751µs
Compile: 98.728µs
VM:      2.252044656s
-------------------------------------
fibonacci(35) (tail-call #1)
-------------------------------------
Result:  9227465
Go:      41.661108ms
Parser:  18.044µs
Compile: 59.931µs
VM:      2.700379139s
-------------------------------------
fibonacci(35) (tail-call #2)
-------------------------------------
Result:  9227465
Go:      240ns
Parser:  22.475µs
Compile: 70.738µs
VM:      22.121µs

@geseq geseq requested a review from d5 July 20, 2022 09:11
@beoran
Copy link

beoran commented Jul 20, 2022

@geseq While value based semantics can indeed improve performance, this drastically changes the API of Tengo. It would need to be versioned as v3 to make this change.

@geseq
Copy link
Collaborator Author

geseq commented Jul 20, 2022

I agree

@beoran
Copy link

beoran commented Jul 20, 2022

@geseq In that case, it might be an idea to try to implement full value semantics for all types for v3, not just for integrets. This might seem strange for maps , arrays, etc but it is possible if somewhat unusual.

@geseq
Copy link
Collaborator Author

geseq commented Jul 20, 2022

I would want to construct benchmarks before I do that, and at the moment my use case doesn't really require it.

@geseq
Copy link
Collaborator Author

geseq commented Jul 20, 2022

I'm happy for you to look into it and create a PR if you're interested.

@beoran
Copy link

beoran commented Jul 20, 2022

Well, my main interest here is consistency. Now Tengo uses pointer semantics for v2 everywhere and that is easy to remember. Having a mix of pointer and value semantics is more confusing and less straightforward to use

@geseq
Copy link
Collaborator Author

geseq commented Jul 20, 2022

Most languages have different semantics between primitive and Object types so that shouldn't be a concern.

I wouldn't mind implementing value semantics for Bool creating value semantics for the primitive types and leaving the pointer semantics well alone for the rest for now.

@geseq
Copy link
Collaborator Author

geseq commented Jul 20, 2022

Again, I do agree with you that in the view of backwards compatibility this shouldn't go into v2, but the performance gains are fairly clear so I would think it's a worthwhile addition.

@d5
Copy link
Owner

d5 commented Jul 20, 2022

curious. why couldn't you make bool value sematics?

@geseq
Copy link
Collaborator Author

geseq commented Jul 20, 2022

Sorry, I just didn't think of it at first. As I mentioned a couple of posts above, i wouldn't mind doing Bools. Working on that now.

It would carve out primitive types as having value semantics.

@d5
Copy link
Owner

d5 commented Jul 20, 2022

Yeah. I didn't look into all the details yet, but I like the idea of having value- and pointer- semantics for different types. It was long time ago, but I'm pretty sure I tried value-sematics when I was working on the first version for an obvious performance reason. Then I had to decide on pointers for a couple of reasons that I don't remember, unfortunately. Maybe I failed because I used value sematics for all types. Not sure.

@geseq
Copy link
Collaborator Author

geseq commented Jul 20, 2022

updated with bools.

I wouldn't be entirely surprised if value semantics for non-primitive types did worsen the performance.

I'm happy to look into it whenever there is time (which there rarely is tbh), but for now I think this offers an improvement that's worth adding.

@d5
Copy link
Owner

d5 commented Jul 20, 2022

Also I would suggest that we create v3 branch so we can put some other changes we need to upgrade the version - documentation, maybe some settings, etc. We can change this PR to base off of v3 branch. How does that sound?

@d5
Copy link
Owner

d5 commented Jul 20, 2022

and maybe update the performance benchmark in README too? :)

@geseq geseq changed the base branch from master to v3 July 20, 2022 15:30
@beoran
Copy link

beoran commented Jul 20, 2022

Fair enough to use pointer semantics for arrays and maps, but in Go strings also have value semantics. I wonder if that would also work in Tengo?

@geseq geseq added the v3 label Mar 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants