-
-
Notifications
You must be signed in to change notification settings - Fork 20k
PERF: Use SIMD for read_csv C tokenizer #64515
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
Open
jbrockmendel
wants to merge
15
commits into
pandas-dev:main
Choose a base branch
from
jbrockmendel:perf-read_csv-simd
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
300e26c
build(simd): add xsimd dependency and simd verification
Alvaro-Kothe 1022e10
Update pandas/_libs/meson.build
Alvaro-Kothe ed588ad
ci: remove no simd job
Alvaro-Kothe 84a4982
refactor: create arch specific loop
Alvaro-Kothe aaf025b
refactor: move configuration set inside main verification
Alvaro-Kothe 0fefde7
fix: bump xsimd to 14.2 for MSVC ARM support
Alvaro-Kothe effdd43
build: remove simd option
Alvaro-Kothe 95c7fad
PERF: Use SIMD for read_csv C tokenizer
jbrockmendel 44c3f86
BLD: Fix MSVC build for SIMD tokenizer by replacing __builtin_ctz
jbrockmendel 94cb290
CLN: Address review comments on SIMD tokenizer
jbrockmendel b097535
BLD: Fix -Werror build failure from tautological char < 256 comparison
jbrockmendel e384990
CLN: Address review comments on SIMD tokenizer
jbrockmendel ee47d11
BLD: Fix MSVC build for SIMD tokenizer on Windows ARM64 by replacing …
jbrockmendel f4c9046
REF: Use xsimd library for read_csv SIMD tokenizer
jbrockmendel 615a2b4
BLD: Link simd_scan into tslibs/parsing extension
jbrockmendel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| Copyright (c) 2016, Johan Mabille, Sylvain Corlay, Wolf Vollprecht and Martin Renou | ||
| Copyright (c) 2016, QuantStack | ||
| Copyright (c) 2018, Serge Guelton | ||
| All rights reserved. | ||
|
|
||
| Redistribution and use in source and binary forms, with or without | ||
| modification, are permitted provided that the following conditions are met: | ||
|
|
||
| * Redistributions of source code must retain the above copyright notice, this | ||
| list of conditions and the following disclaimer. | ||
|
|
||
| * Redistributions in binary form must reproduce the above copyright notice, | ||
| this list of conditions and the following disclaimer in the documentation | ||
| and/or other materials provided with the distribution. | ||
|
|
||
| * Neither the name of the copyright holder nor the names of its | ||
| contributors may be used to endorse or promote products derived from | ||
| this software without specific prior written permission. | ||
|
|
||
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
| AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
| DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | ||
| FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
| SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
| CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
| OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
| OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| /* | ||
| Copyright (c) 2026, PyData Development Team | ||
| All rights reserved. | ||
|
|
||
| Distributed under the terms of the BSD Simplified License. | ||
|
|
||
| The full license is in the LICENSE file, distributed with this software. | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <stddef.h> | ||
|
|
||
| #ifdef __cplusplus | ||
| extern "C" { | ||
| #endif | ||
|
|
||
| // Minimum bytes the scanner can process in one call. Callers should | ||
| // fall through to the scalar path when fewer bytes remain. | ||
| #define PD_SCAN_MIN_BYTES 16 | ||
|
|
||
| typedef struct pd_scanner pd_scanner; | ||
|
|
||
| // Build a scanner that halts on any of `n` special bytes. Supported | ||
| // values for `n` are 2 (quoted-field scan) and 6 (unquoted-field scan). | ||
| // Returns NULL on allocation failure or unsupported `n`. | ||
| pd_scanner *pd_scanner_create(const char *chars, int n); | ||
|
|
||
| // Free a scanner. Accepts NULL. | ||
| void pd_scanner_destroy(pd_scanner *scanner); | ||
|
|
||
| // Returns the byte offset of the first special char in data[0..len), | ||
| // or `len` if no special char was found within full SIMD chunks. The | ||
| // trailing <PD_SCAN_MIN_BYTES bytes are not scanned; the caller's | ||
| // scalar fallback handles them. | ||
| size_t pd_scanner_scan(const pd_scanner *scanner, const char *data, size_t len); | ||
|
|
||
| #ifdef __cplusplus | ||
| } | ||
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| # All architectures we might support | ||
| # Key is the architecture name used in file suffixes and macros | ||
| is_msvc_syntax = cxx.get_argument_syntax() == 'msvc' | ||
| simd_x86_flags = { | ||
| 'sse2': is_msvc_syntax ? ['/arch:SSE2'] : ['-msse2'], | ||
| 'avx2': is_msvc_syntax ? ['/arch:AVX2'] : ['-mavx2'], | ||
| 'avx512cd': is_msvc_syntax ? ['/arch:AVX512'] : ['-mavx512cd'], | ||
| } | ||
|
|
||
| simd_config = configuration_data() | ||
| supported_simd_archs = {} | ||
| if host_machine.cpu_family() == 'aarch64' | ||
| supported_simd_archs += {'neon': []} | ||
| simd_config.set('PANDAS_HAVE_NEON', 1) | ||
| elif host_machine.cpu_family() in ['x86', 'x86_64'] | ||
| foreach name, flags : simd_x86_flags | ||
| if cxx.has_multi_arguments(flags) | ||
| supported_simd_archs += {name: flags} | ||
| simd_config.set('PANDAS_HAVE_@0@'.format(name.to_upper()), 1) | ||
| endif | ||
| endforeach | ||
| endif | ||
|
|
||
| # Ensure scalar version on all architectures for now... | ||
| simd_config.set('PANDAS_HAVE_SCALAR', 1) | ||
|
|
||
| configure_file( | ||
| output: 'pandas_simd_config.h', | ||
| configuration: simd_config, | ||
| ) | ||
|
|
||
| simd_config_inc = include_directories('.') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| /* | ||
| Copyright (c) 2026, PyData Development Team | ||
| All rights reserved. | ||
|
|
||
| Distributed under the terms of the BSD Simplified License. | ||
|
|
||
| The full license is in the LICENSE file, distributed with this software. | ||
| */ | ||
|
|
||
| #include "pandas/parser/simd_scan.h" | ||
|
|
||
| #include <xsimd/xsimd.hpp> | ||
|
|
||
| #include <cstdint> | ||
| #include <new> | ||
|
|
||
| #if defined(_MSC_VER) | ||
| # include <intrin.h> | ||
| #endif | ||
|
|
||
| namespace { | ||
|
|
||
| using batch_u8 = xsimd::batch<std::uint8_t>; | ||
| constexpr std::size_t kStep = batch_u8::size; | ||
|
|
||
| static_assert(kStep >= PD_SCAN_MIN_BYTES, | ||
| "xsimd batch<uint8_t> must be at least 16 lanes wide"); | ||
|
|
||
| static inline unsigned ctz64(std::uint64_t value) { | ||
| #if defined(_MSC_VER) | ||
| unsigned long index; | ||
| _BitScanForward64(&index, value); | ||
| return static_cast<unsigned>(index); | ||
| #else | ||
| return static_cast<unsigned>(__builtin_ctzll(value)); | ||
| #endif | ||
| } | ||
|
|
||
| template <int N> | ||
| static inline std::size_t scan_impl(const batch_u8 *v, const char *data, | ||
| std::size_t len) { | ||
| const auto *p = reinterpret_cast<const std::uint8_t *>(data); | ||
| std::size_t i = 0; | ||
| for (; i + kStep <= len; i += kStep) { | ||
| const auto chunk = batch_u8::load_unaligned(p + i); | ||
| auto mask = (chunk == v[0]); | ||
| for (int j = 1; j < N; ++j) { | ||
| mask = mask | (chunk == v[j]); | ||
| } | ||
| if (xsimd::any(mask)) { | ||
| return i + ctz64(mask.mask()); | ||
| } | ||
| } | ||
| return i; | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| struct pd_scanner { | ||
| batch_u8 v[6]; | ||
| int n; | ||
| }; | ||
|
|
||
| extern "C" { | ||
|
|
||
| pd_scanner *pd_scanner_create(const char *chars, int n) { | ||
| if (n != 2 && n != 6) | ||
| return nullptr; | ||
| auto *scanner = new (std::nothrow) pd_scanner; | ||
| if (!scanner) | ||
| return nullptr; | ||
| scanner->n = n; | ||
| for (int j = 0; j < n; ++j) { | ||
| scanner->v[j] = batch_u8::broadcast(static_cast<std::uint8_t>(chars[j])); | ||
| } | ||
| return scanner; | ||
| } | ||
|
|
||
| void pd_scanner_destroy(pd_scanner *scanner) { delete scanner; } | ||
|
|
||
| size_t pd_scanner_scan(const pd_scanner *scanner, const char *data, | ||
| size_t len) { | ||
| switch (scanner->n) { | ||
| case 2: | ||
| return scan_impl<2>(scanner->v, data, len); | ||
| case 6: | ||
| return scan_impl<6>(scanner->v, data, len); | ||
| } | ||
| return len; | ||
| } | ||
|
|
||
| } // extern "C" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| project( | ||
| 'xsimd', | ||
| 'cpp', | ||
| meson_version: '>=0.58.0', | ||
| license: 'BSD-3-Clause', | ||
| version: '14.2.0', | ||
| ) | ||
|
|
||
| xsimd_inc = include_directories('include') | ||
|
|
||
| xsimd_dep = declare_dependency(include_directories: xsimd_inc) | ||
| meson.override_dependency('xsimd', xsimd_dep) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| [wrap-file] | ||
| directory = xsimd-14.2.0 | ||
| source_url = https://github.com/xtensor-stack/xsimd/archive/refs/tags/14.2.0.tar.gz | ||
| source_filename = xsimd-14.2.0.tar.gz | ||
| source_hash = 21e841ab684b05331e81e7f782431753a029ef7b7d9d6d3ddab837e7782a40ee | ||
| patch_directory = xsimd | ||
|
|
||
| [provide] | ||
| dependency_names = xsimd |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Alvaro-Kothe is going through a similar exercise on his SIMD PR and I gave the feedback that this type of looping + bit fiddling is extremely inefficient. nanoarrow has built-in functionality to do this better and it sounds like @Alvaro-Kothe is researching something with xsimd (?) - let's keep tabs on that
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this was a problem with the non-xsimd variant? That felt lower-touch to me and I marginally prefer it.