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).

!7

cprint FreezeStackDemo {
  transient bool shouldFreeze;
  
  start-thread {
    print(myFunc(5)); // Run normal version
    print(myFunc_hibernatable(5)); // Run hibernatable version - should be exactly as fast
    set shouldFreeze;
    try {
      myFunc_hibernatable(5); // Run hibernatable version and throw hibernation exception in the middle of it
    } catch Hib e {
      print("Frozen stack structure:");
      pnlStruct(e.stack);

      // Convert to virtual stack version which is a bit
      // slower than the regular one [need benchmark!],
      // finish the computation and print the result
      
      print("Reviving & resuming!");
      VStack stack = new Hib(restructure(e.stack)).asVStack();
      stepAllWith(stack, r {
        print(structWithReplacements(stack, "FreezeStackDemo$myFunc_vstack", "myFunc"));
      });
      print("Result: " + stack.latestResult);
    }
  }

  S myFunc(int n) {
    if (n >= 100) n = 100;
    if (n <= 0) ret "base";
    ret n + " " + myFunc(n-1);
  }

  S myFunc_hibernatable(int n) throws Hib {
    try {
      if (n >= 100) n = 100;
      if (n <= 0) if (shouldFreeze) hibernate(); else ret "base";
      ret n + " " + myFunc_hibernatable(n-1);
    } on fail Hib e {
      e.add(new myFunc_vstack(n));
    }
  }
  
  srecord myFunc_vstack(int n) implements VStack.Computable<S> {
    public void step(VStack stack, O subResult) {
      if (stack.hasSubResult())
        stack._return(n + " " + subResult);
      else {
        if (n >= 100) n = 100;
        if (n <= 0) ret with stack._return("base");
        stack.push(new myFunc_vstack(n-1));
      }
    }
  }
}

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: 217 / 377
Version history: 8 change(s)
Referenced in: