11import type { Factorization , SequenceInterface } from './SequenceInterface'
22import simpleFactor from './simpleFactor'
33
4+ import { yieldExecution } from '@/shared/asynchronous'
45import { math , CachingError } from '@/shared/math'
56import type { ExtendedBigint } from '@/shared/math'
67import { Paramable , paramClone } from '@/shared/Paramable'
@@ -151,7 +152,7 @@ export function Cached<PD extends GenericParamDescription>(desc: PD) {
151152 firstValueCached : ExtendedBigint = math . posInfinity
152153 lastFactorCached = - 1024n // dummy value
153154 firstFactorCached : ExtendedBigint = math . posInfinity
154- valueCachingPromise = Promise . resolve ( )
155+ valueCachingPromise : Promise < void > | undefined = undefined
155156 factorCachingPromise = Promise . resolve ( )
156157
157158 /**
@@ -256,6 +257,7 @@ export function Cached<PD extends GenericParamDescription>(desc: PD) {
256257 async fillValueCache ( n : bigint ) {
257258 const start = this . lastValueCached + 1n
258259 for ( let i = start ; i <= n ; i ++ ) {
260+ if ( i % 10000n === 0n ) await yieldExecution ( )
259261 const key = i . toString ( )
260262 // trust values we find; hopefully we have cleared when
261263 // needed, so that we can presume they are from some
@@ -269,16 +271,13 @@ export function Cached<PD extends GenericParamDescription>(desc: PD) {
269271 }
270272
271273 async cacheValues ( n : bigint ) {
272- // Let any existing value caching complete
273- await this . valueCachingPromise
274- // Let any pending parameter changes complete
275274 if ( this . parChangePromise ) await this . parChangePromise
275+ if ( this . valueCachingPromise ) await this . valueCachingPromise
276276 if ( n > this . lastValueCached ) {
277277 this . valueCachingPromise = this . fillValueCache ( n )
278278 await this . valueCachingPromise
279- } else {
280- this . valueCachingPromise = Promise . resolve ( )
281279 }
280+ this . valueCachingPromise = undefined
282281 }
283282
284283 getElement ( n : bigint ) : bigint {
@@ -299,6 +298,8 @@ export function Cached<PD extends GenericParamDescription>(desc: PD) {
299298 await this . cacheValues ( n )
300299 const start = this . lastFactorCached + 1n
301300 for ( let i = start ; i <= n ; ++ i ) {
301+ // Can we yield execution?
302+ if ( i % 10000n === 0n ) await yieldExecution ( )
302303 const key = i . toString ( )
303304 // trust values we find
304305 if ( ! ( key in this . factorCache ) ) {
@@ -393,10 +394,13 @@ export function Cached<PD extends GenericParamDescription>(desc: PD) {
393394 // interval from the _previous_ value of `this.first`
394395 // to `this.lastValueCached` is full.
395396 if (
396- this . first < this . firstValueCached
397- || this . first > this . lastValueCached + 1n
397+ ! needsReset
398+ && ( this . first < this . firstValueCached
399+ || this . first > this . lastValueCached + 1n )
398400 ) {
399- await this . valueCachingPromise
401+ if ( this . valueCachingPromise ) {
402+ await this . valueCachingPromise
403+ }
400404 this . firstValueCached = math . posInfinity
401405 this . lastValueCached = this . first - 1n
402406 // Get the new cache rolling:
@@ -406,8 +410,9 @@ export function Cached<PD extends GenericParamDescription>(desc: PD) {
406410 // kick off the factoring process because we don't
407411 // even know if this sequence is being factored.
408412 if (
409- this . first < this . firstFactorCached
410- || this . first > this . lastFactorCached + 1n
413+ ! needsReset
414+ && ( this . first < this . firstFactorCached
415+ || this . first > this . lastFactorCached + 1n )
411416 ) {
412417 await this . factorCachingPromise
413418 this . firstFactorCached = math . posInfinity
@@ -441,7 +446,9 @@ export function Cached<PD extends GenericParamDescription>(desc: PD) {
441446 }
442447 if ( needsReset ) {
443448 this . ready = false
444- await this . valueCachingPromise
449+ if ( this . valueCachingPromise ) {
450+ await this . valueCachingPromise
451+ }
445452 await this . factorCachingPromise
446453 this . initialize ( )
447454 }
0 commit comments