@@ -41,6 +41,20 @@ const TtlCache = ({ setTimeout, clearTimeout }) => {
4141 return cache
4242}
4343
44+ const cyrb53 = ( str , seed = 0 ) => {
45+ let h1 = 0xdeadbeef ^ seed ; let h2 = 0x41c6ce57 ^ seed
46+ for ( let i = 0 , ch ; i < str . length ; i ++ ) {
47+ ch = str . charCodeAt ( i )
48+ h1 = Math . imul ( h1 ^ ch , 2654435761 )
49+ h2 = Math . imul ( h2 ^ ch , 1597334677 )
50+ }
51+ h1 = Math . imul ( h1 ^ ( h1 >>> 16 ) , 2246822507 )
52+ h1 ^= Math . imul ( h2 ^ ( h2 >>> 13 ) , 3266489909 )
53+ h2 = Math . imul ( h2 ^ ( h2 >>> 16 ) , 2246822507 )
54+ h2 ^= Math . imul ( h1 ^ ( h1 >>> 13 ) , 3266489909 )
55+ return 4294967296 * ( 2097151 & h2 ) + ( h1 >>> 0 )
56+ }
57+
4458/**
4559 * @typedef Env
4660 *
@@ -58,6 +72,7 @@ const TtlCache = ({ setTimeout, clearTimeout }) => {
5872 */
5973export function dryRunWith ( env ) {
6074 const DRY_RUN_DEFAULT_MAX_PROCESS_AGE = env . DRY_RUN_DEFAULT_MAX_PROCESS_AGE
75+ const DRY_RUN_RESULT_MAX_AGE = env . DRY_RUN_RESULT_MAX_AGE
6176 const DRY_RUN_PROCESS_CACHE_TTL = env . DRY_RUN_PROCESS_CACHE_TTL
6277 const logger = env . logger
6378 const loadMessageMeta = loadMessageMetaWith ( env )
@@ -73,6 +88,7 @@ export function dryRunWith (env) {
7388 } )
7489
7590 const readStateCache = TtlCache ( env )
91+ const dryRunResultCache = TtlCache ( env )
7692
7793 function loadMessageCtx ( { messageTxId, processId } ) {
7894 /**
@@ -159,6 +175,16 @@ export function dryRunWith (env) {
159175 }
160176
161177 return ( { processId, messageTxId, maxProcessAge = DRY_RUN_DEFAULT_MAX_PROCESS_AGE , dryRun } ) => {
178+ const dryRunHash = cyrb53 ( JSON . stringify ( dryRun ) )
179+ const cached = dryRunResultCache . get ( dryRunHash )
180+ if ( cached && new Date ( ) . getTime ( ) - cached . age <= DRY_RUN_RESULT_MAX_AGE ) {
181+ logger . debug (
182+ 'Using recently cached dry-run result for dry-run to process "%s"' ,
183+ processId
184+ )
185+ return Resolved ( cached . ctx )
186+ }
187+
162188 return of ( { processId, messageTxId } )
163189 . chain ( loadMessageCtx )
164190 . chain ( ensureProcessLoaded ( { maxProcessAge } ) )
@@ -222,6 +248,11 @@ export function dryRunWith (env) {
222248 */
223249 return evaluate ( { ...ctx , dryRun : true , messages : Readable . from ( dryRunMessage ( ) ) } )
224250 } )
225- . map ( ( res ) => omit ( [ 'Memory' ] , res . output ) )
251+ . map ( ( res ) => {
252+ const omitted = omit ( [ 'Memory' ] , res . output )
253+ const cached = { age : new Date ( ) . getTime ( ) , ctx : omitted }
254+ dryRunResultCache . set ( dryRunHash , cached , DRY_RUN_RESULT_MAX_AGE )
255+ return omitted
256+ } )
226257 }
227258}
0 commit comments