Skip to content
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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting 500x performance drop compared to pure NSRegularExpression #36

Open
iby opened this issue Apr 8, 2017 · 3 comments
Open

Getting 500x performance drop compared to pure NSRegularExpression #36

iby opened this issue Apr 8, 2017 · 3 comments

Comments

@iby
Copy link

iby commented Apr 8, 2017

Love the syntax. Love the features. But performance goes bad on large strings. Here's the test for digesting a crashlog.

internal func testRegex() {
    let content: String = try! String(contentsOf: PathUtility.getCrashlogUrl(name: "gifox"))
    let nsContent: NSString = content as NSString

    let pattern: String = "^\\s*0x([A-F0-9]+) - \\s*0x([A-F0-9]+) \\s*\\+?(.+?) .*? \\<([A-F0-9]{8}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{12})\\>"

    let regex: Regex = try! Regex(pattern: pattern, options: [RegexOptions.caseInsensitive, RegexOptions.anchorsMatchLines])
    let nsRegex: NSRegularExpression = try! NSRegularExpression(pattern: pattern, options: [NSRegularExpression.Options.caseInsensitive, NSRegularExpression.Options.anchorsMatchLines])

    var date: Date
    var intervalAll: Double
    var match: [String]

    date = Date()
    match = regex.findAll(in: content).map({ $0.group(at: 1)! })
    intervalAll = Date().timeIntervalSince(date)
    Swift.print("regex: ", intervalAll)
    Swift.print(match.count, match)

    date = Date()
    match = nsRegex.matches(in: content, range: NSRange(0 ..< content.characters.count)).map({ nsContent.substring(with: $0.rangeAt(1)) as String })
    intervalAll = Date().timeIntervalSince(date)
    Swift.print("nsRegex: ", intervalAll)
    Swift.print(match.count, match)

    // regex:  7.71533399820328
    // 327 ["101e03000", "102186000", "102348000", "1023bf000", "102451000", "1024de000", "1024fc000", "102573000", "1025c2000", "1027dc000", "102812000", "102847000", "1028bf000", "102914000", "102992000", "1029ee000", "102a3b000", "102a49000", "102a64000", "102f78000", "102f92000", "102fbd000", "102fc8000", "102fda000", "103034000", "103215000", "103220000", "103231000", "103243000", "103250000", "1032ee000", "10335e000", "10336c000", "10337d000", "10343d000", "1034e5000", "10352b000", "103560000", "10359b000", "103613000", "1036c3000", "105225000", "105412000", "1058f3000", "1058f9000", "107afe000", "107ce3000", "108a6e000", "108b4e000", "10913f000", "10982f000", "7fffa6cf2000", "7fffa7038000", "7fffa886e000", "7fffa8a30000", "7fffa8b9d000", "7fffa8bb6000", "7fffa90d0000", "7fffa9242000", "7fffa9257000", "7fffa964e000", "7fffa9665000", "7fffa966c000", "7fffa9681000", "7fffa9809000", "7fffa98bc000", "7fffa98bd000", "7fffa98fb000", "7fffa9b7b000", "7fffaa966000", "7fffaa967000", "7fffaaa6f000", "7fffaab9f000", "7fffaac46000", "7fffaace5000", "7fffaad90000", "7fffaade1000", "7fffaadf1000", "7fffaae3f000", "7fffaae7b000", "7fffaae87000", "7fffab094000", "7fffab1fd000", "7fffab5f1000", "7fffab5f2000", "7fffab5f6000", "7fffab900000", "7fffab904000", "7fffab90a000", "7fffab9a2000", "7fffab9bd000", "7fffab9bf000", "7fffab9c2000", "7fffabaa9000", "7fffabab7000", "7fffabbf3000", "7fffabc81000", "7fffabc95000", "7fffabf91000", "7fffac03e000", "7fffac4d8000", "7fffacb5b000", "7fffacd9f000", "7ffface05000", "7fffaceb7000", "7fffacf03000", "7fffacf04000", "7fffacf56000", "7fffad232000", "7fffad266000", "7fffad26f000", "7fffad3dc000", "7fffad48d000", "7fffad4ed000", "7fffad55e000", "7fffad62c000", "7fffad779000", "7fffad7af000", "7fffad853000", "7fffad91e000", "7fffadab5000", "7fffade87000", "7fffadf78000", "7fffae01c000", "7fffae0b2000", "7fffae0b9000", "7fffae10b000", "7fffae26b000", "7fffae270000", "7fffae361000", "7fffae385000", "7fffae3ad000", "7fffae3b0000", "7fffae55c000", "7fffaf165000", "7fffaf17f000", "7fffaf213000", "7fffaf407000", "7fffaf423000", "7fffaf95d000", "7fffb02a1000", "7fffb0480000", "7fffb0489000", "7fffb04d8000", "7fffb04f2000", "7fffb04fe000", "7fffb0501000", "7fffb0505000", "7fffb050a000", "7fffb0514000", "7fffb0520000", "7fffb055d000", "7fffb06d4000", "7fffb107d000", "7fffb1128000", "7fffb1270000", "7fffb14db000", "7fffb15a3000", "7fffb1aca000", "7fffb1aee000", "7fffb1bdc000", "7fffb1bdd000", "7fffb1dde000", "7fffb1e34000", "7fffb2344000", "7fffb2647000", "7fffb26bd000", "7fffb26e7000", "7fffb2a71000", "7fffb2ae1000", "7fffb2e90000", "7fffb4217000", "7fffb4404000", "7fffb5333000", "7fffb534f000", "7fffb535d000", "7fffb5380000", "7fffb547a000", "7fffb5506000", "7fffb5519000", "7fffb5535000", "7fffb553e000", "7fffb5542000", "7fffb5938000", "7fffb6052000", "7fffb6641000", "7fffb6f8d000", "7fffb705d000", "7fffb70ae000", "7fffb711a000", "7fffb74fa000", "7fffb75c5000", "7fffb76de000", "7fffb77b3000", "7fffb77d0000", "7fffb7a29000", "7fffb7cea000", "7fffb7d7a000", "7fffb7eba000", "7fffb7fda000", "7fffb8040000", "7fffb80c0000", "7fffb80cb000", "7fffb8105000", "7fffb81b6000", "7fffb81f6000", "7fffb8449000", "7fffb85ed000", "7fffb9d75000", "7fffb9d82000", "7fffba046000", "7fffba07b000", "7fffba773000", "7fffbadf9000", "7fffbae04000", "7fffbae0c000", "7fffbae0d000", "7fffbae22000", "7fffbaee0000", "7fffbaf2b000", "7fffbb752000", "7fffbba03000", "7fffbbbd8000", "7fffbbbf6000", "7fffbbc1f000", "7fffbbcd0000", "7fffbc128000", "7fffbc5b3000", "7fffbc637000", "7fffbccdc000", "7fffbccf7000", "7fffbcd0f000", "7fffbd9f8000", "7fffbda8a000", "7fffbdb38000", "7fffbdf7c000", "7fffbe672000", "7fffbeb24000", "7fffbebb6000", "7fffbecc5000", "7fffbecdb000", "7fffbecf0000", "7fffbecf2000", "7fffbeefc000", "7fffbf951000", "7fffbfe5c000", "7fffbff33000", "7fffbff61000", "7fffbff8a000", "7fffbffa6000", "7fffc0043000", "7fffc0045000", "7fffc0274000", "7fffc027c000", "7fffc027d000", "7fffc0282000", "7fffc02ef000", "7fffc031b000", "7fffc039b000", "7fffc039c000", "7fffc03ad000", "7fffc03bc000", "7fffc0413000", "7fffc043e000", "7fffc044f000", "7fffc0466000", "7fffc0467000", "7fffc0522000", "7fffc07a5000", "7fffc084a000", "7fffc0873000", "7fffc0883000", "7fffc0889000", "7fffc097c000", "7fffc0ba7000", "7fffc0ba9000", "7fffc0bc3000", "7fffc0bda000", "7fffc0eb5000", "7fffc0f30000", "7fffc1305000", "7fffc130a000", "7fffc1358000", "7fffc1375000", "7fffc13af000", "7fffc13c2000", "7fffc13c4000", "7fffc156d000", "7fffc1606000", "7fffc1614000", "7fffc1704000", "7fffc172e000", "7fffc174e000", "7fffc1753000", "7fffc175e000", "7fffc1766000", "7fffc176f000", "7fffc17f3000", "7fffc1825000", "7fffc182b000", "7fffc182c000", "7fffc1839000", "7fffc183a000", "7fffc1840000", "7fffc1843000", "7fffc1845000", "7fffc185e000", "7fffc185f000", "7fffc18ed000", "7fffc18f1000", "7fffc18f5000", "7fffc190e000", "7fffc1915000", "7fffc193f000", "7fffc1962000", "7fffc19aa000", "7fffc19c9000", "7fffc1a23000", "7fffc1a2d000", "7fffc1a37000", "7fffc1a40000", "7fffc1a4b000", "7fffc1a4f000", "7fffc1a51000", "7fffc1a59000", "7fffc1a6d000", "7fffc1a73000"]
    // nsRegex:  0.0154680013656616
    // 327 ["101e03000", "102186000", "102348000", "1023bf000", "102451000", "1024de000", "1024fc000", "102573000", "1025c2000", "1027dc000", "102812000", "102847000", "1028bf000", "102914000", "102992000", "1029ee000", "102a3b000", "102a49000", "102a64000", "102f78000", "102f92000", "102fbd000", "102fc8000", "102fda000", "103034000", "103215000", "103220000", "103231000", "103243000", "103250000", "1032ee000", "10335e000", "10336c000", "10337d000", "10343d000", "1034e5000", "10352b000", "103560000", "10359b000", "103613000", "1036c3000", "105225000", "105412000", "1058f3000", "1058f9000", "107afe000", "107ce3000", "108a6e000", "108b4e000", "10913f000", "10982f000", "7fffa6cf2000", "7fffa7038000", "7fffa886e000", "7fffa8a30000", "7fffa8b9d000", "7fffa8bb6000", "7fffa90d0000", "7fffa9242000", "7fffa9257000", "7fffa964e000", "7fffa9665000", "7fffa966c000", "7fffa9681000", "7fffa9809000", "7fffa98bc000", "7fffa98bd000", "7fffa98fb000", "7fffa9b7b000", "7fffaa966000", "7fffaa967000", "7fffaaa6f000", "7fffaab9f000", "7fffaac46000", "7fffaace5000", "7fffaad90000", "7fffaade1000", "7fffaadf1000", "7fffaae3f000", "7fffaae7b000", "7fffaae87000", "7fffab094000", "7fffab1fd000", "7fffab5f1000", "7fffab5f2000", "7fffab5f6000", "7fffab900000", "7fffab904000", "7fffab90a000", "7fffab9a2000", "7fffab9bd000", "7fffab9bf000", "7fffab9c2000", "7fffabaa9000", "7fffabab7000", "7fffabbf3000", "7fffabc81000", "7fffabc95000", "7fffabf91000", "7fffac03e000", "7fffac4d8000", "7fffacb5b000", "7fffacd9f000", "7ffface05000", "7fffaceb7000", "7fffacf03000", "7fffacf04000", "7fffacf56000", "7fffad232000", "7fffad266000", "7fffad26f000", "7fffad3dc000", "7fffad48d000", "7fffad4ed000", "7fffad55e000", "7fffad62c000", "7fffad779000", "7fffad7af000", "7fffad853000", "7fffad91e000", "7fffadab5000", "7fffade87000", "7fffadf78000", "7fffae01c000", "7fffae0b2000", "7fffae0b9000", "7fffae10b000", "7fffae26b000", "7fffae270000", "7fffae361000", "7fffae385000", "7fffae3ad000", "7fffae3b0000", "7fffae55c000", "7fffaf165000", "7fffaf17f000", "7fffaf213000", "7fffaf407000", "7fffaf423000", "7fffaf95d000", "7fffb02a1000", "7fffb0480000", "7fffb0489000", "7fffb04d8000", "7fffb04f2000", "7fffb04fe000", "7fffb0501000", "7fffb0505000", "7fffb050a000", "7fffb0514000", "7fffb0520000", "7fffb055d000", "7fffb06d4000", "7fffb107d000", "7fffb1128000", "7fffb1270000", "7fffb14db000", "7fffb15a3000", "7fffb1aca000", "7fffb1aee000", "7fffb1bdc000", "7fffb1bdd000", "7fffb1dde000", "7fffb1e34000", "7fffb2344000", "7fffb2647000", "7fffb26bd000", "7fffb26e7000", "7fffb2a71000", "7fffb2ae1000", "7fffb2e90000", "7fffb4217000", "7fffb4404000", "7fffb5333000", "7fffb534f000", "7fffb535d000", "7fffb5380000", "7fffb547a000", "7fffb5506000", "7fffb5519000", "7fffb5535000", "7fffb553e000", "7fffb5542000", "7fffb5938000", "7fffb6052000", "7fffb6641000", "7fffb6f8d000", "7fffb705d000", "7fffb70ae000", "7fffb711a000", "7fffb74fa000", "7fffb75c5000", "7fffb76de000", "7fffb77b3000", "7fffb77d0000", "7fffb7a29000", "7fffb7cea000", "7fffb7d7a000", "7fffb7eba000", "7fffb7fda000", "7fffb8040000", "7fffb80c0000", "7fffb80cb000", "7fffb8105000", "7fffb81b6000", "7fffb81f6000", "7fffb8449000", "7fffb85ed000", "7fffb9d75000", "7fffb9d82000", "7fffba046000", "7fffba07b000", "7fffba773000", "7fffbadf9000", "7fffbae04000", "7fffbae0c000", "7fffbae0d000", "7fffbae22000", "7fffbaee0000", "7fffbaf2b000", "7fffbb752000", "7fffbba03000", "7fffbbbd8000", "7fffbbbf6000", "7fffbbc1f000", "7fffbbcd0000", "7fffbc128000", "7fffbc5b3000", "7fffbc637000", "7fffbccdc000", "7fffbccf7000", "7fffbcd0f000", "7fffbd9f8000", "7fffbda8a000", "7fffbdb38000", "7fffbdf7c000", "7fffbe672000", "7fffbeb24000", "7fffbebb6000", "7fffbecc5000", "7fffbecdb000", "7fffbecf0000", "7fffbecf2000", "7fffbeefc000", "7fffbf951000", "7fffbfe5c000", "7fffbff33000", "7fffbff61000", "7fffbff8a000", "7fffbffa6000", "7fffc0043000", "7fffc0045000", "7fffc0274000", "7fffc027c000", "7fffc027d000", "7fffc0282000", "7fffc02ef000", "7fffc031b000", "7fffc039b000", "7fffc039c000", "7fffc03ad000", "7fffc03bc000", "7fffc0413000", "7fffc043e000", "7fffc044f000", "7fffc0466000", "7fffc0467000", "7fffc0522000", "7fffc07a5000", "7fffc084a000", "7fffc0873000", "7fffc0883000", "7fffc0889000", "7fffc097c000", "7fffc0ba7000", "7fffc0ba9000", "7fffc0bc3000", "7fffc0bda000", "7fffc0eb5000", "7fffc0f30000", "7fffc1305000", "7fffc130a000", "7fffc1358000", "7fffc1375000", "7fffc13af000", "7fffc13c2000", "7fffc13c4000", "7fffc156d000", "7fffc1606000", "7fffc1614000", "7fffc1704000", "7fffc172e000", "7fffc174e000", "7fffc1753000", "7fffc175e000", "7fffc1766000", "7fffc176f000", "7fffc17f3000", "7fffc1825000", "7fffc182b000", "7fffc182c000", "7fffc1839000", "7fffc183a000", "7fffc1840000", "7fffc1843000", "7fffc1845000", "7fffc185e000", "7fffc185f000", "7fffc18ed000", "7fffc18f1000", "7fffc18f5000", "7fffc190e000", "7fffc1915000", "7fffc193f000", "7fffc1962000", "7fffc19aa000", "7fffc19c9000", "7fffc1a23000", "7fffc1a2d000", "7fffc1a37000", "7fffc1a40000", "7fffc1a4b000", "7fffc1a4f000", "7fffc1a51000", "7fffc1a59000", "7fffc1a6d000", "7fffc1a73000"]
}
@dileping
Copy link
Member

Hey @ianbytchek , thanks for reporting. Could you, please, post the log?

@kengruven
Copy link

My first thought is that this looks like a symptom of #38 -- detecting extended grapheme clusters can be really slow -- but the NSRegularExpression example also (incorrectly) uses .characters.count here, so there's something else going on in the library, too.

@chriszielinski
Copy link
Contributor

I've managed to track it down to the MatchSequence.Iterator. I believe the performance drop is attributed to the Match initializations that occur within the iterator. You can see the temporary heap allocations are almost doubled.

Temporary Heap Allocations

You can play around with the test cases in the performance branch on my fork.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants