Skip to content

Commit

Permalink
use temp file on server to handle large requests (#859)
Browse files Browse the repository at this point in the history
  • Loading branch information
EagleoutIce committed Jul 4, 2024
2 parents a6c8967 + e969fc1 commit 1606c4f
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions src/cli/repl/server/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ import type { PipelineOutput } from '../../../core/steps/pipeline/pipeline'
import type { NormalizedAst } from '../../../r-bridge/lang-4.x/ast/model/processing/decorate'
import type { DeepPartial } from 'ts-essentials'
import { DataflowGraph } from '../../../dataflow/graph/graph'
import * as tmp from 'tmp'
import fs from 'fs'
import type { RParseRequest } from '../../../r-bridge/retriever'

/**
* Each connection handles a single client, answering to its requests.
Expand All @@ -47,6 +50,8 @@ export class FlowRServerConnection {
pipeline: PipelineExecutor<typeof DEFAULT_SLICING_PIPELINE>
}>()

private tempFile: tmp.FileResult | undefined

// we do not have to ensure synchronized shell-access as we are always running synchronized
constructor(socket: Socket, name: string, shell: RShell) {
this.socket = socket
Expand Down Expand Up @@ -155,13 +160,19 @@ export class FlowRServerConnection {
}

private createPipelineExecutorForRequest(message: FileAnalysisRequestMessage) {
let request: RParseRequest
if(message.content !== undefined){
// we store the code in a temporary file in case it's too big for the shell to handle
const temp = this.getTempFile()
fs.writeFileSync(temp.name, message.content ?? '')
request = { request: 'file', content: temp.name }
} else {
request = { request: 'file', content: message.filepath as string }
}

const slicer = new PipelineExecutor(DEFAULT_SLICING_PIPELINE, {
shell: this.shell,
// we have to make sure that the content is not interpreted as a file path if it starts with 'file://' therefore, we do it manually
request: {
request: message.content === undefined ? 'file' : 'text',
content: message.content ?? message.filepath as string
},
shell: this.shell,
request,
criterion: [] // currently unknown
})
if(message.filetoken) {
Expand All @@ -174,6 +185,14 @@ export class FlowRServerConnection {
return slicer
}

private getTempFile(): tmp.FileResult {
if(!this.tempFile) {
this.tempFile = tmp.fileSync({ postfix: '.R', keep: false })
this.socket.on('close', () => this.tempFile?.removeCallback())
}
return this.tempFile
}

private handleSliceRequest(base: SliceRequestMessage) {
const requestResult = validateMessage(base, requestSliceMessage)
if(requestResult.type === 'error') {
Expand Down

2 comments on commit 1606c4f

@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: 1606c4f Previous: d16b9a2 Ratio
Retrieve AST from R code 247.16063495454546 ms (109.87841922987363) 241.68802936363636 ms (102.8761180575731) 1.02
Normalize R AST 32.359998363636365 ms (65.07131591233215) 31.739752136363638 ms (62.50756200538419) 1.02
Produce dataflow information 56.07293577272727 ms (136.816999199163) 53.60407036363637 ms (129.65284550123417) 1.05
Total per-file 1421.398435909091 ms (3693.4055478860137) 1269.491520090909 ms (3081.8881842692626) 1.12
Static slicing 1.1670126548019515 ms (1.0206433425268728) 1.1992957765588008 ms (1.0956190505793448) 0.97
Reconstruct code 0.43937968868692306 ms (0.29017214707317324) 0.40823540912587025 ms (0.23279531342826934) 1.08
Total per-slice 1.6244154321553097 ms (1.1075752334439364) 1.6255373838885934 ms (1.1638162453002103) 1.00
failed to reconstruct/re-parse 0 # 0 # 1
times hit threshold 0 # 0 # 1
reduction (characters) 0.7974469447714406 # 0.7974469447714406 # 1
reduction (normalized tokens) 0.774073695901592 # 0.774073695901592 # 1
memory (df-graph) 147.58589311079547 KiB (359.2574768951678) 147.58589311079547 KiB (359.2574768951678) 1

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: 1606c4f Previous: d16b9a2 Ratio
Retrieve AST from R code 239.40209787999999 ms (43.598821748108755) 247.62148213999998 ms (45.06237178263203) 0.97
Normalize R AST 32.31091648 ms (26.492203931230392) 31.95647656 ms (25.60791349140293) 1.01
Produce dataflow information 79.42043978 ms (90.43021709227257) 81.06377198 ms (92.78105042956382) 0.98
Total per-file 2190.43111466 ms (3725.7835413982048) 2253.7859776799996 ms (3816.1054094291667) 0.97
Static slicing 4.27340698541059 ms (7.830632573033979) 4.320062977207287 ms (7.979427803781336) 0.99
Reconstruct code 0.36539708426191764 ms (0.18462940519189328) 0.4180827296166881 ms (0.21129610863943676) 0.87
Total per-slice 4.647506236363018 ms (7.89258162805894) 4.7471979325717735 ms (8.060206129507852) 0.98
failed to reconstruct/re-parse 0 # 0 # 1
times hit threshold 0 # 0 # 1
reduction (characters) 0.9250085598882571 # 0.9250085598882571 # 1
reduction (normalized tokens) 0.8936602769604 # 0.8936602769604 # 1
memory (df-graph) 142.5463671875 KiB (146.6995040110581) 142.5463671875 KiB (146.6995040110581) 1

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

Please sign in to comment.