~martanne/devel

This thread contains a patchset. You're looking at the original emails, but you may wish to use the patch review UI. Review patch
3 3

[PATCH vis] complete-filename: speed-up the tilda handling

Details
Message ID
<20250208081912.3508-1-mcepl@cepl.eu>
Sender timestamp
1739006304
DKIM signature
pass
Download raw message
Patch: +3 -2
When profiling (on 100,000 calls) with this test:

```lua
start_t = os.clock()
for i = 1, 100000 do
    _ = home .. prefix
end
end_t = os.clock()
print(end_t - start_t)
```

I get on my computer this (warm):

    os.getenv("HOME") 0.005506
    str:gsub("^~", home) 0.009333
    str:find("^~") 0.006041
    str:sub(2) 0.005455
    a .. b 0.003603

Difference between gsub and sub + .. seems negligible, but just
to make it correct, here we are.
---
Note to myself: attaching new patch to the old thread via --in-reply-to makes the patch lost.


 lua/plugins/complete-filename.lua | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/lua/plugins/complete-filename.lua b/lua/plugins/complete-filename.lua
index 02b9f3a9..604361c2 100644
--- a/lua/plugins/complete-filename.lua
+++ b/lua/plugins/complete-filename.lua
@@ -23,9 +23,10 @@ local complete_filename = function(expand)
	end

	-- Expand tilda for the home directory
	if prefix:find('^~') then
	_, j = prefix:find('^~')
	if j ~= nil then
		local home = assert(os.getenv("HOME"), "$HOME variable not set!")
		prefix = prefix:gsub("^~", home, 1)
		prefix = home .. prefix:sub(j + 1)
	end

	local cmdfmt = "vis-complete --file '%s'"
-- 
2.48.1

[vis/patches] build success

builds.sr.ht <builds@sr.ht>
Details
Message ID
<D7MX289JV1L7.247DOIBGM1QN3@fra02>
In-Reply-To
<20250208081912.3508-1-mcepl@cepl.eu> (view parent)
Sender timestamp
1739002850
DKIM signature
missing
Download raw message
vis/patches: SUCCESS in 1m26s

[complete-filename: speed-up the tilda handling][0] from [~mcepl][1]

[0]: https://lists.sr.ht/~martanne/devel/patches/57359
[1]: mcepl@cepl.eu

✓ #1425198 SUCCESS vis/patches/debian.yml  https://builds.sr.ht/~martanne/job/1425198
✓ #1425197 SUCCESS vis/patches/alpine.yml  https://builds.sr.ht/~martanne/job/1425197
✓ #1425199 SUCCESS vis/patches/freebsd.yml https://builds.sr.ht/~martanne/job/1425199
✓ #1425200 SUCCESS vis/patches/openbsd.yml https://builds.sr.ht/~martanne/job/1425200
Details
Message ID
<27LI4VLP4FR43.2UVVGFLN4C8UG@homearch.localdomain>
In-Reply-To
<20250208081912.3508-1-mcepl@cepl.eu> (view parent)
Sender timestamp
1739011469
DKIM signature
missing
Download raw message
Matěj Cepl <mcepl@cepl.eu> wrote:
> When profiling (on 100,000 calls) with this test:
> 
> ```lua
> start_t = os.clock()
> for i = 1, 100000 do
>     _ = home .. prefix
> end
> end_t = os.clock()
> print(end_t - start_t)
> ```
> 
> I get on my computer this (warm):
> 
>     os.getenv("HOME") 0.005506
>     str:gsub("^~", home) 0.009333
>     str:find("^~") 0.006041
>     str:sub(2) 0.005455
>     a .. b 0.003603
> 
> Difference between gsub and sub + .. seems negligible, but just
> to make it correct, here we are.
> ---
> Note to myself: attaching new patch to the old thread via --in-reply-to makes the patch lost.
> 
> 
>  lua/plugins/complete-filename.lua | 5 +++--
>  1 file changed, 3 insertions(+), 2 deletions(-)

I don't think it makes a difference whether it takes 5/100000 of a
second or 10/100000 of a second to run this check (and potentially the
substitution) each time ...

Since it doesn't make the code more complex, I'm ok with it though!

LGTM!

Cheers,
Silvan 

> 
> diff --git a/lua/plugins/complete-filename.lua b/lua/plugins/complete-filename.lua
> index 02b9f3a9..604361c2 100644
> --- a/lua/plugins/complete-filename.lua
> +++ b/lua/plugins/complete-filename.lua
> @@ -23,9 +23,10 @@ local complete_filename = function(expand)
>  	end
>  
>  	-- Expand tilda for the home directory
> -	if prefix:find('^~') then
> +	_, j = prefix:find('^~')
> +	if j ~= nil then
>  		local home = assert(os.getenv("HOME"), "$HOME variable not set!")
> -		prefix = prefix:gsub("^~", home, 1)
> +		prefix = home .. prefix:sub(j + 1)
>  	end
>  
>  	local cmdfmt = "vis-complete --file '%s'"
Details
Message ID
<324Z39BNP7UF9.3U5W5YTVV21IF@rnpnr.xyz>
In-Reply-To
<20250208081912.3508-1-mcepl@cepl.eu> (view parent)
Sender timestamp
1740215782
DKIM signature
permerror
Download raw message
Matěj Cepl <mcepl@cepl.eu> wrote:
> When profiling (on 100,000 calls) with this test:
> 
> ```lua
> start_t = os.clock()
> for i = 1, 100000 do
>     _ = home .. prefix
> end
> end_t = os.clock()
> print(end_t - start_t)
> ```
> 
> I get on my computer this (warm):
> 
>     os.getenv("HOME") 0.005506
>     str:gsub("^~", home) 0.009333
>     str:find("^~") 0.006041
>     str:sub(2) 0.005455
>     a .. b 0.003603
> 
> Difference between gsub and sub + .. seems negligible, but just
> to make it correct, here we are.
> ---
> Note to myself: attaching new patch to the old thread via --in-reply-to makes the patch lost.
> 
> 
>  lua/plugins/complete-filename.lua | 5 +++--
>  1 file changed, 3 insertions(+), 2 deletions(-)
> 
> diff --git a/lua/plugins/complete-filename.lua b/lua/plugins/complete-filename.lua
> index 02b9f3a9..604361c2 100644
> --- a/lua/plugins/complete-filename.lua
> +++ b/lua/plugins/complete-filename.lua
> @@ -23,9 +23,10 @@ local complete_filename = function(expand)
>  	end
>  
>  	-- Expand tilda for the home directory
> -	if prefix:find('^~') then
> +	_, j = prefix:find('^~')
> +	if j ~= nil then
>  		local home = assert(os.getenv("HOME"), "$HOME variable not set!")
> -		prefix = prefix:gsub("^~", home, 1)
> +		prefix = home .. prefix:sub(j + 1)
>  	end
>  
>  	local cmdfmt = "vis-complete --file '%s'"

Applied!

I agree with Silvan that its probably not a good use of time to go
looking in paths that are rarely called for such small
improvements. But I'm not going to let the work go to waste since
it was already done.

If I put you up to this based on some comment of mine I'm sorry
that wasn't my intention.

- Randy

-- 
https://rnpnr.xyz/
GPG Fingerprint: B8F0 CF4C B6E9 415C 1B27 A8C4 C8D2 F782 86DF 2DC5
Reply to thread Export thread (mbox)