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

vlib: simplify byte character conditions by utilizing methods #21725

Merged
merged 1 commit into from
Jun 25, 2024

Conversation

ttytm
Copy link
Member

@ttytm ttytm commented Jun 24, 2024

No description provided.

@spytheman
Copy link
Member

What is the performance impact?

@yuyi98
Copy link
Member

yuyi98 commented Jun 25, 2024

Because is_alnum()/is_capital()/is_letter() are inline functions, there is no performance impact and the overall clarity is much clearer.

@ttytm
Copy link
Member Author

ttytm commented Jun 25, 2024

Yep, should be only only a cosmetic / read- / maintainability change.

  • (c >= `a` && c <= `z`) || (c >= `A` && c <= `Z`)

    occurrences of those are replaced with

    v/vlib/builtin/string.v

    Lines 2065 to 2068 in c5c49d3

    @[inline]
    pub fn (c u8) is_letter() bool {
    return (c >= `a` && c <= `z`) || (c >= `A` && c <= `Z`)
    }

  • c >= `0` && c <= `9`

    replaced with

    v/vlib/builtin/string.v

    Lines 2037 to 2040 in c5c49d3

    @[inline]
    pub fn (c u8) is_digit() bool {
    return c >= `0` && c <= `9`
    }

  • (c >= `a` && c <= `z`) || (c >= `A` && c <= `Z`) || (c >= `0` && c <= `9`)

    replaced with

    v/vlib/builtin/string.v

    Lines 2072 to 2075 in c5c49d3

    @[inline]
    pub fn (c u8) is_alnum() bool {
    return (c >= `a` && c <= `z`) || (c >= `A` && c <= `Z`) || (c >= `0` && c <= `9`)
    }

  • To not change anything else, e.g. in case of below (in vlib/v/util/scanning.v)

    (c >= `a` && c <= `z`) || (c >= `A` && c <= `Z`) || c == `_` || (c >= `0` && c <= `9`)

    I'm replacing it with

    c.is_letter() || c == `_` || c.is_digit()
    // instead of 
    c.is_alnum() || c == `_`

@spytheman
Copy link
Member

spytheman commented Jun 25, 2024

Because is_alnum()/is_capital()/is_letter() are inline functions, there is no performance impact and the overall clarity is much clearer.

In theory I agree, but have you measured, for example, recompiling V itself before/after the change?
Keep in mind, that compilers are not perfect in inlining functions, and some of those comparisons, are used in relatively hot loops.

@spytheman
Copy link
Member

V executables compiled with -prod -cc gcc-11, are consistently a bit faster with the changes here:
image

@spytheman
Copy link
Member

V executables compiled with just v self (i.e. using tcc), are in contrast a bit slower, but the relative difference is much smaller, and sometimes the new version is faster too:
image

Copy link
Member

@spytheman spytheman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent work.

@spytheman spytheman merged commit 5b93582 into vlang:master Jun 25, 2024
76 checks passed
@ttytm
Copy link
Member Author

ttytm commented Jun 25, 2024

Thanks for verifying @spytheman ! Didn't found the a free moment to do and share those tests on time.

@ttytm ttytm deleted the v/use-byte-methods branch June 25, 2024 07:16
@JalonSolov
Copy link
Contributor

I'm glad the extra performance testing was done. Too many times in the past I've seen someone say "It gets inlined, so it'll be fast."

2 things to always keep in mind:

  1. Inline is a suggestion, not a guarantee. The C compiler is free to ignore it.

  2. Inline only works (sometimes, currently, at least) with the C backend. It has no affect on the JavaScript, WASM, native, etc. backends.

However, with the tcc tests, it appears the changes don't have any significant impact, so should be fine all around.

raw-bin pushed a commit to raw-bin/v that referenced this pull request Jul 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants