Skip to content

Commit

Permalink
try to improve
Browse files Browse the repository at this point in the history
  • Loading branch information
ssalbdivad committed Jan 16, 2025
1 parent fded04c commit 781e7fc
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 59 deletions.
16 changes: 3 additions & 13 deletions ark/schema/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export abstract class BaseNode<
: pipedFromCtx.data
}

const ctx = new Traversal(data, this.$.resolvedConfig)
const ctx = new Traversal(data, this.$.configSnapshot.resolved)
this.traverseApply(data, ctx)
return ctx.finalize()
},
Expand All @@ -97,16 +97,6 @@ export abstract class BaseNode<
this.$ = $
}

toSchema(): NormalizedSchema<d["kind"]> {
const result: NormalizedSchema<d["kind"]> = { ...this.inner }
if (this.hasNonEmptyMeta()) (result as any).meta = this.meta
return result
}

hasNonEmptyMeta(): boolean {
return "meta" in this.json
}

withMeta(
meta: ArkEnv.meta | ((currentMeta: ArkEnv.meta) => ArkEnv.meta)
): this {
Expand Down Expand Up @@ -154,7 +144,7 @@ export abstract class BaseNode<
return this.cacheGetter(
"description",
this.meta?.description ??
this.resolvedConfig[this.kind].description(this as never)
this.configSnapshot.resolved[this.kind].description(this as never)
)
}

Expand Down Expand Up @@ -212,7 +202,7 @@ export abstract class BaseNode<
if (this.allowsRequiresContext) {
return this.traverseAllows(
data as never,
new Traversal(data, this.$.resolvedConfig)
new Traversal(data, this.$.configSnapshot.resolved)
)
}
return (this.traverseAllows as any)(data)
Expand Down
22 changes: 11 additions & 11 deletions ark/schema/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export const registerNodeId = (prefix: string): NodeId => {
export const parseNode = (ctx: NodeParseContext): BaseNode => {
const impl = nodeImplementationsByKind[ctx.kind]
const configuredSchema =
impl.applyConfig?.(ctx.def, ctx.$.resolvedConfig) ?? ctx.def
impl.applyConfig?.(ctx.def, ctx.$.configSnapshot.resolved) ?? ctx.def
const inner: dict = {}
const { meta: metaSchema, ...innerSchema } = configuredSchema as dict & {
meta?: MetaSchema
Expand Down Expand Up @@ -254,17 +254,17 @@ export const createNode = (
const collapsibleJson = possiblyCollapse(json, impl.collapsibleKey, true)
const hash = JSON.stringify({ kind, ...json })

const parseConfigProps = {
parseConfig: $.parseConfig,
parseConfigHash: $.parseConfigHash,
resolvedConfig: $.resolvedConfig
} satisfies Partial<UnknownAttachments>

const configSnapshot = $.configSnapshot
// we have to wait until after reduction to return a cached entry,
// since reduction can add impliedSiblings
if ($.nodesByHash[hash] && !ignoreCache)
// update to show the node reflects the latest parse config, if it has changed
return Object.assign($.nodesByHash[hash], parseConfigProps)
if ($.nodesByHash[hash] && !ignoreCache) {
const cached = $.nodesByHash[hash]
if (cached.configSnapshot.hash !== configSnapshot.hash)
// update to show the node reflects the latest parse config, if it has changed
(cached as any).config = configSnapshot

return cached
}

const attachments: UnknownAttachments & dict = {
id,
Expand All @@ -280,7 +280,7 @@ export const createNode = (
hash,
collapsibleJson: collapsibleJson as Json,
children,
...parseConfigProps
configSnapshot
}

for (const k in inner)
Expand Down
66 changes: 35 additions & 31 deletions ark/schema/scope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
type anyOrNever,
type array,
type conform,
type dict,
type flattenListable,
type listable,
type noSuggest
Expand Down Expand Up @@ -64,7 +65,11 @@ import { Alias } from "./roots/alias.ts"
import type { BaseRoot } from "./roots/root.ts"
import type { UnionNode } from "./roots/union.ts"
import { CompiledFunction, NodeCompiler } from "./shared/compile.ts"
import type { NodeKind, RootKind } from "./shared/implement.ts"
import type {
NodeKind,
ParseConfigSnapshot,
RootKind
} from "./shared/implement.ts"
import { $ark } from "./shared/registry.ts"
import type { TraverseAllows, TraverseApply } from "./shared/traversal.ts"
import { arkKind, hasArkKind, isNode } from "./shared/utils.ts"
Expand Down Expand Up @@ -227,33 +232,26 @@ export abstract class BaseScope<$ extends {} = {}> {
}

private _lastGlobalResolvedConfig: ArkConfig | undefined
private _parseConfig: ArkScopeConfig | undefined
private _parseConfigHash: string | undefined
private _resolvedConfig: ResolvedScopeConfig | undefined
private updateConfig(): void {
private _lastConfigSnapshot: ParseConfigSnapshot | undefined
get configSnapshot(): ParseConfigSnapshot {
// can't use $ark.config for this check since it gets mutated
if (this._lastGlobalResolvedConfig === $ark.resolvedConfig) return

this._parseConfig =
if (
this._lastConfigSnapshot &&
this._lastGlobalResolvedConfig === $ark.resolvedConfig
)
return this._lastConfigSnapshot
const configured =
this.config ? mergeConfigs($ark.config, this.config) : $ark.config
this._parseConfigHash = serializeConfig(this._parseConfig)
this._resolvedConfig = mergeConfigs($ark.defaultConfig, this._parseConfig)
this._lastGlobalResolvedConfig = $ark.resolvedConfig
}
const hash = serializeConfig(configured)
const resolved = mergeConfigs($ark.defaultConfig, configured)

get resolvedConfig(): ResolvedScopeConfig {
this.updateConfig()
return this._resolvedConfig!
}

get parseConfig(): ArkScopeConfig {
this.updateConfig()
return this._parseConfig!
}
this._lastGlobalResolvedConfig = $ark.resolvedConfig

get parseConfigHash(): string {
this.updateConfig()
return this._parseConfigHash!
return {
configured,
hash,
resolved
}
}

get internal(): this {
Expand Down Expand Up @@ -373,11 +371,11 @@ export abstract class BaseScope<$ extends {} = {}> {

if (isNode(reference)) {
bound =
reference.parseConfigHash === this.parseConfigHash ?
reference.configSnapshot.hash === this.configSnapshot.hash ?
reference.$ === this ?
reference
: new (reference.constructor as any)(reference.attachments, this)
: this.node(reference.kind, reference.toSchema())
: this.node(reference.kind, nodeToSchema(reference))
} else {
bound =
reference.$ === this ?
Expand Down Expand Up @@ -537,13 +535,13 @@ export abstract class BaseScope<$ extends {} = {}> {

this.lazyResolutions.forEach(node => node.resolution)

if (this.resolvedConfig.ambient === true)
if (this.configSnapshot.resolved.ambient === true)
// spread all exports to ambient
Object.assign($ark.ambient as {}, this._exports)
else if (typeof this.resolvedConfig.ambient === "string") {
else if (typeof this.configSnapshot.resolved.ambient === "string") {
// add exports as a subscope with the config value as a name
Object.assign($ark.ambient as {}, {
[this.resolvedConfig.ambient]: new RootModule({
[this.configSnapshot.resolved.ambient]: new RootModule({
...this._exports
})
})
Expand All @@ -555,7 +553,7 @@ export abstract class BaseScope<$ extends {} = {}> {
Object.assign(this.resolutions, this._exportedResolutions)

this.references = Object.values(this.referencesById)
if (!this.resolvedConfig.jitless) {
if (!this.configSnapshot.resolved.jitless) {
this.precompilation = writePrecompilation(this.references)
bindPrecompilation(this.references, this.precompilation)
}
Expand Down Expand Up @@ -625,7 +623,7 @@ export abstract class BaseScope<$ extends {} = {}> {

finalize<node extends BaseRoot>(node: node): node {
bootstrapAliasReferences(node)
if (!node.precompilation && !this.resolvedConfig.jitless)
if (!node.precompilation && !this.configSnapshot.resolved.jitless)
precompile(node.references)
return node
}
Expand Down Expand Up @@ -682,6 +680,12 @@ const bootstrapAliasReferences = (resolution: BaseRoot | GenericRoot) => {
return resolution
}

const nodeToSchema = (node: BaseNode): dict => {
const result: dict = { ...node.inner }
if (!isEmptyObject(node.meta)) result.meta = node.meta
return result
}

const resolutionsToJson = (resolutions: InternalResolutions): JsonStructure =>
flatMorph(resolutions, (k, v) => [
k,
Expand Down
10 changes: 7 additions & 3 deletions ark/schema/shared/implement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,12 @@ export type DescriptionWriter<kind extends NodeKind = NodeKind> = (
node: nodeOfKind<kind>
) => string

export interface ParseConfigSnapshot {
configured: ArkScopeConfig
hash: string
resolved: ResolvedScopeConfig
}

export interface UnknownAttachments {
alias?: string
readonly kind: NodeKind
Expand All @@ -387,9 +393,7 @@ export interface UnknownAttachments {
readonly collapsibleJson: Json
readonly children: BaseNode[]

readonly parseConfig: ArkScopeConfig
readonly parseConfigHash: string
readonly resolvedConfig: ResolvedScopeConfig
readonly configSnapshot: ParseConfigSnapshot
}

export interface NarrowedAttachments<d extends BaseNodeDeclaration>
Expand Down
2 changes: 1 addition & 1 deletion ark/type/parser/definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export const parseInnerDefinition = (

// include parseConfigHash in cacheId to ensure that if global config
// is updated, we reparse strings
const cacheId = `${ctx.$.id}:${ctx.$.parseConfigHash}`
const cacheId = `${ctx.$.id}:${ctx.$.configSnapshot.hash}`
const scopeCache = (parseCache[cacheId] ??= {})
const cachedResult = scopeCache[def]
if (cachedResult) {
Expand Down

0 comments on commit 781e7fc

Please sign in to comment.