1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

builtin: speed up string methods with vmemcpy instead of for loop for copying data (#18211)

This commit is contained in:
Petr Makhnev 2023-05-19 22:24:23 +04:00 committed by GitHub
parent aded6088e9
commit 9d0a1d8496
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1051,12 +1051,8 @@ pub fn (s string) substr(start int, end int) string {
str: unsafe { malloc_noscan(len + 1) } str: unsafe { malloc_noscan(len + 1) }
len: len len: len
} }
for i in 0 .. len {
unsafe {
res.str[i] = s.str[start + i]
}
}
unsafe { unsafe {
vmemcpy(res.str, s.str + start, len)
res.str[len] = 0 res.str[len] = 0
} }
return res return res
@ -1077,12 +1073,8 @@ pub fn (s string) substr_with_check(start int, end int) !string {
str: unsafe { malloc_noscan(len + 1) } str: unsafe { malloc_noscan(len + 1) }
len: len len: len
} }
for i in 0 .. len {
unsafe {
res.str[i] = s.str[start + i]
}
}
unsafe { unsafe {
vmemcpy(res.str, s.str + start, len)
res.str[len] = 0 res.str[len] = 0
} }
return res return res
@ -1114,14 +1106,7 @@ pub fn (s string) substr_ni(_start int, _end int) string {
} }
if start > s.len || end < start { if start > s.len || end < start {
mut res := string{ return ''
str: unsafe { malloc_noscan(1) }
len: 0
}
unsafe {
res.str[0] = 0
}
return res
} }
len := end - start len := end - start
@ -1131,12 +1116,8 @@ pub fn (s string) substr_ni(_start int, _end int) string {
str: unsafe { malloc_noscan(len + 1) } str: unsafe { malloc_noscan(len + 1) }
len: len len: len
} }
for i in 0 .. len {
unsafe {
res.str[i] = s.str[start + i]
}
}
unsafe { unsafe {
vmemcpy(res.str, s.str + start, len)
res.str[len] = 0 res.str[len] = 0
} }
return res return res