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

builtin: zero out internal map/array pointers on m.free(), to reduce the work for the GC mark phase for non escaping maps/arrays, used in hot loops (#18415)

This commit is contained in:
Delyan Angelov
2023-06-14 15:00:36 +03:00
committed by GitHub
parent 4ba4fe7c25
commit 6806086bf1
3 changed files with 30 additions and 6 deletions

View File

@ -750,6 +750,9 @@ pub fn (a &array) free() {
} }
mblock_ptr := &u8(u64(a.data) - u64(a.offset)) mblock_ptr := &u8(u64(a.data) - u64(a.offset))
unsafe { free(mblock_ptr) } unsafe { free(mblock_ptr) }
unsafe {
a.data = nil
}
} }
// Some of the following functions have no implementation in V and exist here // Some of the following functions have no implementation in V and exist here
@ -867,12 +870,12 @@ pub fn (a array) contains(value voidptr) bool
// or `-1` if the value is not found. // or `-1` if the value is not found.
pub fn (a array) index(value voidptr) int pub fn (a array) index(value voidptr) int
[unsafe] [direct_array_access; unsafe]
pub fn (mut a []string) free() { pub fn (mut a []string) free() {
$if prealloc { $if prealloc {
return return
} }
for s in a { for mut s in a {
unsafe { s.free() } unsafe { s.free() }
} }
unsafe { (&array(&a)).free() } unsafe { (&array(&a)).free() }
@ -883,7 +886,7 @@ pub fn (mut a []string) free() {
// str returns a string representation of an array of strings // str returns a string representation of an array of strings
// Example: ['a', 'b', 'c'].str() // => "['a', 'b', 'c']". // Example: ['a', 'b', 'c'].str() // => "['a', 'b', 'c']".
[manualfree] [direct_array_access; manualfree]
pub fn (a []string) str() string { pub fn (a []string) str() string {
mut sb_len := 4 // 2x" + 1x, + 1xspace mut sb_len := 4 // 2x" + 1x, + 1xspace
if a.len > 0 { if a.len > 0 {

View File

@ -728,11 +728,15 @@ pub fn (m &map) clone() map {
[unsafe] [unsafe]
pub fn (m &map) free() { pub fn (m &map) free() {
unsafe { free(m.metas) } unsafe { free(m.metas) }
unsafe {
m.metas = nil
}
if m.key_values.deletes == 0 { if m.key_values.deletes == 0 {
for i := 0; i < m.key_values.len; i++ { for i := 0; i < m.key_values.len; i++ {
unsafe { unsafe {
pkey := m.key_values.key(i) pkey := m.key_values.key(i)
m.free_fn(pkey) m.free_fn(pkey)
vmemset(pkey, 0, m.key_bytes)
} }
} }
} else { } else {
@ -743,12 +747,28 @@ pub fn (m &map) free() {
unsafe { unsafe {
pkey := m.key_values.key(i) pkey := m.key_values.key(i)
m.free_fn(pkey) m.free_fn(pkey)
vmemset(pkey, 0, m.key_bytes)
} }
} }
unsafe { free(m.key_values.all_deleted) }
} }
unsafe { unsafe {
free(m.key_values.keys) if m.key_values.all_deleted != nil {
free(m.key_values.values) free(m.key_values.all_deleted)
m.key_values.all_deleted = nil
}
if m.key_values.keys != nil {
free(m.key_values.keys)
m.key_values.keys = nil
}
if m.key_values.values != nil {
free(m.key_values.values)
m.key_values.values = nil
}
// TODO: the next lines assume that callback functions are static and independent from each particular
// map instance. Closures may invalidate that assumption, so revisit when RC for closures works.
m.hash_fn = nil
m.key_eq_fn = nil
m.clone_fn = nil
m.free_fn = nil
} }
} }

View File

@ -1838,6 +1838,7 @@ pub fn (s &string) free() {
unsafe { unsafe {
// C.printf(c's: %x %s\n', s.str, s.str) // C.printf(c's: %x %s\n', s.str, s.str)
free(s.str) free(s.str)
s.str = nil
} }
s.is_lit = -98761234 s.is_lit = -98761234
} }