[PATCH v1] Use RVV for bounded NUL scans in pq_getmsgstring - #426
Open
pg-hub-mirror[bot] wants to merge 1 commit into
Open
pg-hub-mirror[bot] wants to merge 1 commit into
pg-hub-mirror[bot] wants to merge 1 commit into
Conversation
Co-authored-by: Ni Jincheng <nijincheng@iscas.ac.cn> Co-authored-by: Yuansheng <yuansheng@isrc.iscas.ac.cn>
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
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
pgsql-hackers27ae5a36.e462.1a0b3560058.Coremail.wanghongyan2025@iscas.ac.cnPatch files:
Hi,
This patch adds an RVV-accelerated, bounded NUL-byte scan for
pq_getmsgstring() and pq_getmsgrawstring().
Both functions currently use strlen() and then verify that the terminating
NUL byte is within the message boundary. On RVV builds, the patch replaces
that scan with a common helper that searches for the NUL byte using RVV
intrinsics.
The vector length is limited to the number of bytes remaining in the
message before every load. Therefore, the helper does not read beyond the
PostgreSQL message boundary. If no NUL byte is found within that boundary,
the existing protocol-violation error path is preserved.
The RVV implementation is enabled only when __riscv_vector is defined and
<riscv_vector.h> is available. Non-RVV builds retain the existing strlen()
path. The public APIs and the character-set conversion behavior of
pq_getmsgstring() remain unchanged.
The patch is intended for PostgreSQL master. Full validation was performed
on PostgreSQL commit:
86f7c82
Correctness and portability results:
with GCC 14, GCC 15, and Clang 17
The RISC-V builds used:
CFLAGS="-O2 -g -march=rv64gcv -mabi=lp64d"
Disassembly of the GCC 14 and GCC 15 builds confirmed that the generated
code contains vsetvli, vle8.v, vmseq, and vfirst.m instructions. Clang 17
generated the same RVV operations, although the target system's objdump
displayed some of them as .insn.
End-to-end performance was measured using GCC 15.1 with a 4 KiB
simple-query workload, one client and one thread. The test contained 24
baseline/patched pairs, with each member running for 8 seconds. Execution
order alternated between baseline-first and patched-first.
Results:
A separate microbenchmark covering 55 string-length and alignment
conditions showed a median 3.34x speedup over strlen() and a median 3.97x
speedup over memchr() for the RVV helper.
The performance measurements were performed on one RVV system, and the
end-to-end result is specific to the 4 KiB simple-query, single-client
workload. It should not be assumed to represent every PostgreSQL workload
or RISC-V implementation.
No new SQL regression test is included because the change does not alter
SQL-visible behavior. The existing regression suite passed on both the
RVV and scalar fallback paths, while the message-boundary and missing-NUL
cases were covered by the targeted tests described above. No user-facing
documentation change is needed because this is an internal,
architecture-specific optimization.
Feedback on the bounded scanning approach and the placement of the
RISC-V-specific implementation would be appreciated.
Regards,
Hongyan Wang