fastcsv-python 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- fastcsv_python-0.1.0/LICENSE +21 -0
- fastcsv_python-0.1.0/MANIFEST.in +5 -0
- fastcsv_python-0.1.0/PKG-INFO +95 -0
- fastcsv_python-0.1.0/README.md +66 -0
- fastcsv_python-0.1.0/fastcsv/__init__.py +42 -0
- fastcsv_python-0.1.0/fastcsv_python.egg-info/PKG-INFO +95 -0
- fastcsv_python-0.1.0/fastcsv_python.egg-info/SOURCES.txt +34 -0
- fastcsv_python-0.1.0/fastcsv_python.egg-info/dependency_links.txt +1 -0
- fastcsv_python-0.1.0/fastcsv_python.egg-info/requires.txt +9 -0
- fastcsv_python-0.1.0/fastcsv_python.egg-info/top_level.txt +1 -0
- fastcsv_python-0.1.0/pyproject.toml +3 -0
- fastcsv_python-0.1.0/setup.cfg +4 -0
- fastcsv_python-0.1.0/setup.py +58 -0
- fastcsv_python-0.1.0/src/columnar.c +125 -0
- fastcsv_python-0.1.0/src/columnar.h +43 -0
- fastcsv_python-0.1.0/src/fast_double.h +204 -0
- fastcsv_python-0.1.0/src/fastcsv.h +68 -0
- fastcsv_python-0.1.0/src/mmap_io.c +125 -0
- fastcsv_python-0.1.0/src/mmap_io.h +18 -0
- fastcsv_python-0.1.0/src/parser.c +406 -0
- fastcsv_python-0.1.0/src/parser.h +27 -0
- fastcsv_python-0.1.0/src/pymodule.c +966 -0
- fastcsv_python-0.1.0/src/simd.c +175 -0
- fastcsv_python-0.1.0/src/simd.h +27 -0
- fastcsv_python-0.1.0/src/simd_parse.h +276 -0
- fastcsv_python-0.1.0/src/type_infer.c +105 -0
- fastcsv_python-0.1.0/src/type_infer.h +23 -0
- fastcsv_python-0.1.0/tests/bench_simd.c +47 -0
- fastcsv_python-0.1.0/tests/test_columnar.c +147 -0
- fastcsv_python-0.1.0/tests/test_final.py +102 -0
- fastcsv_python-0.1.0/tests/test_mmap.c +28 -0
- fastcsv_python-0.1.0/tests/test_parallel.py +36 -0
- fastcsv_python-0.1.0/tests/test_parser.c +278 -0
- fastcsv_python-0.1.0/tests/test_python.py +46 -0
- fastcsv_python-0.1.0/tests/test_robustness.py +80 -0
- fastcsv_python-0.1.0/tests/test_type_infer.c +103 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: fastcsv-python
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Fast CSV parsing for Python via C + SIMD
|
|
5
|
+
Classifier: Programming Language :: Python :: 3
|
|
6
|
+
Classifier: Programming Language :: C
|
|
7
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
8
|
+
Classifier: Operating System :: POSIX :: Linux
|
|
9
|
+
Classifier: Operating System :: MacOS
|
|
10
|
+
Classifier: Operating System :: Microsoft :: Windows
|
|
11
|
+
Requires-Python: >=3.8
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
Requires-Dist: numpy>=1.20
|
|
15
|
+
Provides-Extra: bench
|
|
16
|
+
Requires-Dist: pandas; extra == "bench"
|
|
17
|
+
Requires-Dist: polars; extra == "bench"
|
|
18
|
+
Requires-Dist: pyarrow; extra == "bench"
|
|
19
|
+
Provides-Extra: arrow
|
|
20
|
+
Requires-Dist: pyarrow>=10.0; extra == "arrow"
|
|
21
|
+
Dynamic: classifier
|
|
22
|
+
Dynamic: description
|
|
23
|
+
Dynamic: description-content-type
|
|
24
|
+
Dynamic: license-file
|
|
25
|
+
Dynamic: provides-extra
|
|
26
|
+
Dynamic: requires-dist
|
|
27
|
+
Dynamic: requires-python
|
|
28
|
+
Dynamic: summary
|
|
29
|
+
|
|
30
|
+
# fastCSV
|
|
31
|
+
|
|
32
|
+
The fastest CSV parser for Python. Written in C with AVX2 SIMD acceleration,
|
|
33
|
+
memory-mapped I/O, and zero-copy columnar NumPy output.
|
|
34
|
+
|
|
35
|
+
## Benchmarks
|
|
36
|
+
|
|
37
|
+
| File | fastcsv | pandas | polars |
|
|
38
|
+
|------------------|----------|----------|----------|
|
|
39
|
+
| 1M rows mixed | 0.053s | 1.086s | 0.034s |
|
|
40
|
+
| 100k rows wide | 0.027s | 0.380s | 0.033s |
|
|
41
|
+
| 500k rows str | 0.020s | 0.935s | 0.015s |
|
|
42
|
+
|
|
43
|
+
Hardware: AMD Ryzen 5 7235HS
|
|
44
|
+
|
|
45
|
+
## Installation
|
|
46
|
+
|
|
47
|
+
pip install fastcsv-python
|
|
48
|
+
|
|
49
|
+
## Usage
|
|
50
|
+
|
|
51
|
+
```python
|
|
52
|
+
import fastcsv
|
|
53
|
+
|
|
54
|
+
# Returns dict of column_name -> numpy.ndarray
|
|
55
|
+
result = fastcsv.read_csv("data.csv")
|
|
56
|
+
result = fastcsv.read_csv("data.csv", delimiter=",", has_header=True, error_mode="strict")
|
|
57
|
+
|
|
58
|
+
# Streaming — constant memory regardless of file size
|
|
59
|
+
for chunk in fastcsv.reader("data.csv", chunk_size=10_000):
|
|
60
|
+
process(chunk)
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## Options
|
|
64
|
+
|
|
65
|
+
| Parameter | Default | Description |
|
|
66
|
+
|-----------|---------|-------------|
|
|
67
|
+
| `delimiter` | `","` | Field separator. Any single character. |
|
|
68
|
+
| `has_header` | `True` | Treat first row as column names. |
|
|
69
|
+
| `error_mode` | `"strict"` | `"strict"` / `"skip"` / `"replace"` |
|
|
70
|
+
| `chunk_size` | `10000` | Rows per chunk for `reader()`. |
|
|
71
|
+
|
|
72
|
+
## How it's fast
|
|
73
|
+
|
|
74
|
+
- **mmap I/O** — the OS pages in only what is needed. No `read()` syscalls.
|
|
75
|
+
- **AVX2 SIMD** — scans 32 bytes per cycle to find delimiters. Falls back to SSE4.2 (16 bytes) or scalar automatically.
|
|
76
|
+
- **Zero-copy output** — int and float columns are handed to NumPy directly from the C buffer. No data is copied.
|
|
77
|
+
- **Single-pass type inference** — column types resolved in one pass, never re-scanned.
|
|
78
|
+
- **GIL released** — the entire parse phase runs without the GIL. Safe for multi-threaded use.
|
|
79
|
+
|
|
80
|
+
## Error recovery
|
|
81
|
+
|
|
82
|
+
- `strict` — raises `ValueError` on any RFC 4180 violation.
|
|
83
|
+
- `skip` — silently drops malformed rows.
|
|
84
|
+
- `replace` — replaces malformed fields with empty string.
|
|
85
|
+
|
|
86
|
+
## Building from source
|
|
87
|
+
|
|
88
|
+
pip install numpy
|
|
89
|
+
pip install -e .
|
|
90
|
+
make test-all
|
|
91
|
+
|
|
92
|
+
## License
|
|
93
|
+
|
|
94
|
+
MIT
|
|
95
|
+
904c2c43-028b-4195-83f8-cd7f0dcb5d2b
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
# fastCSV
|
|
2
|
+
|
|
3
|
+
The fastest CSV parser for Python. Written in C with AVX2 SIMD acceleration,
|
|
4
|
+
memory-mapped I/O, and zero-copy columnar NumPy output.
|
|
5
|
+
|
|
6
|
+
## Benchmarks
|
|
7
|
+
|
|
8
|
+
| File | fastcsv | pandas | polars |
|
|
9
|
+
|------------------|----------|----------|----------|
|
|
10
|
+
| 1M rows mixed | 0.053s | 1.086s | 0.034s |
|
|
11
|
+
| 100k rows wide | 0.027s | 0.380s | 0.033s |
|
|
12
|
+
| 500k rows str | 0.020s | 0.935s | 0.015s |
|
|
13
|
+
|
|
14
|
+
Hardware: AMD Ryzen 5 7235HS
|
|
15
|
+
|
|
16
|
+
## Installation
|
|
17
|
+
|
|
18
|
+
pip install fastcsv-python
|
|
19
|
+
|
|
20
|
+
## Usage
|
|
21
|
+
|
|
22
|
+
```python
|
|
23
|
+
import fastcsv
|
|
24
|
+
|
|
25
|
+
# Returns dict of column_name -> numpy.ndarray
|
|
26
|
+
result = fastcsv.read_csv("data.csv")
|
|
27
|
+
result = fastcsv.read_csv("data.csv", delimiter=",", has_header=True, error_mode="strict")
|
|
28
|
+
|
|
29
|
+
# Streaming — constant memory regardless of file size
|
|
30
|
+
for chunk in fastcsv.reader("data.csv", chunk_size=10_000):
|
|
31
|
+
process(chunk)
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## Options
|
|
35
|
+
|
|
36
|
+
| Parameter | Default | Description |
|
|
37
|
+
|-----------|---------|-------------|
|
|
38
|
+
| `delimiter` | `","` | Field separator. Any single character. |
|
|
39
|
+
| `has_header` | `True` | Treat first row as column names. |
|
|
40
|
+
| `error_mode` | `"strict"` | `"strict"` / `"skip"` / `"replace"` |
|
|
41
|
+
| `chunk_size` | `10000` | Rows per chunk for `reader()`. |
|
|
42
|
+
|
|
43
|
+
## How it's fast
|
|
44
|
+
|
|
45
|
+
- **mmap I/O** — the OS pages in only what is needed. No `read()` syscalls.
|
|
46
|
+
- **AVX2 SIMD** — scans 32 bytes per cycle to find delimiters. Falls back to SSE4.2 (16 bytes) or scalar automatically.
|
|
47
|
+
- **Zero-copy output** — int and float columns are handed to NumPy directly from the C buffer. No data is copied.
|
|
48
|
+
- **Single-pass type inference** — column types resolved in one pass, never re-scanned.
|
|
49
|
+
- **GIL released** — the entire parse phase runs without the GIL. Safe for multi-threaded use.
|
|
50
|
+
|
|
51
|
+
## Error recovery
|
|
52
|
+
|
|
53
|
+
- `strict` — raises `ValueError` on any RFC 4180 violation.
|
|
54
|
+
- `skip` — silently drops malformed rows.
|
|
55
|
+
- `replace` — replaces malformed fields with empty string.
|
|
56
|
+
|
|
57
|
+
## Building from source
|
|
58
|
+
|
|
59
|
+
pip install numpy
|
|
60
|
+
pip install -e .
|
|
61
|
+
make test-all
|
|
62
|
+
|
|
63
|
+
## License
|
|
64
|
+
|
|
65
|
+
MIT
|
|
66
|
+
904c2c43-028b-4195-83f8-cd7f0dcb5d2b
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
try:
|
|
2
|
+
import pyarrow as pa
|
|
3
|
+
_HAS_ARROW = True
|
|
4
|
+
except ImportError:
|
|
5
|
+
_HAS_ARROW = False
|
|
6
|
+
|
|
7
|
+
from .fastcsv import read_csv as _read_csv_c, reader
|
|
8
|
+
|
|
9
|
+
__version__ = "0.1.0"
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def read_csv(path, delimiter=",", has_header=True, error_mode="strict", raw=False):
|
|
13
|
+
"""Read an entire CSV file into a dict of numpy arrays.
|
|
14
|
+
|
|
15
|
+
When pyarrow is available, string columns are returned as
|
|
16
|
+
pyarrow.LargeStringArray (zero-copy, lazy string creation).
|
|
17
|
+
Otherwise they are returned as numpy object arrays of Python str.
|
|
18
|
+
"""
|
|
19
|
+
use_arrow = _HAS_ARROW and not raw
|
|
20
|
+
result = _read_csv_c(
|
|
21
|
+
path,
|
|
22
|
+
delimiter=delimiter,
|
|
23
|
+
has_header=has_header,
|
|
24
|
+
error_mode=error_mode,
|
|
25
|
+
_arrow_strings=use_arrow,
|
|
26
|
+
raw=raw
|
|
27
|
+
)
|
|
28
|
+
if use_arrow:
|
|
29
|
+
for key in list(result.keys()):
|
|
30
|
+
val = result[key]
|
|
31
|
+
if isinstance(val, tuple) and len(val) == 2:
|
|
32
|
+
offsets, data = val
|
|
33
|
+
buf_offsets = pa.py_buffer(offsets)
|
|
34
|
+
buf_data = pa.py_buffer(data)
|
|
35
|
+
n = len(offsets) - 1
|
|
36
|
+
result[key] = pa.LargeStringArray.from_buffers(
|
|
37
|
+
n, buf_offsets, buf_data
|
|
38
|
+
)
|
|
39
|
+
return result
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
__all__ = ["read_csv", "reader", "__version__"]
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: fastcsv-python
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Fast CSV parsing for Python via C + SIMD
|
|
5
|
+
Classifier: Programming Language :: Python :: 3
|
|
6
|
+
Classifier: Programming Language :: C
|
|
7
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
8
|
+
Classifier: Operating System :: POSIX :: Linux
|
|
9
|
+
Classifier: Operating System :: MacOS
|
|
10
|
+
Classifier: Operating System :: Microsoft :: Windows
|
|
11
|
+
Requires-Python: >=3.8
|
|
12
|
+
Description-Content-Type: text/markdown
|
|
13
|
+
License-File: LICENSE
|
|
14
|
+
Requires-Dist: numpy>=1.20
|
|
15
|
+
Provides-Extra: bench
|
|
16
|
+
Requires-Dist: pandas; extra == "bench"
|
|
17
|
+
Requires-Dist: polars; extra == "bench"
|
|
18
|
+
Requires-Dist: pyarrow; extra == "bench"
|
|
19
|
+
Provides-Extra: arrow
|
|
20
|
+
Requires-Dist: pyarrow>=10.0; extra == "arrow"
|
|
21
|
+
Dynamic: classifier
|
|
22
|
+
Dynamic: description
|
|
23
|
+
Dynamic: description-content-type
|
|
24
|
+
Dynamic: license-file
|
|
25
|
+
Dynamic: provides-extra
|
|
26
|
+
Dynamic: requires-dist
|
|
27
|
+
Dynamic: requires-python
|
|
28
|
+
Dynamic: summary
|
|
29
|
+
|
|
30
|
+
# fastCSV
|
|
31
|
+
|
|
32
|
+
The fastest CSV parser for Python. Written in C with AVX2 SIMD acceleration,
|
|
33
|
+
memory-mapped I/O, and zero-copy columnar NumPy output.
|
|
34
|
+
|
|
35
|
+
## Benchmarks
|
|
36
|
+
|
|
37
|
+
| File | fastcsv | pandas | polars |
|
|
38
|
+
|------------------|----------|----------|----------|
|
|
39
|
+
| 1M rows mixed | 0.053s | 1.086s | 0.034s |
|
|
40
|
+
| 100k rows wide | 0.027s | 0.380s | 0.033s |
|
|
41
|
+
| 500k rows str | 0.020s | 0.935s | 0.015s |
|
|
42
|
+
|
|
43
|
+
Hardware: AMD Ryzen 5 7235HS
|
|
44
|
+
|
|
45
|
+
## Installation
|
|
46
|
+
|
|
47
|
+
pip install fastcsv-python
|
|
48
|
+
|
|
49
|
+
## Usage
|
|
50
|
+
|
|
51
|
+
```python
|
|
52
|
+
import fastcsv
|
|
53
|
+
|
|
54
|
+
# Returns dict of column_name -> numpy.ndarray
|
|
55
|
+
result = fastcsv.read_csv("data.csv")
|
|
56
|
+
result = fastcsv.read_csv("data.csv", delimiter=",", has_header=True, error_mode="strict")
|
|
57
|
+
|
|
58
|
+
# Streaming — constant memory regardless of file size
|
|
59
|
+
for chunk in fastcsv.reader("data.csv", chunk_size=10_000):
|
|
60
|
+
process(chunk)
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## Options
|
|
64
|
+
|
|
65
|
+
| Parameter | Default | Description |
|
|
66
|
+
|-----------|---------|-------------|
|
|
67
|
+
| `delimiter` | `","` | Field separator. Any single character. |
|
|
68
|
+
| `has_header` | `True` | Treat first row as column names. |
|
|
69
|
+
| `error_mode` | `"strict"` | `"strict"` / `"skip"` / `"replace"` |
|
|
70
|
+
| `chunk_size` | `10000` | Rows per chunk for `reader()`. |
|
|
71
|
+
|
|
72
|
+
## How it's fast
|
|
73
|
+
|
|
74
|
+
- **mmap I/O** — the OS pages in only what is needed. No `read()` syscalls.
|
|
75
|
+
- **AVX2 SIMD** — scans 32 bytes per cycle to find delimiters. Falls back to SSE4.2 (16 bytes) or scalar automatically.
|
|
76
|
+
- **Zero-copy output** — int and float columns are handed to NumPy directly from the C buffer. No data is copied.
|
|
77
|
+
- **Single-pass type inference** — column types resolved in one pass, never re-scanned.
|
|
78
|
+
- **GIL released** — the entire parse phase runs without the GIL. Safe for multi-threaded use.
|
|
79
|
+
|
|
80
|
+
## Error recovery
|
|
81
|
+
|
|
82
|
+
- `strict` — raises `ValueError` on any RFC 4180 violation.
|
|
83
|
+
- `skip` — silently drops malformed rows.
|
|
84
|
+
- `replace` — replaces malformed fields with empty string.
|
|
85
|
+
|
|
86
|
+
## Building from source
|
|
87
|
+
|
|
88
|
+
pip install numpy
|
|
89
|
+
pip install -e .
|
|
90
|
+
make test-all
|
|
91
|
+
|
|
92
|
+
## License
|
|
93
|
+
|
|
94
|
+
MIT
|
|
95
|
+
904c2c43-028b-4195-83f8-cd7f0dcb5d2b
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
MANIFEST.in
|
|
3
|
+
README.md
|
|
4
|
+
pyproject.toml
|
|
5
|
+
setup.py
|
|
6
|
+
fastcsv/__init__.py
|
|
7
|
+
fastcsv_python.egg-info/PKG-INFO
|
|
8
|
+
fastcsv_python.egg-info/SOURCES.txt
|
|
9
|
+
fastcsv_python.egg-info/dependency_links.txt
|
|
10
|
+
fastcsv_python.egg-info/requires.txt
|
|
11
|
+
fastcsv_python.egg-info/top_level.txt
|
|
12
|
+
src/columnar.c
|
|
13
|
+
src/columnar.h
|
|
14
|
+
src/fast_double.h
|
|
15
|
+
src/fastcsv.h
|
|
16
|
+
src/mmap_io.c
|
|
17
|
+
src/mmap_io.h
|
|
18
|
+
src/parser.c
|
|
19
|
+
src/parser.h
|
|
20
|
+
src/pymodule.c
|
|
21
|
+
src/simd.c
|
|
22
|
+
src/simd.h
|
|
23
|
+
src/simd_parse.h
|
|
24
|
+
src/type_infer.c
|
|
25
|
+
src/type_infer.h
|
|
26
|
+
tests/bench_simd.c
|
|
27
|
+
tests/test_columnar.c
|
|
28
|
+
tests/test_final.py
|
|
29
|
+
tests/test_mmap.c
|
|
30
|
+
tests/test_parallel.py
|
|
31
|
+
tests/test_parser.c
|
|
32
|
+
tests/test_python.py
|
|
33
|
+
tests/test_robustness.py
|
|
34
|
+
tests/test_type_infer.c
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
fastcsv
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import os, sys, platform
|
|
2
|
+
from setuptools import setup, Extension
|
|
3
|
+
from setuptools.command.build_ext import build_ext
|
|
4
|
+
import numpy as np
|
|
5
|
+
|
|
6
|
+
class BuildExt(build_ext):
|
|
7
|
+
def build_extensions(self):
|
|
8
|
+
ct = self.compiler.compiler_type
|
|
9
|
+
for ext in self.extensions:
|
|
10
|
+
if ct == 'msvc':
|
|
11
|
+
ext.extra_compile_args = ['/O2', '/arch:AVX2', '/DNDEBUG']
|
|
12
|
+
else:
|
|
13
|
+
ext.extra_compile_args = [
|
|
14
|
+
'-O3', '-std=c11', '-DNDEBUG',
|
|
15
|
+
'-Wall', '-Wextra',
|
|
16
|
+
'-funroll-loops',
|
|
17
|
+
'-fno-math-errno',
|
|
18
|
+
'-fomit-frame-pointer',
|
|
19
|
+
]
|
|
20
|
+
super().build_extensions()
|
|
21
|
+
|
|
22
|
+
ext = Extension(
|
|
23
|
+
"fastcsv.fastcsv",
|
|
24
|
+
sources=[
|
|
25
|
+
"src/pymodule.c",
|
|
26
|
+
"src/parser.c",
|
|
27
|
+
"src/mmap_io.c",
|
|
28
|
+
"src/simd.c",
|
|
29
|
+
"src/type_infer.c",
|
|
30
|
+
"src/columnar.c",
|
|
31
|
+
],
|
|
32
|
+
include_dirs=["src", np.get_include()],
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
setup(
|
|
36
|
+
name="fastcsv-python",
|
|
37
|
+
version="0.1.0",
|
|
38
|
+
description="Fast CSV parsing for Python via C + SIMD",
|
|
39
|
+
long_description=open("README.md").read(),
|
|
40
|
+
long_description_content_type="text/markdown",
|
|
41
|
+
packages=["fastcsv"],
|
|
42
|
+
ext_modules=[ext],
|
|
43
|
+
cmdclass={"build_ext": BuildExt},
|
|
44
|
+
python_requires=">=3.8",
|
|
45
|
+
install_requires=["numpy>=1.20"],
|
|
46
|
+
extras_require={
|
|
47
|
+
"bench": ["pandas", "polars", "pyarrow"],
|
|
48
|
+
"arrow": ["pyarrow>=10.0"],
|
|
49
|
+
},
|
|
50
|
+
classifiers=[
|
|
51
|
+
"Programming Language :: Python :: 3",
|
|
52
|
+
"Programming Language :: C",
|
|
53
|
+
"License :: OSI Approved :: MIT License",
|
|
54
|
+
"Operating System :: POSIX :: Linux",
|
|
55
|
+
"Operating System :: MacOS",
|
|
56
|
+
"Operating System :: Microsoft :: Windows",
|
|
57
|
+
],
|
|
58
|
+
)
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
#include "columnar.h"
|
|
2
|
+
#include <stdlib.h>
|
|
3
|
+
#include <string.h>
|
|
4
|
+
|
|
5
|
+
#define INITIAL_ARENA_CAP 65536
|
|
6
|
+
|
|
7
|
+
ColumnarTable *columnar_new(uint32_t num_cols, uint64_t row_capacity) {
|
|
8
|
+
if (row_capacity == 0) row_capacity = 4096;
|
|
9
|
+
|
|
10
|
+
ColumnarTable *t = calloc(1, sizeof(ColumnarTable));
|
|
11
|
+
if (!t) return NULL;
|
|
12
|
+
|
|
13
|
+
t->num_cols = num_cols;
|
|
14
|
+
if (num_cols == 0) return t;
|
|
15
|
+
|
|
16
|
+
t->cols = calloc(num_cols, sizeof(Column));
|
|
17
|
+
if (!t->cols) {
|
|
18
|
+
free(t);
|
|
19
|
+
return NULL;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
for (uint32_t i = 0; i < num_cols; i++) {
|
|
23
|
+
Column *c = &t->cols[i];
|
|
24
|
+
c->type = COL_TYPE_INT; // default
|
|
25
|
+
c->capacity = row_capacity;
|
|
26
|
+
|
|
27
|
+
c->int_data = malloc(row_capacity * sizeof(int64_t));
|
|
28
|
+
c->float_data = malloc(row_capacity * sizeof(double));
|
|
29
|
+
c->str_offsets = malloc(row_capacity * sizeof(uint32_t));
|
|
30
|
+
c->str_lens = malloc(row_capacity * sizeof(uint32_t));
|
|
31
|
+
|
|
32
|
+
c->arena_cap = INITIAL_ARENA_CAP;
|
|
33
|
+
c->str_arena = malloc(INITIAL_ARENA_CAP);
|
|
34
|
+
|
|
35
|
+
if (!c->int_data || !c->float_data || !c->str_offsets || !c->str_lens || !c->str_arena) {
|
|
36
|
+
columnar_free(t);
|
|
37
|
+
return NULL;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return t;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
void columnar_free(ColumnarTable *t) {
|
|
45
|
+
if (!t) return;
|
|
46
|
+
if (t->cols) {
|
|
47
|
+
for (uint32_t i = 0; i < t->num_cols; i++) {
|
|
48
|
+
Column *c = &t->cols[i];
|
|
49
|
+
free(c->int_data);
|
|
50
|
+
free(c->float_data);
|
|
51
|
+
free(c->str_offsets);
|
|
52
|
+
free(c->str_lens);
|
|
53
|
+
free(c->str_arena);
|
|
54
|
+
}
|
|
55
|
+
free(t->cols);
|
|
56
|
+
}
|
|
57
|
+
free(t);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
int columnar_append_row(ColumnarTable *t, CsvField *fields, uint32_t num_fields) {
|
|
61
|
+
for (uint32_t i = 0; i < t->num_cols; i++) {
|
|
62
|
+
Column *c = &t->cols[i];
|
|
63
|
+
|
|
64
|
+
if (c->len >= c->capacity) {
|
|
65
|
+
uint64_t new_cap = c->capacity * 2;
|
|
66
|
+
if (new_cap == 0) new_cap = 4096;
|
|
67
|
+
|
|
68
|
+
void *p1 = realloc(c->int_data, new_cap * sizeof(int64_t));
|
|
69
|
+
void *p2 = realloc(c->float_data, new_cap * sizeof(double));
|
|
70
|
+
void *p3 = realloc(c->str_offsets, new_cap * sizeof(uint32_t));
|
|
71
|
+
void *p4 = realloc(c->str_lens, new_cap * sizeof(uint32_t));
|
|
72
|
+
|
|
73
|
+
if (!p1 || !p2 || !p3 || !p4) {
|
|
74
|
+
if (p1) c->int_data = p1;
|
|
75
|
+
if (p2) c->float_data = p2;
|
|
76
|
+
if (p3) c->str_offsets = p3;
|
|
77
|
+
if (p4) c->str_lens = p4;
|
|
78
|
+
return -1;
|
|
79
|
+
}
|
|
80
|
+
c->int_data = p1;
|
|
81
|
+
c->float_data = p2;
|
|
82
|
+
c->str_offsets = p3;
|
|
83
|
+
c->str_lens = p4;
|
|
84
|
+
c->capacity = new_cap;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
CsvField empty_field = {"", 0, 0};
|
|
88
|
+
const CsvField *f = (i < num_fields) ? &fields[i] : &empty_field;
|
|
89
|
+
|
|
90
|
+
type_infer_update(&c->type, f);
|
|
91
|
+
|
|
92
|
+
int64_t i_val = 0;
|
|
93
|
+
double d_val = 0.0;
|
|
94
|
+
const char *s_val = NULL;
|
|
95
|
+
|
|
96
|
+
type_infer_parse(c->type, f, &i_val, &d_val, &s_val);
|
|
97
|
+
|
|
98
|
+
c->int_data[c->len] = i_val;
|
|
99
|
+
c->float_data[c->len] = d_val;
|
|
100
|
+
c->str_offsets[c->len] = 0;
|
|
101
|
+
c->str_lens[c->len] = 0;
|
|
102
|
+
|
|
103
|
+
if (c->type == COL_TYPE_STR) {
|
|
104
|
+
size_t needed = c->arena_len + f->len;
|
|
105
|
+
if (needed > c->arena_cap) {
|
|
106
|
+
size_t new_cap = c->arena_cap * 2;
|
|
107
|
+
if (new_cap < needed) new_cap = needed * 2;
|
|
108
|
+
void *p = realloc(c->str_arena, new_cap);
|
|
109
|
+
if (!p) return -1;
|
|
110
|
+
c->str_arena = p;
|
|
111
|
+
c->arena_cap = new_cap;
|
|
112
|
+
}
|
|
113
|
+
c->str_offsets[c->len] = (uint32_t)c->arena_len;
|
|
114
|
+
c->str_lens[c->len] = (uint32_t)f->len;
|
|
115
|
+
if (f->len > 0 && s_val) {
|
|
116
|
+
memcpy(c->str_arena + c->arena_len, s_val, f->len);
|
|
117
|
+
}
|
|
118
|
+
c->arena_len += f->len;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
c->len++;
|
|
122
|
+
}
|
|
123
|
+
t->num_rows++;
|
|
124
|
+
return 0;
|
|
125
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
#ifndef FASTCSV_COLUMNAR_H
|
|
2
|
+
#define FASTCSV_COLUMNAR_H
|
|
3
|
+
|
|
4
|
+
#include <stdint.h>
|
|
5
|
+
#include <stddef.h>
|
|
6
|
+
#include "type_infer.h"
|
|
7
|
+
#include "fastcsv.h"
|
|
8
|
+
|
|
9
|
+
typedef struct {
|
|
10
|
+
ColType type;
|
|
11
|
+
uint64_t len; /* rows filled */
|
|
12
|
+
uint64_t capacity; /* rows allocated */
|
|
13
|
+
|
|
14
|
+
int64_t *int_data;
|
|
15
|
+
double *float_data;
|
|
16
|
+
|
|
17
|
+
/* STR columns: offsets into str_arena */
|
|
18
|
+
uint32_t *str_offsets; /* str_offsets[i] = start of row i in str_arena */
|
|
19
|
+
uint32_t *str_lens; /* byte length of each string */
|
|
20
|
+
char *str_arena; /* flat byte buffer, all strings packed */
|
|
21
|
+
size_t arena_len;
|
|
22
|
+
size_t arena_cap;
|
|
23
|
+
} Column;
|
|
24
|
+
|
|
25
|
+
typedef struct {
|
|
26
|
+
Column *cols;
|
|
27
|
+
uint32_t num_cols;
|
|
28
|
+
uint64_t num_rows;
|
|
29
|
+
} ColumnarTable;
|
|
30
|
+
|
|
31
|
+
/* Allocate table with num_cols columns, initial row capacity. */
|
|
32
|
+
ColumnarTable *columnar_new(uint32_t num_cols, uint64_t row_capacity);
|
|
33
|
+
|
|
34
|
+
/* Append one parsed row. Grows buffers if needed.
|
|
35
|
+
fields[i] is matched to cols[i].
|
|
36
|
+
Runs type_infer_update then writes value into the correct buffer. */
|
|
37
|
+
int columnar_append_row(ColumnarTable *t, CsvField *fields,
|
|
38
|
+
uint32_t num_fields);
|
|
39
|
+
|
|
40
|
+
/* Free everything. Safe with NULL. */
|
|
41
|
+
void columnar_free(ColumnarTable *t);
|
|
42
|
+
|
|
43
|
+
#endif
|