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

common,train,examples: using C++17 constexpr string and strlen #9176

Closed
wants to merge 1 commit into from

Conversation

GermanAizek
Copy link
Contributor

@JohannesGaessler

Before:

static cmd_params parse_cmd_params(int argc, char ** argv) {
    cmd_params params;
    std::string arg;
    bool invalid_param = false;
    const std::string arg_prefix = "--";
    const char split_delim = ',';

    params.verbose = cmd_params_defaults.verbose;
    params.output_format = cmd_params_defaults.output_format;
    params.output_format_stderr = cmd_params_defaults.output_format_stderr;
    params.reps = cmd_params_defaults.reps;
    params.numa = cmd_params_defaults.numa;

    for (int i = 1; i < argc; i++) {
        arg = argv[i];
        if (arg.compare(0, arg_prefix.size(), arg_prefix) == 0) {
            std::replace(arg.begin(), arg.end(), '_', '-');
        }

Assembly -std=c++17 -fPIC -O3 -Wall -Wextra -Wpedantic -Wcast-qual -Wno-unused-function -Wmissing-declarations -Wmissing-noreturn -pthread -fopenmp -march=native -mtune=native -Wno-array-bounds -Wno-format-truncation -Wextra-semi -Iggml/include -Iggml/src -Iinclude -Isrc -Icommon -D_XOPEN_SOURCE=600 -D_GNU_SOURCE -DGGML_USE_OPENMP -DGGML_USE_LLAMAFILE -c

parse_cmd_params(int, char**):
	push	rbp
	lea	rax, 48[rdi]
	vpxor	xmm0, xmm0, xmm0
	mov	rbp, rsp
	push	r15
	push	r14
	mov	r15, rdi
	push	r13
	push	r12
	push	rbx
	mov	ebx, esi
	sub	rsp, 1000
	mov	QWORD PTR -912[rbp], rax
	lea	rax, 96[rdi]
	mov	DWORD PTR -844[rbp], esi
	mov	QWORD PTR -952[rbp], rax
	lea	rax, 144[rdi]
	vmovdqu	XMMWORD PTR [rdi], xmm0
	lea	rsi, .LC237[rip]
	mov	QWORD PTR -944[rbp], rax
	lea	rax, 192[rdi]
	vmovdqu	XMMWORD PTR 16[rdi], xmm0
	mov	QWORD PTR -936[rbp], rax
	lea	rax, 240[rdi]
	vmovdqu	XMMWORD PTR 32[rdi], xmm0
	vmovdqu	XMMWORD PTR 48[rdi], xmm0
	vmovdqu	XMMWORD PTR 64[rdi], xmm0
	vmovdqu	XMMWORD PTR 80[rdi], xmm0
	vmovdqu	XMMWORD PTR 96[rdi], xmm0
	vmovdqu	XMMWORD PTR 112[rdi], xmm0
	vmovdqu	XMMWORD PTR 128[rdi], xmm0
	vmovdqu	XMMWORD PTR 144[rdi], xmm0
	vmovdqu	XMMWORD PTR 160[rdi], xmm0
	vmovdqu	XMMWORD PTR 176[rdi], xmm0
	mov	QWORD PTR -840[rbp], rdx
	vmovdqu	XMMWORD PTR 192[rdi], xmm0
	mov	QWORD PTR -928[rbp], rax
	lea	rax, -592[rbp]
	mov	QWORD PTR -608[rbp], rax
	lea	rax, -576[rbp]
	vmovdqu	XMMWORD PTR 208[rdi], xmm0
	vmovdqu	XMMWORD PTR 224[rdi], xmm0
	vmovdqu	XMMWORD PTR 240[rdi], xmm0
	vmovdqu	XMMWORD PTR 256[rdi], xmm0
	vmovdqu	XMMWORD PTR 272[rdi], xmm0
	vmovdqu	XMMWORD PTR 288[rdi], xmm0
	vmovdqu	XMMWORD PTR 304[rdi], xmm0
	mov	DWORD PTR 320[rdi], 0
	mov	QWORD PTR 328[rdi], 0
	mov	DWORD PTR 336[rdi], 0
	vmovdqu	XMMWORD PTR 344[rdi], xmm0
	mov	DWORD PTR 360[rdi], 0
	mov	QWORD PTR 368[rdi], 0
	mov	DWORD PTR 376[rdi], 0
	vmovdqu	XMMWORD PTR 384[rdi], xmm0
	vmovdqu	XMMWORD PTR 400[rdi], xmm0
	mov	QWORD PTR 416[rdi], 0
	mov	DWORD PTR 424[rdi], 0
	mov	QWORD PTR 432[rdi], 0
	mov	DWORD PTR 440[rdi], 0
	vmovdqu	XMMWORD PTR 448[rdi], xmm0
	mov	DWORD PTR 464[rdi], 0
	mov	QWORD PTR 472[rdi], 0
	mov	DWORD PTR 480[rdi], 0
	mov	QWORD PTR 488[rdi], 0
	mov	rdi, rax
	mov	QWORD PTR -600[rbp], 0
	mov	BYTE PTR -592[rbp], 0
	mov	QWORD PTR -920[rbp], rax
	call	std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string<std::allocator<char> >(char const*, std::allocator<char> const&).constprop.0
	movzx	eax, BYTE PTR cmd_params_defaults[rip+504]
	mov	BYTE PTR 504[r15], al
	lea	rax, 508[r15]
	mov	QWORD PTR -960[rbp], rax
	mov	rax, QWORD PTR cmd_params_defaults[rip+508]
	mov	QWORD PTR 508[r15], rax
	mov	eax, DWORD PTR cmd_params_defaults[rip+500]
	mov	DWORD PTR 500[r15], eax
	mov	eax, DWORD PTR cmd_params_defaults[rip+496]
	mov	DWORD PTR 496[r15], eax
	cmp	ebx, 1
	jle	.L9663
	lea	rax, -608[rbp]
	mov	r14d, 1
	mov	BYTE PTR -845[rbp], 0
	mov	QWORD PTR -856[rbp], rax
.L9258:
	mov	rax, QWORD PTR -840[rbp]
	movsx	rbx, r14d
	sal	rbx, 3
	mov	r13, QWORD PTR [rax+rbx]
	mov	rdi, r13
	call	strlen@PLT
	mov	rdx, QWORD PTR -600[rbp]
	mov	rcx, r13
	xor	esi, esi
	mov	rdi, QWORD PTR -856[rbp]
	mov	r8, rax
	call	std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_replace(unsigned long, unsigned long, char const*, unsigned long)@PLT
	mov	rcx, QWORD PTR -568[rbp]
	mov	r12, QWORD PTR -600[rbp]
	mov	rdi, QWORD PTR -608[rbp]
	cmp	rcx, r12
	mov	r13, r12
	cmovbe	r13, rcx
	mov	QWORD PTR -880[rbp], rdi
	test	r13, r13
	je	.L9042
	mov	rsi, QWORD PTR -576[rbp]
	mov	rdx, r13
	mov	QWORD PTR -832[rbp], rcx
	call	memcmp@PLT
	mov	rcx, QWORD PTR -832[rbp]
	test	eax, eax
	jne	.L9043

After:

static cmd_params parse_cmd_params(int argc, char ** argv) {
    cmd_params params;
    std::string arg;
    bool invalid_param = false;
    static constexpr auto arg_prefix = "--";
    const char split_delim = ',';

    params.verbose = cmd_params_defaults.verbose;
    params.output_format = cmd_params_defaults.output_format;
    params.output_format_stderr = cmd_params_defaults.output_format_stderr;
    params.reps = cmd_params_defaults.reps;
    params.numa = cmd_params_defaults.numa;

    for (int i = 1; i < argc; i++) {
        arg = argv[i];
        if (arg.compare(0, std::char_traits<char>::length(arg_prefix), arg_prefix) == 0) {
            std::replace(arg.begin(), arg.end(), '_', '-');
        }

Assembly -std=c++17 -fPIC -O3 -Wall -Wextra -Wpedantic -Wcast-qual -Wno-unused-function -Wmissing-declarations -Wmissing-noreturn -pthread -fopenmp -march=native -mtune=native -Wno-array-bounds -Wno-format-truncation -Wextra-semi -Iggml/include -Iggml/src -Iinclude -Isrc -Icommon -D_XOPEN_SOURCE=600 -D_GNU_SOURCE -DGGML_USE_OPENMP -DGGML_USE_LLAMAFILE -c

parse_cmd_params(int, char**):
	push	rbp
	lea	rax, 48[rdi]
	vpxor	xmm0, xmm0, xmm0
	mov	rbp, rsp
	push	r15
	push	r14
	mov	r14, rdi
	push	r13
	push	r12
	push	rbx
	sub	rsp, 968
	mov	QWORD PTR -880[rbp], rax
	lea	rax, 96[rdi]
	mov	DWORD PTR -812[rbp], esi
	mov	QWORD PTR -912[rbp], rax
	lea	rax, 144[rdi]
	mov	QWORD PTR -808[rbp], rdx
	mov	QWORD PTR -904[rbp], rax
	lea	rax, 192[rdi]
	vmovdqu	XMMWORD PTR [rdi], xmm0
	mov	QWORD PTR -896[rbp], rax
	lea	rax, 240[rdi]
	vmovdqu	XMMWORD PTR 16[rdi], xmm0
	vmovdqu	XMMWORD PTR 32[rdi], xmm0
	vmovdqu	XMMWORD PTR 48[rdi], xmm0
	vmovdqu	XMMWORD PTR 64[rdi], xmm0
	vmovdqu	XMMWORD PTR 80[rdi], xmm0
	vmovdqu	XMMWORD PTR 96[rdi], xmm0
	vmovdqu	XMMWORD PTR 112[rdi], xmm0
	vmovdqu	XMMWORD PTR 128[rdi], xmm0
	vmovdqu	XMMWORD PTR 144[rdi], xmm0
	vmovdqu	XMMWORD PTR 160[rdi], xmm0
	vmovdqu	XMMWORD PTR 176[rdi], xmm0
	vmovdqu	XMMWORD PTR 192[rdi], xmm0
	mov	QWORD PTR -888[rbp], rax
	lea	rax, -560[rbp]
	vmovdqu	XMMWORD PTR 208[rdi], xmm0
	vmovdqu	XMMWORD PTR 224[rdi], xmm0
	vmovdqu	XMMWORD PTR 240[rdi], xmm0
	vmovdqu	XMMWORD PTR 256[rdi], xmm0
	vmovdqu	XMMWORD PTR 272[rdi], xmm0
	vmovdqu	XMMWORD PTR 288[rdi], xmm0
	vmovdqu	XMMWORD PTR 304[rdi], xmm0
	vmovdqu	XMMWORD PTR 384[rdi], xmm0
	vmovdqu	XMMWORD PTR 400[rdi], xmm0
	mov	QWORD PTR -576[rbp], rax
	mov	DWORD PTR 320[rdi], 0
	mov	QWORD PTR 328[rdi], 0
	mov	DWORD PTR 336[rdi], 0
	vmovdqu	XMMWORD PTR 344[rdi], xmm0
	mov	DWORD PTR 360[rdi], 0
	mov	QWORD PTR 368[rdi], 0
	mov	DWORD PTR 376[rdi], 0
	mov	QWORD PTR 416[rdi], 0
	mov	DWORD PTR 424[rdi], 0
	mov	QWORD PTR 432[rdi], 0
	mov	DWORD PTR 440[rdi], 0
	vmovdqu	XMMWORD PTR 448[rdi], xmm0
	mov	DWORD PTR 464[rdi], 0
	mov	QWORD PTR 472[rdi], 0
	mov	DWORD PTR 480[rdi], 0
	mov	QWORD PTR 488[rdi], 0
	mov	QWORD PTR -568[rbp], 0
	mov	BYTE PTR -560[rbp], 0
	movzx	eax, BYTE PTR cmd_params_defaults[rip+504]
	mov	BYTE PTR 504[rdi], al
	lea	rax, 508[rdi]
	mov	QWORD PTR -920[rbp], rax
	mov	rax, QWORD PTR cmd_params_defaults[rip+508]
	mov	QWORD PTR 508[rdi], rax
	mov	eax, DWORD PTR cmd_params_defaults[rip+500]
	mov	DWORD PTR 500[rdi], eax
	mov	eax, DWORD PTR cmd_params_defaults[rip+496]
	mov	DWORD PTR 496[rdi], eax
	cmp	esi, 1
	jle	.L9657
	lea	rax, -576[rbp]
	xor	r13d, r13d
	mov	r15d, 1
	mov	BYTE PTR -813[rbp], 0
	mov	QWORD PTR -824[rbp], rax
.L9258:
	mov	rax, QWORD PTR -808[rbp]
	movsx	rbx, r15d
	sal	rbx, 3
	mov	r12, QWORD PTR [rax+rbx]
	mov	rdi, r12
	call	strlen@PLT
	mov	rdi, QWORD PTR -824[rbp]
	mov	rcx, r12
	mov	rdx, r13
	mov	r8, rax
	xor	esi, esi
	call	std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_replace(unsigned long, unsigned long, char const*, unsigned long)@PLT
	mov	r12, QWORD PTR -568[rbp]
	test	r12, r12
	je	.L9054
	mov	rax, QWORD PTR -576[rbp]
	mov	r13d, 2
	lea	rsi, .LC238[rip]
	cmp	r12, r13
	cmovbe	r13, r12
	mov	rdx, r13
	sub	r13, 2
	mov	rdi, rax
	mov	QWORD PTR -800[rbp], rax
	call	memcmp@PLT
	or	eax, r13d
	jne	.L9045
	mov	rax, QWORD PTR -800[rbp]
	lea	rdx, [rax+r12]
	cmp	rdx, rax
	je	.L9046

@JohannesGaessler
Copy link
Collaborator

I think this PR makes the code more difficult to understand and I am not convinced that there is any scenario where it makes a meaningful difference.

@ggerganov
Copy link
Owner

Also, the project does not use C++17

@ggerganov ggerganov closed this Aug 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants