New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix exhaust stack & make better performance at oneOrMore parser #15

Merged
merged 1 commit into from Apr 20, 2018

Conversation

Projects
None yet
2 participants
@banjun
Copy link
Contributor

banjun commented Apr 20, 2018

I found that Character -> String version of oneOrMore parser can crash by exhausting stack.
It seems to be related to recursive call with lazy.
This fix actually uses the generic version. Therefore it's naive but should work in better performance without crashes 馃槙

Reproduce:

  • change oneOrMore test input with large number such as 10000 and do test

let input = String(repeating: "a", count: 1000)
measure {
self.assertParseSucceeds(parser, input, consumed: 1000)

Analysis:

Print debugging Thread.callStackSymbols.count shows an increase as parsing progress before this fix. With this fix, the stack depth does not increase.

Example use case:

Parsing Server Sent Events format requires parsing payload characters until newline. Sometimes it's large single-line JSON and can cause parser crash on runtime.

e.g. https://github.com/banjun/ReactiveSSE/blob/05d95c87b05018d154e33ff9bc74ac5168a1fc53/ReactiveSSE/Classes/EventStream.swift

Performance:

I measured using the performance test with release build and it makes around 60% better performance for Strings.

@banjun

This comment has been minimized.

Copy link
Contributor

banjun commented Apr 20, 2018

additional info...
As background queue has less stack size than main queue, parsers more likely to crash when used in background.

@kareman kareman merged commit 7095040 into kareman:master Apr 20, 2018

1 check passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details
@kareman

This comment has been minimized.

Copy link
Owner

kareman commented Apr 20, 2018

Nice, and thank you for the detailed description.

@banjun banjun deleted the banjun:fix-exhaust-stack branch Apr 21, 2018

@banjun

This comment has been minimized.

Copy link
Contributor

banjun commented Apr 23, 2018

Thanks for merging. Would you mind pod trunk push a new version?

@kareman

This comment has been minimized.

Copy link
Owner

kareman commented Apr 23, 2018

No problem, just released version 0.5.0. It also includes an update to Swift 4.1. Let me know if you can't use Swift 4.1 yet, I can release a version 0.4.2 with just your changes.

@banjun

This comment has been minimized.

Copy link
Contributor

banjun commented Apr 24, 2018

@kareman thank you & everything works fine!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment