Skip to content

Commit

Permalink
Minor refactoring (#2468)
Browse files Browse the repository at this point in the history
  • Loading branch information
lantua authored Sep 29, 2024
1 parent a1c49b9 commit 3a57092
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 36 deletions.
12 changes: 3 additions & 9 deletions libs/gi/ui/src/context/OptTargetContext.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { MainSubStatKey } from '@genshin-optimizer/gi/consts'
import { TeamCharacterContext, useOptConfig } from '@genshin-optimizer/gi/db-ui'
import type { OptNode } from '@genshin-optimizer/gi/wr'
import { precompute } from '@genshin-optimizer/gi/wr'
import { forEachNodes } from '@genshin-optimizer/gi/wr'
import type { ReactNode } from 'react'
import { createContext, useContext, useMemo } from 'react'
import { optimizeNodesForScaling } from '../util'
Expand Down Expand Up @@ -51,15 +51,9 @@ export function OptTargetWrapper({ children }: { children: ReactNode }) {

function getScalesWith(nodes: OptNode[]) {
const scalesWith = new Set<string>()
precompute(
forEachNodes(
nodes,
{},
(f) => {
const val = f.path[1]
scalesWith.add(val)
return val
},
1
(node) => node.operation === 'read' && scalesWith.add(node.path[1])
)
return scalesWith as Set<MainSubStatKey>
}
29 changes: 4 additions & 25 deletions libs/gi/ui/src/util/optimizeNodesForScaling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,7 @@ import {
} from '@genshin-optimizer/gi/consts'
import type { OptConfig } from '@genshin-optimizer/gi/db'
import type { NumNode } from '@genshin-optimizer/gi/wr'
import {
constant,
dynamicData,
mapFormulas,
mergeData,
optimize,
} from '@genshin-optimizer/gi/wr'
import { dynamicData, mergeData, optimize } from '@genshin-optimizer/gi/wr'
import type { TeamData } from '../type'
import { statFilterToNumNode } from './statFilterToNumNode'
export function optimizeNodesForScaling(
Expand All @@ -39,26 +33,11 @@ export function optimizeNodesForScaling(
...valueFilter.map((x) => x.value),
]

let nodes = optimize(
const nodes = optimize(
unoptimizedNodes,
workerData,
({ path: [p] }) => p !== 'dyn'
)
// Const fold read nodes
nodes = mapFormulas(
nodes,
(f) => {
if (f.operation === 'read' && f.path[0] === 'dyn') {
// const a = artSets[f.path[1]]
// if (a) return constant(a)
const stat = f.path[1]
if (!allMainSubStatKeys.includes(stat as MainSubStatKey))
return constant(0)
}
return f
},
(f) => f
({ path: [p, stat] }) =>
p !== 'dyn' || !allMainSubStatKeys.includes(stat as MainSubStatKey)
)
nodes = optimize(nodes, {}, (_) => false)
return { nodes, valueFilter }
}
2 changes: 1 addition & 1 deletion libs/gi/wr/src/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export function deepNodeClone<
export function forEachNodes<T extends Base<T> = AnyNode>(
formulas: T[],
topDown: (formula: T) => void,
bottomUp: (formula: T) => void
bottomUp: (formula: T) => void = () => {}
): void {
const visiting = new Set<T>(),
visited = new Set<T>()
Expand Down
2 changes: 1 addition & 1 deletion libs/gi/wr/src/optimization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ function deduplicate(formulas: OptNode[]): OptNode[] {

/**
* Replace nodes with known values with appropriate constants,
* avoiding removal of any nodes that pass `isFixed` predicate
* and remove read nodes where `shouldFold(node)` is `true`
*/
export function constantFold(
formulas: NumNode[],
Expand Down

0 comments on commit 3a57092

Please sign in to comment.