net,vweb: reduce allocations by ~80%

This commit is contained in:
Alexander Medvednikov 2023-11-11 01:10:23 +03:00
parent b3a9701129
commit e7cad4f55d
12 changed files with 464 additions and 91 deletions

View file

@ -1048,6 +1048,20 @@ pub fn (s string) substr(start int, _end int) string {
return res
}
// substr_unsafe works like substr(), but doesn't copy (allocate) the substring
[direct_array_access]
pub fn (s string) substr_unsafe(start int, _end int) string {
end := if _end == 2147483647 { s.len } else { _end } // max_int
len := end - start
if len == s.len {
return s
}
return string{
str: unsafe { s.str + start }
len: len
}
}
// version of `substr()` that is used in `a[start..end] or {`
// return an error when the index is out of range
[direct_array_access]