<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="" applecontenteditable="true">Hi Nethra,<div class=""><br class=""><div class=""><div><blockquote type="cite" class=""><div class="">On Feb 7, 2017, at 11:44 PM, Nethra Ravindran via swift-corelibs-dev <<a href="mailto:swift-corelibs-dev@swift.org" class="">swift-corelibs-dev@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><br class=""><div class=""><div class=""><br class=""></div><div class=""><br class=""></div><div class="">Hi everyone,</div><div class=""><br class=""></div><div class="">I am working on <a href="https://bugs.swift.org/browse/SR-3536" class="">https://bugs.swift.org/browse/SR-3536</a></div><div class=""><p class="">There is a memory leak when searching for the substring of a string using regular expression.</p></div><div class=""><br class=""></div><div class=""><pre style="box-sizing:border-box;font-family:consolas,'liberation mono',menlo,courier,monospace;font-size:11.899999618530273px;margin-top:0px;margin-bottom:16px;line-height:1.45;word-wrap:normal;padding:16px;overflow:auto;background-color:rgb(247,247,247);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;color:rgb(51,51,51)" class=""><code style="box-sizing:border-box;font-family:consolas,'liberation mono',menlo,courier,monospace;padding:0px;margin:0px;background-color:transparent;border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;word-break:normal;border:0px;display:inline;overflow:visible;line-height:inherit;word-wrap:normal" class="">import Foundation
let myString = "Foo"
for _ in 1...10000 {
let _ = myString.range(of: "bar", options: .regularExpression)
}</code></pre><p style="font-family: helveticaneue; font-size: 12px;" class=""><br class=""></p><p style="font-family: helveticaneue;" class="">From the above test case i could see that over a period of time, around 60 Mb of memory was leaked. </p><p style="font-family: helveticaneue;" class="">I see in String.range we eventually call NSString._createRegexForPattern. Here we maintain a mapping between NSString and NSRegularExpression object in NSCache<NSString, NSRegularExpression>. All the entries in the cache are maintained in a dictionary ( _entries ) which takes the UnsafeRawPointer as the key, which seems to be the address of the NSString Object and NSCachedEntry as value.</p><p style="font-family: helveticaneue;" class="">Though the pattern is of type String, it is stored in the NSCache as NSString. And since we are storing the NSCachedEntry objects in a dictionary indexed by the address (UnsafeRawPointer) of the NSString object, there is a new cache entry created for each iteration ( in the test case ) though the pattern string remains the same.</p><p style="font-family: helveticaneue;" class="">Can someone guide me about how to go about resolving this issue.</p><div class=""><br class=""></div></div></div></div></div></blockquote><br class="">Looks like you’ve done most of the analysis, so you’re already pretty much there. =)</div><div><br class=""></div><div>Is there some other way we could be caching the results here?</div><div><br class=""></div><div>- Tony<br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><div class=""><div class=""><p style="font-family: helveticaneue;" class="">Thank you.</p><p style="font-family: helveticaneue;" class="">- Nethra Ravindran</p><div class=""><br class="webkit-block-placeholder"></div></div></div></div>
_______________________________________________<br class="">swift-corelibs-dev mailing list<br class=""><a href="mailto:swift-corelibs-dev@swift.org" class="">swift-corelibs-dev@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-corelibs-dev<br class=""></div></blockquote></div><br class=""></div></div></body></html>