Skip to content

Commit d4ab87a

Browse files
committed
Scale is now a ref object
Scale previously was a huge stack waster. This caused the failures seen in: nim-lang/Nim#13206 The test =tests/tests.nim= previously required a stack of about 3600 kB to avoid a stack overflow. With the =Scale= being a =ref object= we get that down to 135 kB!
1 parent 457f58f commit d4ab87a

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

src/ggplotnim.nim

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,7 @@ proc fillScaleImpl(
457457
## NOTE: The given `col` arg is not necessarily exactly a DF key anymore, since
458458
## it might contain two or more columns as its basis
459459
# get the data column we scale by
460+
result = new Scale
460461
if isDiscrete:
461462
# convert to set to filter duplicates, back to seq and sort
462463
# TODO: we could also use `sequtils.deduplicate` here
@@ -1061,7 +1062,7 @@ proc `+`*(p: GgPlot, theme: Theme): GgPlot =
10611062
proc applyScale(aes: Aesthetics, scale: Scale): Aesthetics =
10621063
## applies the given `scale` to the `aes` by returning a modified
10631064
## `aes`
1064-
var mscale = scale
1065+
var mscale = deepCopy(scale)
10651066
result = aes
10661067
case mscale.scKind
10671068
of scLinearData, scTransformedData:
@@ -1340,6 +1341,7 @@ macro genGetScale(field: untyped): untyped =
13401341
let name = ident("get" & $field.strVal & "Scale")
13411342
result = quote do:
13421343
proc `name`(filledScales: FilledScales, geom = Geom(gid: 0)): Scale =
1344+
result = new Scale
13431345
if filledScales.`field`.main.isSome:
13441346
# use main
13451347
result = filledScales.`field`.main.get

src/ggplotnim/ggplot_types.nim

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ type
6969
# e.g. `xaxis`, `<name of geom>.xaxis` etc.?
7070
ScaleTransform* = proc(v: Value): Value
7171

72-
Scale* = object
72+
Scale* = ref object
7373
# the column which this scale corresponds to
7474
col*: string
7575
name*: string
@@ -288,6 +288,7 @@ proc hash*(x: Scale): Hash =
288288
result = result !& hash(x.dataScale)
289289
result = !$result
290290

291+
proc `$`*(s: Scale): string
291292
proc `$`*(f: Facet): string =
292293
result = "(columns: "
293294
for i, x in f.columns:

0 commit comments

Comments
 (0)