[swift-dev] large dictionary literal overflows swiftc stack

Rafkind, Jon jon.rafkind at hpe.com
Wed Mar 23 13:56:50 CDT 2016

I ran a test to see the maximum number of elements swift could handle before crashing and the numbers are

swift 2.2: 1213
swift 3-dev (mar 16 snapshot): 976

I don't know why swift 3 is lower than swift 2.2, but I'm guessing its due to the same underlying cause, which is running out of stack space. If the swift 3 snapshot was compiled with different flags than 2.2 then swift 3 could have different stack usage properties, and thus run out of stack space quicker.

Here is a python script that generates a dictionary literal with some number of elements and runs swift on it, then does binary search to find the maximum number of elements before swift crashes.

#!/usr/bin/env python

def make_swift(n):
    def element(x):
        return '"x%d": "0"' % x

    data = "let n = [";
    data += ',\n'.join([element(x) for x in xrange(0, n)])
    data += "]"

    path = 'test-%d.swift' % n
    file = open(path, 'w')
    return path

def translate(path):
    print "Testing %s" % path
    import subprocess
    out = subprocess.call(['swift', path])
    if out == 0:
        print "  ok"
        print "  failed"
    return out == 0

def test(n):
    path = make_swift(n)
    return translate(path)

def binary_search(low, high):
    while low < high:
        middle = (low + high) / 2
        if middle == low:
            low = high
            middle = high
        if test(middle):
            low = middle
            high = middle
    return middle


low = 1
high = 10
while test(high):
    low = high
    high *= 2

last_failed = binary_search(low, high)
print "Failed at %d" % last_failed

On 03/22/2016 09:03 PM, Kyle Jessup via swift-dev wrote:

Ok I will test with swift 3, but just to avoid any confusion I am not a developer on PerfectLib.

I am! Admittedly, that dictionary contains many obsolete mime type mappings which could be pruned (anyone serving Lotus 1-2-3 files?). However, 816 items is not an absurdly large number so it’s likely someone else would have run into this in the near future.

The code does successfully compile for me using the release 2.2 version on my VMWare based Ubuntu 15 system. It also compiles using 3.0.


I was just using that file as a test case for my application that is based on the swiftc code base. My application is designed to consume arbitrary swift 2.2 code. If there is a problem with swift 3 then I suppose it can be fixed, but if swift 3 has no issues then it looks like I have few options for remediation.

On 03/22/2016 12:56 PM, Dmitri Gribenko wrote:

On Tue, Mar 22, 2016 at 12:17 PM, Rafkind, Jon via swift-dev
<swift-dev at swift.org><mailto:swift-dev at swift.org><mailto:swift-dev at swift.org><mailto:swift-dev at swift.org>wrote:

I have to support swift 2.2 for the time being because I have to support the current release of xcode. I will upgrade to swift 3 when it is released.

I understand your motivation, but I would still recommend trying to
update your code (on a branch) to Swift 3. This way you will get a
preview of the changes, would be able to provide feedback, and maybe
even find issues with the changes that we are making before Swift 3 is
finalized in a release. There is benefit for both your library and
the Swift community.



swift-dev mailing list
swift-dev at swift.org<mailto:swift-dev at swift.org>


More information about the swift-dev mailing list