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

Speed up large FromConfigAdapter usages with alternative adapter id calculation #4138

Merged
merged 2 commits into from
Dec 14, 2023
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
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@
"@types/jest-image-snapshot": "^6.1.0",
"@types/jexl": "^2.3.0",
"@types/json-parse-better-errors": "^1.0.0",
"@types/json-stable-stringify": "^1.0.32",
"@types/lodash.merge": "^4.6.6",
"@types/node": "^14.14.0",
"@types/node-fetch": "^2.5.7",
Expand Down
20 changes: 10 additions & 10 deletions packages/core/assemblyManager/assembly.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { getParent, types, Instance, IAnyType } from 'mobx-state-tree'
import jsonStableStringify from 'json-stable-stringify'
import AbortablePromiseCache from 'abortable-promise-cache'

// locals
Expand All @@ -13,6 +12,9 @@ import PluginManager from '../PluginManager'
import { when, Region, Feature } from '../util'
import QuickLRU from '../util/QuickLRU'
import RpcManager from '../rpc/RpcManager'
import { adapterConfigCacheKey } from '../data_adapters/dataAdapterCache'

type AdapterConf = Record<string, unknown>

const refNameRegex = new RegExp(
'[0-9A-Za-z!#$%&+./:;?@^_|~-][0-9A-Za-z!#$%&*+./:;=?@^_|~-]*',
Expand Down Expand Up @@ -107,10 +109,6 @@ function checkRefName(refName: string) {
}
}

function getAdapterId(adapterConf: unknown) {
return jsonStableStringify(adapterConf)
}

type RefNameAliases = Record<string, string | undefined>

interface CacheData {
Expand Down Expand Up @@ -430,14 +428,13 @@ export default function assemblyFactory(
/**
* #method
*/
getAdapterMapEntry(adapterConf: unknown, options: BaseOptions) {
getAdapterMapEntry(adapterConf: AdapterConf, options: BaseOptions) {
const { signal, statusCallback, ...rest } = options
if (!options.sessionId) {
throw new Error('sessionId is required')
}
const adapterId = getAdapterId(adapterConf)
return adapterLoads.get(
adapterId,
adapterConfigCacheKey(adapterConf),
{
adapterConf,
self: self as Assembly,
Expand All @@ -455,7 +452,10 @@ export default function assemblyFactory(
* #method
* get Map of `canonical-name -> adapter-specific-name`
*/
async getRefNameMapForAdapter(adapterConf: unknown, opts: BaseOptions) {
async getRefNameMapForAdapter(
adapterConf: AdapterConf,
opts: BaseOptions,
) {
if (!opts?.sessionId) {
throw new Error('sessionId is required')
}
Expand All @@ -468,7 +468,7 @@ export default function assemblyFactory(
* get Map of `adapter-specific-name -> canonical-name`
*/
async getReverseRefNameMapForAdapter(
adapterConf: unknown,
adapterConf: AdapterConf,
opts: BaseOptions,
) {
const map = await this.getAdapterMapEntry(adapterConf, opts)
Expand Down
8 changes: 5 additions & 3 deletions packages/core/assemblyManager/assemblyManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import assemblyFactory, { Assembly } from './assembly'
import PluginManager from '../PluginManager'
import RpcManager from '../rpc/RpcManager'

type AdapterConf = Record<string, unknown>

/**
* #stateModel AssemblyManager
*/
Expand Down Expand Up @@ -105,7 +107,7 @@ function assemblyManagerFactory(conf: IAnyType, pm: PluginManager) {
await assembly.load()
await when(
() =>
Boolean(assembly?.regions && assembly.refNameAliases) ||
!!(assembly?.regions && assembly.refNameAliases) ||
!!assembly?.error,
)
if (assembly.error) {
Expand All @@ -118,7 +120,7 @@ function assemblyManagerFactory(conf: IAnyType, pm: PluginManager) {
* #method
*/
async getRefNameMapForAdapter(
adapterConf: unknown,
adapterConf: AdapterConf,
assemblyName: string | undefined,
opts: { signal?: AbortSignal; sessionId: string },
) {
Expand All @@ -133,7 +135,7 @@ function assemblyManagerFactory(conf: IAnyType, pm: PluginManager) {
* #method
*/
async getReverseRefNameMapForAdapter(
adapterConf: unknown,
adapterConf: AdapterConf,
assemblyName: string | undefined,
opts: { signal?: AbortSignal; sessionId: string },
) {
Expand Down
4 changes: 2 additions & 2 deletions packages/core/data_adapters/dataAdapterCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import idMaker from '../util/idMaker'

type ConfigSnap = SnapshotIn<AnyConfigurationSchemaType>

function adapterConfigCacheKey(adapterConfig: ConfigSnap) {
return `${idMaker(adapterConfig)}`
export function adapterConfigCacheKey(conf: Record<string, unknown> = {}) {
return `${idMaker(conf)}`
}

interface AdapterCacheEntry {
Expand Down
1 change: 0 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
"http-range-fetcher": "^2.0.0",
"is-object": "^1.0.1",
"jexl": "^2.3.0",
"json-stable-stringify": "^1.0.1",
"librpc-web-mod": "^1.1.5",
"load-script": "^2.0.0",
"material-ui-popup-state": "^5.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/core/util/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,7 @@ export async function renameRegionsIfNeeded<
assemblyName?: string
regions?: Region[]
signal?: AbortSignal
adapterConfig: unknown
adapterConfig: Record<string, unknown>
sessionId: string
statusCallback?: (arg: string) => void
},
Expand Down
58 changes: 24 additions & 34 deletions plugins/config/src/FromConfigAdapter/FromConfigAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,29 @@ import {
import PluginManager from '@jbrowse/core/PluginManager'
import { getSubAdapterType } from '@jbrowse/core/data_adapters/dataAdapterCache'

/**
* Adapter that just returns the features defined in its `features` configuration
* key, like:
* `"features": [ { "refName": "ctgA", "start":1, "end":20 }, ... ]`
*/
export function makeFeatures(fdata: SimpleFeatureSerialized[]) {
const features = new Map<string, Feature[]>()
for (const entry of fdata) {
if (entry) {
const f = new SimpleFeature(entry)
const refName = f.get('refName') as string
let bucket = features.get(refName)
if (!bucket) {
bucket = []
features.set(refName, bucket)
}

bucket.push(f)
}
}

// sort the features on each reference sequence by start coordinate
for (const refFeatures of features.values()) {
refFeatures.sort((a, b) => a.get('start') - b.get('start'))
}

return features
}

export default class FromConfigAdapter extends BaseFeatureDataAdapter {
protected features: Map<string, Feature[]>
Expand All @@ -28,35 +46,7 @@ export default class FromConfigAdapter extends BaseFeatureDataAdapter {
) {
super(conf, getSubAdapter, pluginManager)
const feats = readConfObject(conf, 'features') as SimpleFeatureSerialized[]
this.features = FromConfigAdapter.makeFeatures(feats || [])
}

static makeFeatures(fdata: SimpleFeatureSerialized[]) {
const features = new Map<string, Feature[]>()
for (const entry of fdata) {
if (entry) {
const f = this.makeFeature(entry)
const refName = f.get('refName') as string
let bucket = features.get(refName)
if (!bucket) {
bucket = []
features.set(refName, bucket)
}

bucket.push(f)
}
}

// sort the features on each reference sequence by start coordinate
for (const refFeatures of features.values()) {
refFeatures.sort((a, b) => a.get('start') - b.get('start'))
}

return features
}

static makeFeature(data: SimpleFeatureSerialized) {
return new SimpleFeature(data)
this.features = makeFeatures(feats || [])
}

async getRefNames() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import {
} from '@jbrowse/core/configuration'
import PluginManager from '@jbrowse/core/PluginManager'
import { getSubAdapterType } from '@jbrowse/core/data_adapters/dataAdapterCache'

import FromConfigAdapter from '../FromConfigAdapter/FromConfigAdapter'
import { makeFeatures } from '../FromConfigAdapter/FromConfigAdapter'

/**
* Adapter that just returns the features defined in its `features` configuration
Expand All @@ -32,11 +31,8 @@ export default class FromConfigRegionsAdapter
pluginManager?: PluginManager,
) {
super(config, getSubAdapter, pluginManager)
const features = readConfObject(
config,
'features',
) as SimpleFeatureSerialized[]
this.features = FromConfigAdapter.makeFeatures(features || [])
const f = readConfObject(config, 'features') as SimpleFeatureSerialized[]
this.features = makeFeatures(f || [])
}

/**
Expand Down
20 changes: 0 additions & 20 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4417,11 +4417,6 @@
resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.15.tgz#596a1747233694d50f6ad8a7869fcb6f56cf5841"
integrity sha512-5+fP8P8MFNC+AyZCDxrB2pkZFPGzqQWUzpSeuuVLvm8VMcorNYavBqoFcxK8bQz4Qsbn4oUEEem4wDLfcysGHA==

"@types/json-stable-stringify@^1.0.32":
version "1.0.36"
resolved "https://registry.yarnpkg.com/@types/json-stable-stringify/-/json-stable-stringify-1.0.36.tgz#fe6c6001a69ff8160a772da08779448a333c7ddd"
integrity sha512-b7bq23s4fgBB76n34m2b3RBf6M369B0Z9uRR8aHTMd8kZISRkmDEpPD8hhpYvDFzr3bJCPES96cm3Q6qRNDbQw==

"@types/keyv@^3.1.4":
version "3.1.4"
resolved "https://registry.yarnpkg.com/@types/keyv/-/keyv-3.1.4.tgz#3ccdb1c6751b0c7e52300bcdacd5bcbf8faa75b6"
Expand Down Expand Up @@ -11538,16 +11533,6 @@ json-stable-stringify-without-jsonify@^1.0.1:
resolved "https://registry.yarnpkg.com/json-stable-stringify-without-jsonify/-/json-stable-stringify-without-jsonify-1.0.1.tgz#9db7b59496ad3f3cfef30a75142d2d930ad72651"
integrity sha512-Bdboy+l7tA3OGW6FjyFHWkP5LuByj1Tk33Ljyq0axyzdk9//JSi2u3fP1QSmd1KNwq6VOKYGlAu87CisVir6Pw==

json-stable-stringify@^1.0.1:
version "1.1.0"
resolved "https://registry.yarnpkg.com/json-stable-stringify/-/json-stable-stringify-1.1.0.tgz#43d39c7c8da34bfaf785a61a56808b0def9f747d"
integrity sha512-zfA+5SuwYN2VWqN1/5HZaDzQKLJHaBVMZIIM+wuYjdptkaQsqzDdqjqf+lZZJUuJq1aanHiY8LhH8LmH+qBYJA==
dependencies:
call-bind "^1.0.5"
isarray "^2.0.5"
jsonify "^0.0.1"
object-keys "^1.1.1"

json-stringify-nice@^1.1.4:
version "1.1.4"
resolved "https://registry.yarnpkg.com/json-stringify-nice/-/json-stringify-nice-1.1.4.tgz#2c937962b80181d3f317dd39aa323e14f5a60a67"
Expand Down Expand Up @@ -11584,11 +11569,6 @@ jsonfile@^6.0.1:
optionalDependencies:
graceful-fs "^4.1.6"

jsonify@^0.0.1:
version "0.0.1"
resolved "https://registry.yarnpkg.com/jsonify/-/jsonify-0.0.1.tgz#2aa3111dae3d34a0f151c63f3a45d995d9420978"
integrity sha512-2/Ki0GcmuqSrgFyelQq9M05y7PS0mEwuIzrf3f1fPqkVDVRvZrPZtVSMHxdgo8Aq0sxAOb/cr2aqqA3LeWHVPg==

jsonparse@^1.2.0, jsonparse@^1.3.1:
version "1.3.1"
resolved "https://registry.yarnpkg.com/jsonparse/-/jsonparse-1.3.1.tgz#3f4dae4a91fac315f71062f8521cc239f1366280"
Expand Down
Loading