Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor refactoring #2468

Merged
merged 1 commit into from
Sep 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading