Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Json NSIS AdvReplaceInFile太慢,无法更改共享文件_Json_Performance_Replace_Installation_Nsis - Fatal编程技术网

Json NSIS AdvReplaceInFile太慢,无法更改共享文件

Json NSIS AdvReplaceInFile太慢,无法更改共享文件,json,performance,replace,installation,nsis,Json,Performance,Replace,Installation,Nsis,我的安装程序需要在一个相当大(500Kb,约11k行)的json文件中更改一行。我正在使用查找和替换事件 问题是,完成这项工作大约需要3秒钟。我可以使用相同的文件终止应用程序,但如果在该时间窗口内自动重新启动,即使替换成功,该过程稍后也会覆盖该文件 我假设负责搜索和替换的算法是导致减速的原因 除了编写自己的DLL插件之外,我怎样才能让它运行得更快呢?NSIS代码从来都不是设计得很快的,对Int*函数的任何调用都至少涉及两个字符串到数字的转换等 使用稍慢一点的函数版本,可以防止其他进程打开文件进行

我的安装程序需要在一个相当大(500Kb,约11k行)的json文件中更改一行。我正在使用查找和替换事件

问题是,完成这项工作大约需要3秒钟。我可以使用相同的文件终止应用程序,但如果在该时间窗口内自动重新启动,即使替换成功,该过程稍后也会覆盖该文件

我假设负责搜索和替换的算法是导致减速的原因


除了编写自己的DLL插件之外,我怎样才能让它运行得更快呢?

NSIS代码从来都不是设计得很快的,对Int*函数的任何调用都至少涉及两个字符串到数字的转换等

使用稍慢一点的函数版本,可以防止其他进程打开文件进行写入:

Function AdvReplaceInFile
Exch $0 ;file to replace in
Exch
Exch $1 ;number to replace after
Exch
Exch 2
Exch $2 ;replace and onwards
Exch 2
Exch 3
Exch $3 ;replace with
Exch 3
Exch 4
Exch $4 ;to replace
Exch 4
Push $5 ;minus count
Push $6 ;universal
Push $7 ;end string
Push $8 ;left string
Push $9 ;right string
Push $R0 ;file1
Push $R1 ;file2
Push $R2 ;read
Push $R3 ;universal
Push $R4 ;count (onwards)
Push $R5 ;count (after)
Push $R6 ;temp file name

  GetTempFileName $R6
  FileOpen $R1 $0 a ;file to search in
  FileOpen $R0 $R6 a ;temp file
   StrLen $R3 $4
   StrCpy $R4 -1
   StrCpy $R5 -1

loop_read:
 ClearErrors
 FileRead $R1 $R2 ;read line
 IfErrors exit

   StrCpy $5 0
   StrCpy $7 $R2

loop_filter:
   IntOp $5 $5 - 1
   StrCpy $6 $7 $R3 $5 ;search
   StrCmp $6 "" file_write1
   StrCmp $6 $4 0 loop_filter

StrCpy $8 $7 $5 ;left part
IntOp $6 $5 + $R3
IntCmp $6 0 is0 not0
is0:
StrCpy $9 ""
Goto done
not0:
StrCpy $9 $7 "" $6 ;right part
done:
StrCpy $7 $8$3$9 ;re-join

IntOp $R4 $R4 + 1
StrCmp $2 all loop_filter
StrCmp $R4 $2 0 file_write2
IntOp $R4 $R4 - 1

IntOp $R5 $R5 + 1
StrCmp $1 all loop_filter
StrCmp $R5 $1 0 file_write1
IntOp $R5 $R5 - 1
Goto file_write2

file_write1:
 FileWrite $R0 $7 ;write modified line
Goto loop_read

file_write2:
 FileWrite $R0 $R2 ;write unmodified line
Goto loop_read

exit:
  FileSeek $R1 0 SET
  !if "${NSIS_PTR_SIZE}" > 4
  System::Call 'kernel32::SetEndOfFile(p$R1)'
  !else
  System::Call 'kernel32::SetEndOfFile(i$R1)'
  !endif
  FileSeek $R0 0 SET
  ClearErrors
  mov_loop:
    FileRead $R0 $R2
    IfErrors mov_done
    FileWrite $R1 $R2
    Goto mov_loop
  mov_done:
  FileClose $R0
  FileClose $R1
  SetDetailsPrint none
  Delete $R6
  SetDetailsPrint lastused

Pop $R6
Pop $R5
Pop $R4
Pop $R3
Pop $R2
Pop $R1
Pop $R0
Pop $9
Pop $8
Pop $7
Pop $6
Pop $5
;These values are stored in the stack in the reverse order they were pushed
Pop $0
Pop $1
Pop $2
Pop $3
Pop $4
FunctionEnd
真正让它更快的唯一方法是调用外部程序或编写插件