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

the switch object is slow because of recursion #3058

Open
maxonfjvipon opened this issue Apr 6, 2024 · 2 comments · Fixed by #3103
Open

the switch object is slow because of recursion #3058

maxonfjvipon opened this issue Apr 6, 2024 · 2 comments · Fixed by #3103

Comments

@maxonfjvipon
Copy link
Member

Current implementation of switch object uses tuple.at object which is convenient for retrieval object by index but quite heavy.
Instead we can implement some recursive algorithm that can just do cases.head and cases.tail instead. Something similar to #3048 but in EO

levBagryansky added a commit to levBagryansky/eo that referenced this issue Apr 17, 2024
levBagryansky added a commit to levBagryansky/eo that referenced this issue Apr 17, 2024
levBagryansky added a commit to levBagryansky/eo that referenced this issue Apr 18, 2024
levBagryansky added a commit to levBagryansky/eo that referenced this issue Apr 26, 2024
levBagryansky added a commit to levBagryansky/eo that referenced this issue Apr 26, 2024
levBagryansky added a commit to levBagryansky/eo that referenced this issue Apr 26, 2024
levBagryansky added a commit to levBagryansky/eo that referenced this issue Apr 26, 2024
levBagryansky added a commit to levBagryansky/eo that referenced this issue Apr 26, 2024
levBagryansky added a commit to levBagryansky/eo that referenced this issue Apr 26, 2024
levBagryansky added a commit to levBagryansky/eo that referenced this issue Apr 26, 2024
levBagryansky added a commit to levBagryansky/eo that referenced this issue Apr 26, 2024
levBagryansky added a commit to levBagryansky/eo that referenced this issue Apr 27, 2024
levBagryansky added a commit to levBagryansky/eo that referenced this issue Apr 28, 2024
levBagryansky added a commit to levBagryansky/eo that referenced this issue Apr 28, 2024
levBagryansky added a commit to levBagryansky/eo that referenced this issue Apr 28, 2024
levBagryansky added a commit to levBagryansky/eo that referenced this issue Apr 28, 2024
levBagryansky added a commit to levBagryansky/eo that referenced this issue Apr 28, 2024
levBagryansky added a commit to levBagryansky/eo that referenced this issue Apr 28, 2024
levBagryansky added a commit to levBagryansky/eo that referenced this issue Apr 28, 2024
levBagryansky added a commit to levBagryansky/eo that referenced this issue Apr 28, 2024
levBagryansky added a commit to levBagryansky/eo that referenced this issue Apr 28, 2024
levBagryansky added a commit to levBagryansky/eo that referenced this issue May 2, 2024
levBagryansky added a commit to levBagryansky/eo that referenced this issue May 2, 2024
@maxonfjvipon maxonfjvipon reopened this May 2, 2024
@l3r8yJ
Copy link
Contributor

l3r8yJ commented May 10, 2024

@maxonfjvipon can you help a bit, did I understand correctly how this implementation works?

[cases] > switch
  cases.length > len!

  # Take case by given index.
  # If case key is true, return case value.
  # Otherwise take next case.
  [index] > case-at
    if. > @
      index.eq ^.len
      true
      if.
        at.
          ^.cases.at index > case
          0
        case.at 1
        ^.case-at
          index.plus 1
  if. > @
    len.eq 0
    error "switch cases are empty"
    case-at 0

Here we're got an objectswitch. switch decorates encapsulates an array of tuples, where each of them consists of 2 objects. First object – the condition, the second one – something that we have to return if condition was true.

I'm not sure that I can say what happens here cases.length > len!, but I can suppose that something like "alias" for our object – switch, and later we can get access to this value via ^.len.

Object case-at decorates encapsulates index of all tuples. If index equal to length of cases – returns true, otherwise execute next if. Next if object says that if case(i.e. tuple) has true at 0 index then we need to return tuple at 1, otherwise recursively return an object case-at with incremented index?

@maxonfjvipon
Copy link
Member Author

@l3r8yJ you're right. But switch does not "decorates" tuple of tuples, it "encapsulates" it. The same story with recursive case-at

@objectionary objectionary deleted a comment from github-actions bot Oct 14, 2024
@yegor256 yegor256 changed the title We can speed up switch object the switch object is slow because of recursion Oct 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants