Skip to content

Commit

Permalink
Handle library (#793)
Browse files Browse the repository at this point in the history
  • Loading branch information
EagleoutIce committed May 11, 2024
2 parents 05d0f1b + 53de635 commit 109c97b
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/dataflow/environments/built-in.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import type { RFunctionArgument } from '../../r-bridge/lang-4.x/ast/model/nodes/
import type { RSymbol } from '../../r-bridge/lang-4.x/ast/model/nodes/r-symbol'
import type { NodeId } from '../../r-bridge/lang-4.x/ast/model/processing/node-id'
import { EdgeType } from '../graph/edge'
import { processLibrary } from '../internal/process/functions/call/built-in/built-in-library'

export const BuiltIn = 'built-in'

Expand Down Expand Up @@ -158,6 +159,7 @@ registerBuiltInFunctions(['[', '[['], processAccess,
registerBuiltInFunctions(['$', '@'], processAccess, { treatIndicesAsString: true }, )
registerBuiltInFunctions(['if'], processIfThenElse, {}, )
registerBuiltInFunctions(['get'], processGet, {}, )
registerBuiltInFunctions(['library'], processLibrary, {}, )
registerBuiltInFunctions(['<-', '='], processAssignment, { canBeReplacement: true }, )
registerBuiltInFunctions([':=', 'assign'], processAssignment, {}, )
registerBuiltInFunctions(['delayedAssign'], processAssignment, { quoteSource: true }, )
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { type DataflowProcessorInformation } from '../../../../../processor'
import type { DataflowInformation } from '../../../../../info'
import { processKnownFunctionCall } from '../known-call-handling'
import type { ParentInformation } from '../../../../../../r-bridge/lang-4.x/ast/model/processing/decorate'
import type { RFunctionArgument } from '../../../../../../r-bridge/lang-4.x/ast/model/nodes/r-function-call'
import type { RSymbol } from '../../../../../../r-bridge/lang-4.x/ast/model/nodes/r-symbol'
import type { NodeId } from '../../../../../../r-bridge/lang-4.x/ast/model/processing/node-id'
import { dataflowLogger } from '../../../../../logger'
import { unpackArgument } from '../argument/unpack-argument'
import type { RString } from '../../../../../../r-bridge/lang-4.x/ast/model/nodes/r-string'
import { RType } from '../../../../../../r-bridge/lang-4.x/ast/model/type'
import { wrapArgumentsUnnamed } from '../argument/make-argument'


export function processLibrary<OtherInfo>(
name: RSymbol<OtherInfo & ParentInformation>,
args: readonly RFunctionArgument<OtherInfo & ParentInformation>[],
rootId: NodeId,
data: DataflowProcessorInformation<OtherInfo & ParentInformation>
): DataflowInformation {
if(args.length !== 1) {
dataflowLogger.warn(`Currently only one-arg library-likes are allows (for ${name.content}), skipping`)
return processKnownFunctionCall({ name, args, rootId, data }).information
}
const nameToLoad = unpackArgument(args[0])
if(nameToLoad === undefined || nameToLoad.type !== RType.Symbol) {
dataflowLogger.warn('No library name provided, skipping')
return processKnownFunctionCall({ name, args, rootId, data }).information
}

// treat as a function call but convert the first argument to a string
const newArg: RString<OtherInfo & ParentInformation> = {
type: RType.String,
info: nameToLoad.info,
lexeme: nameToLoad.lexeme,
location: nameToLoad.location,
content: {
quotes: 'none',
str: nameToLoad.content
}
}

return processKnownFunctionCall({ name, args: wrapArgumentsUnnamed([newArg], data.completeAst.idMap), rootId, data }).information
}
6 changes: 5 additions & 1 deletion src/slicing/criterion/collect-all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { guard } from '../../util/assert'
import { getUniqueCombinationsOfSize } from '../../util/arrays'
import type { RNodeWithParent } from '../../r-bridge/lang-4.x/ast/model/processing/decorate'
import type { NodeId } from '../../r-bridge/lang-4.x/ast/model/processing/node-id'
import { EmptyArgument } from '../../r-bridge/lang-4.x/ast/model/nodes/r-function-call'

/**
* Defines the filter for collecting all possible slicing criteria.
Expand Down Expand Up @@ -49,6 +50,9 @@ export function* collectAllSlicingCriteria<OtherInfo>(ast: RNodeWithParent<Other
}

for(const combination of getUniqueCombinationsOfSize(potentialSlicingNodes, filter.minimumSize, filter.maximumSize)) {
yield combination.map(n => `$${n}` as SingleSlicingCriterion)
const c = combination.filter(n => n !== undefined && n !== EmptyArgument)
if(c.length > 0) {
yield c.map(n => `$${n}` as SingleSlicingCriterion)
}
}
}
4 changes: 1 addition & 3 deletions src/slicing/static/visiting-queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,17 @@ export class VisitingQueue {
*/
public add(target: NodeId, env: REnvironmentInformation, envFingerprint: string, onlyForSideEffects: boolean): void {
const idCounter = this.idThreshold.get(target) ?? 0

if(idCounter > this.threshold) {
slicerLogger.warn(`id: ${target} has been visited ${idCounter} times, skipping`)
this.timesHitThreshold++
return
} else {
this.idThreshold.set(target, idCounter + 1)
}

/* we do not include the in call part in the fingerprint as it is 'deterministic' from the source position */
const print = fingerprint(target, envFingerprint, onlyForSideEffects)

if(!this.seen.has(print)) {
this.idThreshold.set(target, idCounter + 1)
this.seen.set(print, target)
this.queue.push({ id: target, baseEnvironment: env, onlyForSideEffects })
}
Expand Down
2 changes: 1 addition & 1 deletion test/functionality/benchmark/slicer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ cat(d)`
}, statInfo)
assert.deepStrictEqual(stats.dataflow, {
numberOfNodes: 23,
numberOfEdges: 37,
numberOfEdges: 38,
numberOfCalls: 9,
numberOfFunctionDefinitions: 0
}, statInfo)
Expand Down

2 comments on commit 109c97b

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"artificial" Benchmark Suite

Benchmark suite Current: 109c97b Previous: 1e5ddeb Ratio
Retrieve AST from R code 238.58655709090908 ms 305.50355490909095 ms 0.78
Normalize R AST 31.396694181818184 ms 33.04192136363636 ms 0.95
Produce dataflow information 57.500119318181824 ms 66.58585145454545 ms 0.86
Total per-file 1268.435217909091 ms 1512.547620909091 ms 0.84
Static slicing 1.2457829018017852 ms (1.0300278002694851) 1.3817132831241319 ms (1.27303105869458) 0.90
Reconstruct code 0.39786935534707357 ms (0.21208717073911798) 0.45135141022522257 ms (0.25927254974160097) 0.88
Total per-slice 1.6628788016526317 ms (1.086112033950825) 1.8475447304133532 ms (1.3326721674469058) 0.90
failed to reconstruct/re-parse 0 # 0 # 1
times hit threshold 0 # 0 # 1
reduction (characters) 0.797431685913541 # 0.7329390759026897 # 1.09
reduction (normalized tokens) 0.7740577588998524 # 0.7209834969577295 # 1.07

This comment was automatically generated by workflow using github-action-benchmark.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"social-science" Benchmark Suite

Benchmark suite Current: 109c97b Previous: 1e5ddeb Ratio
Retrieve AST from R code 238.08479186000002 ms 330.55613118 ms 0.72
Normalize R AST 32.145331219999996 ms 36.10837608 ms 0.89
Produce dataflow information 82.76708640000001 ms 188.6117503 ms 0.44
Total per-file 2698.0098440799998 ms 3858.78617088 ms 0.70
Static slicing 5.490282685186625 ms (10.19658663160487) 8.763488287471356 ms (15.142795018273485) 0.63
Reconstruct code 0.36974444329004974 ms (0.19980092020192466) 0.6295735167403027 ms (0.28826473087031307) 0.59
Total per-slice 5.869149850496005 ms (10.265754110897918) 9.403718790030862 ms (15.25727767695505) 0.62
failed to reconstruct/re-parse 2 # 7 # 0.29
times hit threshold 0 # 298 # 0
reduction (characters) 0.9241844105867956 # 0.8935817303062389 # 1.03
reduction (normalized tokens) 0.8924953600737399 # 0.8531248144961375 # 1.05

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.