Skip to content

Commit

Permalink
feat: add check in resolveAllStates, optimizing when no changes are d…
Browse files Browse the repository at this point in the history
…etected at all.
  • Loading branch information
nytamin committed Feb 1, 2020
1 parent ac35795 commit 6d02009
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 11 deletions.
6 changes: 4 additions & 2 deletions scratch/testWebPerformance.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@
<body>
<button onclick="test()">test</button>
<script>
const cache = {}
function test () {
const startTime = Date.now()
const options = {
time: 0
time: 0,
cache
}
const resolvedTimeline = SuperTimeline.Resolver.resolveTimeline(timeline, options)

SuperTimeline.Resolver.resolveAllStates(resolvedTimeline)
SuperTimeline.Resolver.resolveAllStates(resolvedTimeline, cache)
const time0 = Date.now() - startTime
console.log(`Time of execution: ${time0}`)
}
Expand Down
2 changes: 2 additions & 0 deletions src/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,4 +226,6 @@ export interface ResolverCacheInternal {

resolvedTimeline: ResolvedTimeline
hasOldData?: boolean

resolvedStates?: ResolvedStates
}
11 changes: 4 additions & 7 deletions src/resolver/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import {
Cap,
ResolvedStates,
ResolverCacheInternal,
ResolvedTimelineObjects
ResolvedTimelineObjects,
ResolverCache
} from '../api/api'
import {
extendMandadory,
Expand Down Expand Up @@ -177,7 +178,6 @@ export class Resolver {
_.each(resolvedTimeline.objects, obj => {
validObjects[obj.id] = obj
})

/** All references that depend on another reference (ie objects, classs or layers): */
const affectReferenceMap: {[ref: string]: string[]} = {}

Expand Down Expand Up @@ -215,11 +215,9 @@ export class Resolver {
if (!affectReferenceMap[ref]) affectReferenceMap[ref] = []
affectReferenceMap[ref].push('#' + obj.id)
}
} else {
}
}
})

// Invalidate all changed objects, and recursively invalidate all objects that reference those objects:
const handledReferences: {[ref: string]: true} = {}
const invalidateObjectsWithReference = (
Expand Down Expand Up @@ -254,7 +252,6 @@ export class Resolver {
_.each(Object.keys(changedReferences), reference => {
invalidateObjectsWithReference(reference, affectReferenceMap, validObjects)
})

// The objects that are left in validObjects at this point are still valid.
// We can reuse the old resolving for those:
_.each(validObjects, (obj: ResolvedTimelineObject) => {
Expand Down Expand Up @@ -291,8 +288,8 @@ export class Resolver {
}
}
/** Calculate the state for all points in time. */
static resolveAllStates (resolvedTimeline: ResolvedTimeline): ResolvedStates {
return resolveStates(resolvedTimeline)
static resolveAllStates (resolvedTimeline: ResolvedTimeline, cache?: ResolverCache): ResolvedStates {
return resolveStates(resolvedTimeline, undefined, cache)
}
/**
* Calculate the state at a given point in time.
Expand Down
19 changes: 17 additions & 2 deletions src/resolver/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import {
TimeEvent,
ResolvedStates,
ResolvedTimelineObjectInstanceKeyframe,
NextEvent
NextEvent,
ResolverCache
} from '../api/api'
import * as _ from 'underscore'
import { addObjectToResolvedTimeline } from './common'
Expand Down Expand Up @@ -39,7 +40,7 @@ export function getState (resolved: ResolvedTimeline | ResolvedStates, time: Tim

return state
}
export function resolveStates (resolved: ResolvedTimeline, onlyForTime?: Time): ResolvedStates {
export function resolveStates (resolved: ResolvedTimeline, onlyForTime?: Time, cache?: ResolverCache): ResolvedStates {
const resolvedStates: ResolvedStates = {
options: resolved.options,
statistics: resolved.statistics,
Expand All @@ -53,6 +54,16 @@ export function resolveStates (resolved: ResolvedTimeline, onlyForTime?: Time):
nextEvents: []
}

if (
cache &&
!onlyForTime &&
resolved.statistics.resolvingCount === 0 &&
cache.resolvedStates
) {
// Nothing has changed since last time, just return the states right away:
return cache.resolvedStates
}

const resolvedObjects = _.values(resolved.objects)
// Sort to make sure parent groups are evaluated before their children:
resolvedObjects.sort((a, b) => {
Expand Down Expand Up @@ -483,6 +494,10 @@ export function resolveStates (resolved: ResolvedTimeline, onlyForTime?: Time):
return 0
})

if (cache && !onlyForTime) {
cache.resolvedStates = resolvedStates
}

return resolvedStates
}
export function applyKeyframeContent (parentContent: Content, keyframeContent: Content) {
Expand Down

0 comments on commit 6d02009

Please sign in to comment.