diff options
author | heterodoxic <122719743+heterodoxic@users.noreply.github.com> | 2023-05-30 13:41:56 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-05-30 13:41:56 +0200 |
commit | 546af8c571fb4f09187e1b343b11cf449657bad8 (patch) | |
tree | a8fdca24310cb1a1bae40615e9a7c691294df47b | |
parent | 40f88da90b0589a91f4f60b2ebf49859b79e2247 (diff) | |
download | Nim-546af8c571fb4f09187e1b343b11cf449657bad8.tar.gz |
simple micro-optimizations of ropes' runtime-formatting (#21962)
-rw-r--r-- | compiler/cgen.nim | 10 | ||||
-rw-r--r-- | compiler/ropes.nim | 13 |
2 files changed, 8 insertions, 15 deletions
diff --git a/compiler/cgen.nim b/compiler/cgen.nim index 8c6946300..fe5c253dd 100644 --- a/compiler/cgen.nim +++ b/compiler/cgen.nim @@ -216,13 +216,9 @@ macro ropecg(m: BModule, frmt: static[FormatStr], args: untyped): Rope = elif frmt[i] == '#' and frmt[i+1] == '#': inc(i, 2) strLit.add("#") - - var start = i - while i < frmt.len: - if frmt[i] != '$' and frmt[i] != '#': inc(i) - else: break - if i - 1 >= start: - strLit.add(substr(frmt, start, i - 1)) + else: + strLit.add(frmt[i]) + inc(i) flushStrLit() result.add newCall(ident"rope", resVar) diff --git a/compiler/ropes.nim b/compiler/ropes.nim index 5bf154393..e96a3ed3a 100644 --- a/compiler/ropes.nim +++ b/compiler/ropes.nim @@ -21,8 +21,8 @@ type # though it is not necessary) Rope* = string -proc newRopeAppender*(): string {.inline.} = - result = newString(0) +proc newRopeAppender*(cap = 80): string {.inline.} = + result = newStringOfCap(cap) proc freeze*(r: Rope) {.inline.} = discard @@ -102,12 +102,9 @@ proc runtimeFormat*(frmt: FormatStr, args: openArray[Rope]): Rope = inc(i) else: doAssert false, "invalid format string: " & frmt - var start = i - while i < frmt.len: - if frmt[i] != '$': inc(i) - else: break - if i - 1 >= start: - result.add(substr(frmt, start, i - 1)) + else: + result.add(frmt[i]) + inc(i) proc `%`*(frmt: static[FormatStr], args: openArray[Rope]): Rope = runtimeFormat(frmt, args) |