55import { MemoryBackend , VFS } from "../fs.ts" ;
66import { JVM , SnapshotError } from "../jvm.ts" ;
77import { HttpBackend , parsePack } from "./http.ts" ;
8- import { dropSnapshot , loadSnapshot , persist , saveSnapshot } from "./idb.ts" ;
9- import type { SnapshotImage } from "../snapshot.ts" ;
8+ import { dropSnapshot , dropWarmSnapshot , loadSnapshot , loadWarmSnapshot , persist , saveSnapshot , saveWarmSnapshot } from "./idb.ts" ;
9+ import type { SnapshotImage , WarmSnapshotImage } from "../snapshot.ts" ;
1010import type { LibraryError , LibraryReply } from "../library.ts" ;
1111import type { NativeReply } from "../bridge.ts" ;
1212import type { FromWorker , InitMessage , ToWorker } from "./protocol.ts" ;
@@ -113,6 +113,8 @@ declare const JAVA_WEB_BUILD: string;
113113// in IndexedDB for later loads. A VM changes the image it restores, so each
114114// gets a copy.
115115const snapshots = new Map < string , SnapshotImage > ( ) ;
116+ const warmSnapshots = new Map < string , WarmSnapshotImage > ( ) ;
117+ const droppedWarmSnapshots = new Set < string > ( ) ;
116118const keep = ( image : SnapshotImage ) => {
117119 snapshots . delete ( image . key ) ;
118120 snapshots . set ( image . key , image ) ;
@@ -143,6 +145,29 @@ const snapshotCache = {
143145 } ,
144146} ;
145147
148+ const warmSnapshotCache = {
149+ load : async ( key : string ) => {
150+ if ( droppedWarmSnapshots . has ( key ) ) return null ;
151+ const image = warmSnapshots . get ( key ) ?? await loadWarmSnapshot ( key ) ;
152+ if ( ! image ) return null ;
153+ warmSnapshots . delete ( key ) ;
154+ warmSnapshots . set ( key , image ) ;
155+ return structuredClone ( image ) ;
156+ } ,
157+ save : ( image : WarmSnapshotImage ) => {
158+ droppedWarmSnapshots . delete ( image . warmKey ) ;
159+ warmSnapshots . delete ( image . warmKey ) ;
160+ warmSnapshots . set ( image . warmKey , structuredClone ( image ) ) ;
161+ for ( const k of [ ...warmSnapshots . keys ( ) ] . slice ( 0 , - 2 ) ) warmSnapshots . delete ( k ) ;
162+ saveWarmSnapshot ( image ) ;
163+ } ,
164+ drop : ( key : string ) => {
165+ warmSnapshots . delete ( key ) ;
166+ droppedWarmSnapshots . add ( key ) ;
167+ dropWarmSnapshot ( key ) ;
168+ } ,
169+ } ;
170+
146171// A snapshot that cannot be restored is dropped, and a second VM boots.
147172function retried < T > ( start : ( ) => Promise < T > ) : Promise < T > {
148173 return start ( ) . catch ( ( e ) => {
@@ -152,9 +177,10 @@ function retried<T>(start: () => Promise<T>): Promise<T> {
152177 } ) ;
153178}
154179
155- function newVm ( classpath : string [ ] , vmId : number ) : JVM {
180+ function newVm ( classpath : string [ ] , vmId : number , library = false ) : JVM {
156181 return new JVM ( {
157182 snapshots : snapshotCache ,
183+ ...( library && config ! . warmSnapshot ? { warmSnapshot : true , warmSnapshots : warmSnapshotCache } : { } ) ,
158184 fs : vfs ! ,
159185 javaHome : "/jdk" ,
160186 classpath,
@@ -226,7 +252,7 @@ self.onmessage = (e: MessageEvent<ToWorker>) => {
226252 }
227253 case "library" : {
228254 reply ( m . id , retried ( ( ) => {
229- const vm = newVm ( m . classpath , m . lib ) ;
255+ const vm = newVm ( m . classpath , m . lib , true ) ;
230256 vms . set ( m . lib , vm ) ;
231257 return vm . startLibrary ( ) ;
232258 } ) . then ( ( ) => ( { value : undefined } ) ) ) ;
0 commit comments