Skip to content

Commit

Permalink
Reduce memory usage during parse (experimental; works, but we need to…
Browse files Browse the repository at this point in the history
… apply this to all methods inside SVGKPointsAndPathsParser)
  • Loading branch information
adamgit authored and adamgit committed Sep 7, 2013
1 parent f1eabd1 commit 570eacd
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions Source/Parsers/SVGKPointsAndPathsParser.m
Original file line number Diff line number Diff line change
Expand Up @@ -441,14 +441,33 @@ + (SVGCurve) readCurvetoCommand:(NSScanner*)scanner path:(CGMutablePathRef)path
*/
+ (SVGCurve) readCurvetoArgumentSequence:(NSScanner*)scanner path:(CGMutablePathRef)path relativeTo:(CGPoint)origin isRelative:(BOOL) isRelative
{
SVGCurve curve = [SVGKPointsAndPathsParser readCurvetoArgument:scanner path:path relativeTo:origin];

if (![scanner isAtEnd]) {
curve = [SVGKPointsAndPathsParser readCurvetoArgumentSequence:scanner path:path relativeTo:(isRelative ? curve.p : origin) isRelative:isRelative];
}
SVGCurve curve;

[self readCurvetoArgumentSequence:scanner path:path relativeTo:origin isRelative:isRelative overwritingCurve:&curve];

return curve;
}

/**
Recursive method that does the work of:
readCurvetoArgumentSequence:path:relativeTo:isRelative:
...with constant memory (avoids allocating a new struct on the stack).
Large SVG files recurse very deep, and the tiny struct was reaching multiple-megabytes in size, when multipled by 100's of recursions
*/
+ (void) readCurvetoArgumentSequence:(NSScanner*)scanner path:(CGMutablePathRef)path relativeTo:(CGPoint)origin isRelative:(BOOL) isRelative overwritingCurve:(SVGCurve*) curvePointer
{
*curvePointer = [SVGKPointsAndPathsParser readCurvetoArgument:scanner path:path relativeTo:origin];

if (![scanner isAtEnd])
{
CGPoint newOrigin = isRelative ? curvePointer->p : origin; /** FIXME: this could accidentally get corrupted, add an extra pre-allocation for it before the recursion starts */
[SVGKPointsAndPathsParser readCurvetoArgumentSequence:scanner path:path relativeTo:newOrigin isRelative:isRelative overwritingCurve:curvePointer];
}
}

/**
curveto-argument:
coordinate-pair comma-wsp? coordinate-pair comma-wsp? coordinate-pair
Expand Down

0 comments on commit 570eacd

Please sign in to comment.