Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

a V compiler bug #21651

Open
vieangry opened this issue Jun 6, 2024 · 0 comments · May be fixed by #21737
Open

a V compiler bug #21651

vieangry opened this issue Jun 6, 2024 · 0 comments · May be fixed by #21737
Labels
Bug This tag is applied to issues which reports bugs. Generics[T] Bugs/feature requests, that are related to the V generics. Unit: cgen Bugs/feature requests, that are related to the default C generating backend. Unit: Checker Bugs/feature requests, that are related to the type checker. Unit: Compiler Bugs/feature requests, that are related to the V compiler in general.

Comments

@vieangry
Copy link

vieangry commented Jun 6, 2024

Describe the bug

image

Reproduction Steps

// lru.v
module lru

import datatypes
import time

struct Entry[V] {
mut:
	value     V
	load_time u32
}

struct Lru[T, V] {
mut:
	m    map[T]Entry[V]
	list datatypes.DoublyLinkedList[T]
	cap  u32 = 1000
	ttl  u32 = 3
	on_del ?fn(T, V)
pub mut:
	hits u32
	miss u32
}

pub fn new[T, V](cap u32, ttl u32) &Lru[T, V] {
	return &Lru[T, V]{
		cap: cap
		ttl: ttl
	}
}

pub fn (mut l Lru[T, V]) set_on_del(on_del fn(T, V)) {
	l.on_del = on_del
}

pub fn (mut l Lru[T, V]) add(k T, v V) {
	if l.m.len >= (l.cap * 97 / 100) {
		l.remove_expired(0)
		for l.m.len > (l.cap * 90 / 100) {
			l.del(l.list.pop_back() or { return })
		}
	}
	l.m[k] = Entry[V]{v, u32(time.now().unix())}
	l.list.push_front(k)
}

pub fn (mut l Lru[T, V]) get(k T) ?V {
	l.remove_expired(0)
	if k in l.m {
		l.list.delete(l.list.index(k) or { return none })
		l.list.push_front(k)
		l.hits++
		return l.m[k].value
	}
	l.miss++
	return none
}

pub fn (mut l Lru[T, V]) remove_expired(cnt int) {
	now := u32(time.now().unix())
	iter := l.list.back_iterator()
	del_cnt := 0
	for key in iter {
		if e := l.m[key] {
			if e.load_time + l.ttl >= now || (cnt > 0 && del_cnt >= cnt) {
				break
			}
			l.del(key)
		}
	}
}

pub fn (mut l Lru[T, V]) del(k T) {
	if k in l.m {
		val := l.m[k].value
		l.m.delete(k)
		l.list.delete(l.list.index(k) or { -1 })
		on_del := l.on_del or { return }
		on_del(k, val)
	}
}

// main.v
module main
import lru
// import time

struct TT {
	age int
	dd int
}

fn main() {
    mut c := lru.new[int, &TT](10, 3)
	c.add(1, &TT{1,1})
	print(c.get(1))
	// c.set_on_del(fn(k int, v TT) {
	// 	println("on del k: ${k}, v: ${v}")
	// })
	// println(c.get(1) or {TT{}})
	// time.sleep(4*time.second)
	// println(c.get(1) or {TT{}})
}

Expected Behavior

why &

Current Behavior

why &

Possible Solution

No response

Additional Information/Context

No response

V version

V 0.4.6 6b2d527

Environment details (OS name and version, etc.)

V full version: V 0.4.6 6b2d527
OS: linux, Ubuntu 22.04.3 LTS (WSL)

Note

You can use the 👍 reaction to increase the issue's priority for developers.

Please note that only the 👍 reaction to the issue itself counts as a vote.
Other reactions and those to comments will not be taken into account.

@vieangry vieangry added the Bug This tag is applied to issues which reports bugs. label Jun 6, 2024
@spytheman spytheman added Unit: cgen Bugs/feature requests, that are related to the default C generating backend. Unit: Compiler Bugs/feature requests, that are related to the V compiler in general. Generics[T] Bugs/feature requests, that are related to the V generics. Unit: Checker Bugs/feature requests, that are related to the type checker. labels Jun 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug This tag is applied to issues which reports bugs. Generics[T] Bugs/feature requests, that are related to the V generics. Unit: cgen Bugs/feature requests, that are related to the default C generating backend. Unit: Checker Bugs/feature requests, that are related to the type checker. Unit: Compiler Bugs/feature requests, that are related to the V compiler in general.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants