From 600b3a91abbe7dede5abd9744e02fc48089ee34d Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Mon, 7 Nov 2022 21:36:43 +0800 Subject: [PATCH] fixes regression #20746; remove string copies for ORC booted compiler (#20776) * fixes #20746; remove string copies for ORC booted compiler * add a test case * use `cursor` thanks to @beef331 * for old compilers * change file extension * change test cases --- compiler/vm.nim | 4 +++- tests/vm/t20746.nim | 13 +++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 tests/vm/t20746.nim diff --git a/compiler/vm.nim b/compiler/vm.nim index 1a41ce8a1f..99d8c4d416 100644 --- a/compiler/vm.nim +++ b/compiler/vm.nim @@ -32,6 +32,8 @@ const when hasFFI: import evalffi +when not defined(nimHasCursor): + {.pragma: cursor.} proc stackTraceAux(c: PCtx; x: PStackFrame; pc: int; recursionLimit=100) = if x != nil: @@ -787,7 +789,7 @@ proc rawExecute(c: PCtx, start: int, tos: PStackFrame): TFullReg = of opcLdStrIdx: decodeBC(rkInt) let idx = regs[rc].intVal.int - let s = regs[rb].node.strVal + let s {.cursor.} = regs[rb].node.strVal if idx <% s.len: regs[ra].intVal = s[idx].ord else: diff --git a/tests/vm/t20746.nim b/tests/vm/t20746.nim new file mode 100644 index 0000000000..bfad269ef3 --- /dev/null +++ b/tests/vm/t20746.nim @@ -0,0 +1,13 @@ +discard """ + timeout: 10 + joinable: false + output: "fine" +""" + +func addString(): string = + let x = newString(1000000) + for i in 0..<1000000: + discard x[i] + +const translationTable = addString() +echo "fine"