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

Implement _customIndexOfEquatableElement for UnsafeRawBufferPointer #63200

Open
glessard opened this issue Jan 24, 2023 · 7 comments · Fixed by #63106 · May be fixed by #63128
Open

Implement _customIndexOfEquatableElement for UnsafeRawBufferPointer #63200

glessard opened this issue Jan 24, 2023 · 7 comments · Fixed by #63106 · May be fixed by #63128
Assignees
Labels
feature A feature request or implementation standard library Area: Standard library umbrella

Comments

@glessard
Copy link
Contributor

UnsafeRawBufferPointer and UnsafeMutableRawBufferPointer perform element-by-element scans in firstIndex(of: Element) and lastIndex(of: Element). It appears that these types should be amenable to some improvements, by using vectorized loops.

Motivation
UnsafeRawBufferPointer.firstIndex(of:) might be faster and/or more efficient with a specialized implementation.

Solution
Benchmarks should be created, and then implementations of the Collection customization points _customIndexOfEquatableElement() and _customLastIndexOfEquatableElement() should be investigated.

Alternatives considered
If no substantial improvements are found, then drop it. That fact should be documented so that someone else doesn't repeat this work in the future.

@glessard
Copy link
Contributor Author

@valeriyvan you can assign yourself this issue if you want; I couldn't do it myself.

@valeriyvan
Copy link
Contributor

@valeriyvan you can assign yourself this issue if you want; I couldn't do it myself.

I cannot assign myself to this issue also.

@glessard
Copy link
Contributor Author

glessard commented Jan 25, 2023

It occurred to a colleague and I that platforms usually have well-tuned implementations of the forward version of this algorithm in the form of the memchr function from libc. memchr is an expected dependency for the swift build, so we can always use it. It would be more economical in effort and code size to call that from _customIndexOfEquatableElement(). (The LLVM repository contains hand-tuned assembly versions contributed by ARM.)

The reverse algorithm is a different story, but we can gauge tuning success against a forward version that forwards to memchr.

@valeriyvan
Copy link
Contributor

It occurred to a colleague and I that platforms usually have well-tuned implementations of the forward version of this algorithm in the form of the memchr function from libc. memchr is an expected dependency for the swift build, so we can always use it. It would be more economical in effort and code size to call that from _customIndexOfEquatableElement(). (The LLVM repository contains hand-tuned assembly versions contributed by ARM.)

I thought usage of libc is discouraged. Is there any guidelines what and when could be used from libc? What about intrinsics? Could intrinsics be used?

The reverse algorithm is a different story, but we can gauge tuning success against a forward version that forwards to memchr.

What about memrchr?

@tbkka
Copy link
Contributor

tbkka commented Jan 26, 2023

I thought usage of libc is discouraged. Is there any guidelines what and when could be used from libc? What about intrinsics? Could intrinsics be used?

It is strongly discouraged for everyone except the Swift standard library.

@glessard
Copy link
Contributor Author

What about memrchr?

memrchr isn't part of the approved/necessary set, which can be found in utils/check_freestanding_dependencies.py. It also isn't part of the C standard, which would make it an unreliable dependency.

@glessard
Copy link
Contributor Author

glessard commented Feb 1, 2023

Re-opening, since getting the new benchmark merged is only the first step.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature A feature request or implementation standard library Area: Standard library umbrella
Projects
None yet
3 participants