Skip to content

Commit

Permalink
chore: use for … of + if instead of arrow func chaining
Browse files Browse the repository at this point in the history
[cap/dev] etc/perf.js

for (each of keys) if ...                  196 ms
for (each of keys) || ...                  188 ms
keys.filter.forEach (...)                  323 ms
  • Loading branch information
patricebender committed Sep 12, 2024
1 parent d25af70 commit 6525aca
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 31 deletions.
42 changes: 18 additions & 24 deletions db-service/lib/cqn4sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,15 +132,12 @@ function cqn4sql(originalQuery, model) {
const queryTarget = Object.values(inferred.sources)[0].definition
const keys = Object.values(queryTarget.elements).filter(e => e.key === true)
const primaryKey = { list: [] }
keys
.filter(k => !k.virtual) // e.g. draft column `isActiveEntity` is virtual and key
.forEach(k => {
// cqn4sql will add the table alias to the column later, no need to add it here
subquery.SELECT.columns.push({ ref: [k.name] })

// add the alias of the main query to the list of primary key references
primaryKey.list.push({ ref: [transformedFrom.as, k.name] })
})
for (const k of keys) {
if (!k.virtual) {
subquery.SELECT.columns.push({ ref: [k.name] })
primaryKey.list.push({ ref: [transformedFrom.as, k.name] })
}
}

const transformedSubquery = cqn4sql(subquery, model)

Expand Down Expand Up @@ -1069,13 +1066,12 @@ function cqn4sql(originalQuery, model) {
*/
function getColumnsForWildcard(exclude = [], replace = [], baseName = null) {
const wildcardColumns = []
Object.keys(inferred.$combinedElements)
.filter(k => !exclude.includes(k))
.forEach(k => {
for (const k of Object.keys(inferred.$combinedElements)) {
if (!exclude.includes(k)) {
const { index, tableAlias } = inferred.$combinedElements[k][0]
const element = tableAlias.elements[k]
// ignore FK for odata csn / ignore blobs from wildcard expansion
if (isManagedAssocInFlatMode(element) || element.type === 'cds.LargeBinary') return
if (isManagedAssocInFlatMode(element) || element.type === 'cds.LargeBinary') continue
// for wildcard on subquery in from, just reference the elements
if (tableAlias.SELECT && !element.elements && !element.target) {
wildcardColumns.push(index ? { ref: [index, k] } : { ref: [k] })
Expand All @@ -1091,7 +1087,8 @@ function cqn4sql(originalQuery, model) {
)
wildcardColumns.push(...flatColumns)
}
})
}
}
return wildcardColumns

/**
Expand Down Expand Up @@ -1421,12 +1418,12 @@ function cqn4sql(originalQuery, model) {
const keys = def.keys // use key aspect on entity
const keyValComparisons = []
const flatKeys = []
Object.values(keys)
// up__ID already part of inner where exists, no need to add it explicitly here
.filter(k => k !== backlinkFor($baseLink.definition)?.[0])
.forEach(v => {
for (const v of Object.values(keys)) {
if (v !== backlinkFor($baseLink.definition)?.[0]) {
// up__ID already part of inner where exists, no need to add it explicitly here
flatKeys.push(...getFlatColumnsFor(v, { tableAlias: $baseLink.alias }))
})
}
}
if (flatKeys.length > 1)
throw new Error('Filters can only be applied to managed associations which result in a single foreign key')
flatKeys.forEach(c => keyValComparisons.push([...[c, '=', token]]))
Expand Down Expand Up @@ -1586,10 +1583,7 @@ function cqn4sql(originalQuery, model) {
if (!def.$refLinks) return def
const leaf = def.$refLinks[def.$refLinks.length - 1]
const first = def.$refLinks[0]
const tableAlias = getTableAlias(
def,
def.ref.length > 1 && first.definition.isAssociation ? first : $baseLink,
)
const tableAlias = getTableAlias(def, def.ref.length > 1 && first.definition.isAssociation ? first : $baseLink)
if (leaf.definition.parent.kind !== 'entity')
// we need the base name
return getFlatColumnsFor(leaf.definition, {
Expand Down Expand Up @@ -1855,7 +1849,7 @@ function cqn4sql(originalQuery, model) {
result[i] = asXpr(xpr)
continue
}
if(lhs.args) {
if (lhs.args) {
const args = calculateOnCondition(lhs.args)
result[i] = { ...lhs, args }
continue
Expand Down
18 changes: 11 additions & 7 deletions db-service/lib/infer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,8 @@ function infer(originalQuery, model) {
if (list) list.forEach(arg => attachRefLinksToArg(arg, $baseLink, expandOrExists))
if (!ref) return
init$refLinks(arg)
ref.forEach((step, i) => {
let i = 0
for (const step of ref) {
const id = step.id || step
if (i === 0) {
// infix filter never have table alias
Expand Down Expand Up @@ -231,7 +232,8 @@ function infer(originalQuery, model) {
step.where.forEach(walkTokenStream)
} else throw new Error('A filter can only be provided when navigating along associations')
}
})
i += 1
}
const { definition, target } = arg.$refLinks[arg.$refLinks.length - 1]
if (definition.value) {
// nested calculated element
Expand Down Expand Up @@ -1046,15 +1048,17 @@ function infer(originalQuery, model) {
if (Object.keys(queryElements).length === 0 && aliases.length === 1) {
const { elements } = getDefinitionFromSources(sources, aliases[0])
// only one query source and no overwritten columns
Object.keys(elements)
.filter(k => !exclude(k))
.forEach(k => {
for (const k of Object.keys(elements)) {
if (!exclude(k)) {
const element = elements[k]
if (element.type !== 'cds.LargeBinary') queryElements[k] = element
if (element.type !== 'cds.LargeBinary') {
queryElements[k] = element
}
if (element.value) {
linkCalculatedElement(element)
}
})
}
}
return
}

Expand Down

0 comments on commit 6525aca

Please sign in to comment.