Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

56
LINES

< > BotCompany Repo | #1030481 // Hibernate & Restore Stack Demo [shortened]

JavaX source code (Dynamic Module) [tags: use-pretranspiled] - run with: Stefan's OS

Uses 1113K of libraries. Click here for Pure Java version (11313L/55K).

1  
!7
2  
3  
cprint FreezeStackDemo {
4  
  transient bool shouldFreeze;
5  
  
6  
  start-thread {
7  
    print(myFunc(5)); // Run normal version
8  
    print(myFunc_hibernatable(5)); // Run hibernatable version - should be exactly as fast
9  
    set shouldFreeze;
10  
    try {
11  
      myFunc_hibernatable(5); // Run hibernatable version and throw hibernation exception in the middle of it
12  
    } catch Hib e {
13  
      print("Frozen stack structure:");
14  
      pnlStruct(e.stack);
15  
16  
      // Convert to virtual stack version which is a bit
17  
      // slower than the regular one [need benchmark!],
18  
      // finish the computation and print the result
19  
      
20  
      print("Reviving & resuming!");
21  
      VStack stack = new Hib(restructure(e.stack)).asVStack();
22  
      stepAllWith(stack, r {
23  
        print(structWithReplacements(stack, "FreezeStackDemo$myFunc_vstack", "myFunc"));
24  
      });
25  
      print("Result: " + stack.latestResult);
26  
    }
27  
  }
28  
29  
  S myFunc(int n) {
30  
    if (n >= 100) n = 100;
31  
    if (n <= 0) ret "base";
32  
    ret n + " " + myFunc(n-1);
33  
  }
34  
35  
  S myFunc_hibernatable(int n) throws Hib {
36  
    try {
37  
      if (n >= 100) n = 100;
38  
      if (n <= 0) if (shouldFreeze) hibernate(); else ret "base";
39  
      ret n + " " + myFunc_hibernatable(n-1);
40  
    } on fail Hib e {
41  
      e.add(new myFunc_vstack(n));
42  
    }
43  
  }
44  
  
45  
  srecord myFunc_vstack(int n) implements VStack.Computable<S> {
46  
    public void step(VStack stack, O subResult) {
47  
      if (stack.hasSubResult())
48  
        stack._return(n + " " + subResult);
49  
      else {
50  
        if (n >= 100) n = 100;
51  
        if (n <= 0) ret with stack._return("base");
52  
        stack.push(new myFunc_vstack(n-1));
53  
      }
54  
    }
55  
  }
56  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, vouqrxazstgt

No comments. add comment

Snippet ID: #1030481
Snippet name: Hibernate & Restore Stack Demo [shortened]
Eternal ID of this version: #1030481/9
Text MD5: fa95abe5fff9d064da1398d1ea004119
Transpilation MD5: cd099b6ea1715c05a158747a921d0ccf
Author: stefan
Category:
Type: JavaX source code (Dynamic Module)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-01-02 00:09:40
Source code size: 1683 bytes / 56 lines
Pitched / IR pitched: No / No
Views / Downloads: 152 / 294
Version history: 8 change(s)
Referenced in: [show references]