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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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))
unsafe { free(mblock_ptr) }
unsafe {
a.data = nil
}
}
// 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.
pub fn (a array) index(value voidptr) int
[unsafe]
[direct_array_access; unsafe]
pub fn (mut a []string) free() {
$if prealloc {
return
}
for s in a {
for mut s in a {
unsafe { s.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
// Example: ['a', 'b', 'c'].str() // => "['a', 'b', 'c']".
[manualfree]
[direct_array_access; manualfree]
pub fn (a []string) str() string {
mut sb_len := 4 // 2x" + 1x, + 1xspace
if a.len > 0 {

View File

@ -728,11 +728,15 @@ pub fn (m &map) clone() map {
[unsafe]
pub fn (m &map) free() {
unsafe { free(m.metas) }
unsafe {
m.metas = nil
}
if m.key_values.deletes == 0 {
for i := 0; i < m.key_values.len; i++ {
unsafe {
pkey := m.key_values.key(i)
m.free_fn(pkey)
vmemset(pkey, 0, m.key_bytes)
}
}
} else {
@ -743,12 +747,28 @@ pub fn (m &map) free() {
unsafe {
pkey := m.key_values.key(i)
m.free_fn(pkey)
vmemset(pkey, 0, m.key_bytes)
}
}
unsafe { free(m.key_values.all_deleted) }
}
unsafe {
free(m.key_values.keys)
free(m.key_values.values)
if m.key_values.all_deleted != nil {
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 {
// C.printf(c's: %x %s\n', s.str, s.str)
free(s.str)
s.str = nil
}
s.is_lit = -98761234
}