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

[SR-6477] UnsafeBufferPointer should have a stack allocating version (or at least a conditional stack allocation) #49027

Open
phausler opened this issue Nov 26, 2017 · 4 comments
Labels
new feature standard library Area: Standard library umbrella

Comments

@phausler
Copy link
Member

Previous ID SR-6477
Radar rdar://problem/70151059
Original Reporter @phausler
Type New Feature
Additional Detail from JIRA
Votes 0
Component/s Standard Library
Labels New Feature
Assignee None
Priority Medium

md5: 6857a631645c8afe8fb362159661c0eb

relates to:

  • SR-13570 UnsafeMutable*Pointer should stack-promote allocations (like malloc)

Issue Description:

often times you just need an allocation for a single frame and dont need to actually heap allocate. alloca or stack allocation is considerably faster than malloc/heap allocation. There should be a way to indicate either by inference of usage or by explicit call that the compiler should be able to emit a stack allocated memory buffer.

Often times as a work-around a tuple is used to accommodate stack allocation but this is not very ergonomic and can cause some severe cargo-cult-ed patterns to be used. We should have a cleaner and better answer than this.

@phausler
Copy link
Member Author

at least on Darwin and linux based platforms the main thread has additional stack space available so a limit on the allocation can be conditional on determination of the main thread.

Here is an example of something similar that is used in CoreFoundation

#include <pthread.h>
#include <alloca.h>

static void withStackOrHeapBuffer(size_t amount, void (^perform)(void *)) {
    void *stackBuffer = pthread_main_np() == 1 ? (amount < 2048 ? alloca(amount) : nil) : (amount < 512 ? alloca(amount) : nil)
    if (stackBuffer == nil) {
        void *heapBuffer = malloc(amount);
        perform(heapBuffer);
        free(heapBuffer);
    } else {
        perform(stackBuffer);
    }
}

@belkadan
Copy link
Contributor

I mocked up an implementation of this and posted it on Twitter a few weeks ago. If we were to really do it we'd probably want to add a new builtin instead of relying on C, so we could optimize it.

@phausler
Copy link
Member Author

phausler commented Dec 4, 2017

the problem with that approach (which is currently used in Foundation's overlay via the _withStackOrHeapBuffer helper function is that we cannot rethrow...

@typesanitizer
Copy link

@swift-ci create

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new feature standard library Area: Standard library umbrella
Projects
None yet
Development

No branches or pull requests

3 participants