Re: Speed up COPY FROM text/CSV parsing using SIMD - Mailing list pgsql-hackers
From | Shinya Kato |
---|---|
Subject | Re: Speed up COPY FROM text/CSV parsing using SIMD |
Date | |
Msg-id | CAOzEurR5nFt=-SijfU7y0BHVcrT6RG9ovvdVfKt_uBZfEQew9w@mail.gmail.com Whole thread Raw |
In response to | Re: Speed up COPY FROM text/CSV parsing using SIMD (Nazir Bilal Yavuz <byavuz81@gmail.com>) |
Responses |
Re: Speed up COPY FROM text/CSV parsing using SIMD
|
List | pgsql-hackers |
On Thu, Aug 7, 2025 at 8:15 PM Nazir Bilal Yavuz <byavuz81@gmail.com> wrote: > > Hi, > > Thank you for working on this! > > On Thu, 7 Aug 2025 at 04:49, Shinya Kato <shinya11.kato@gmail.com> wrote: > > > > Hi hackers, > > > > I have implemented SIMD optimization for the COPY FROM (FORMAT {csv, > > text}) command and observed approximately a 5% performance > > improvement. Please see the detailed test results below. > > I have been working on the same idea. I was not moving input_buf_ptr > as far as possible, so I think your approach is better. Great. I'm looking forward to working with you on this feature implementation. > Also, I did a benchmark on text format. I created a benchmark for line > length in a table being from 1 byte to 1 megabyte.The peak improvement > is line length being 4096 and the improvement is more than 20% [1], I > saw no regression on your patch. Thank you for the additional benchmarks. > I have a couple of ideas that I was working on: > --- > > + * However, SIMD optimization cannot be applied in the following cases: > + * - Inside quoted fields, where escape sequences and closing quotes > + * require sequential processing to handle correctly. > > I think you can continue SIMD inside quoted fields. Only important > thing is you need to set last_was_esc to false when SIMD skipped the > chunk. That's a clever point that last_was_esc should be reset to false when a SIMD chunk is skipped. You're right about that specific case. However, the core challenge is not what happens when we skip a chunk, but what happens when a chunk contains special characters like quotes or escapes. The main reason we avoid SIMD inside quoted fields is that the parsing logic becomes fundamentally sequential and context-dependent. To correctly parse a "" as a single literal quote, we must perform a lookahead to check the next character. This is an inherently sequential operation that doesn't map well to SIMD's parallel nature. Trying to handle this stateful logic with SIMD would lead to significant implementation complexity, especially with edge cases like an escape character falling on the last byte of a chunk. > + * - When the remaining buffer size is smaller than the size of a SIMD > + * vector register, as SIMD operations require processing data in > + * fixed-size chunks. > > You run SIMD when 'copy_buf_len - input_buf_ptr >= sizeof(Vector8)' > but you only call CopyLoadInputBuf() when 'input_buf_ptr >= > copy_buf_len || need_data' so basically you need to wait at least the > sizeof(Vector8) character to pass for the next SIMD. And in the worst > case; if CopyLoadInputBuf() puts one character less than > sizeof(Vector8), then you can't ever run SIMD. I think we need to make > sure that CopyLoadInputBuf() loads at least the sizeof(Vector8) > character to the input_buf so we do not encounter that problem. I think you're probably right, but we only need to account for sizeof(Vector8) when USE_NO_SIMD is not defined. > What do you think about adding SIMD to CopyReadAttributesText() and > CopyReadAttributesCSV() functions? When I add your SIMD approach to > CopyReadAttributesText() function, the improvement on the 4096 byte > line length input [1] goes from 20% to 30%. Agreed, I will. > I shared my ideas as a Feedback.txt file (.txt to stay off CFBot's > radar for this thread). I hope these help, please let me know if you > have any questions. Thanks a lot! On Mon, Aug 11, 2025 at 5:52 PM Nazir Bilal Yavuz <byavuz81@gmail.com> wrote: > > Hi, > > On Thu, 7 Aug 2025 at 14:15, Nazir Bilal Yavuz <byavuz81@gmail.com> wrote: > > > > On Thu, 7 Aug 2025 at 04:49, Shinya Kato <shinya11.kato@gmail.com> wrote: > > > > > > I have implemented SIMD optimization for the COPY FROM (FORMAT {csv, > > > text}) command and observed approximately a 5% performance > > > improvement. Please see the detailed test results below. > > > > Also, I did a benchmark on text format. I created a benchmark for line > > length in a table being from 1 byte to 1 megabyte.The peak improvement > > is line length being 4096 and the improvement is more than 20% [1], I > > saw no regression on your patch. > > I did the same benchmark for the CSV format. The peak improvement is > line length being 4096 and the improvement is more than 25% [1]. I saw > a 5% regression on the 1 byte benchmark, there are no other > regressions. Thank you. I'm not too concerned about a regression when there's only one byte per line. > > What do you think about adding SIMD to CopyReadAttributesText() and > > CopyReadAttributesCSV() functions? When I add your SIMD approach to > > CopyReadAttributesText() function, the improvement on the 4096 byte > > line length input [1] goes from 20% to 30%. > > I wanted to try using SIMD in CopyReadAttributesCSV() as well. The > improvement on the 4096 byte line length input [1] goes from 25% to > 35%, the regression on the 1 byte input is the same. Yes, I'm on it. I'm currently adding the SIMD logic to CopyReadAttributesCSV() as you suggested. I'll share the new version of the patch soon. -- Best regards, Shinya Kato NTT OSS Center
pgsql-hackers by date: