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

Performance Issues with time.now? #19554

Open
xiusin opened this issue Oct 12, 2023 · 8 comments
Open

Performance Issues with time.now? #19554

xiusin opened this issue Oct 12, 2023 · 8 comments
Labels
Bug This tag is applied to issues which reports bugs. Status: Confirmed This bug has been confirmed to be valid by a contributor. Type: Optimization This issue/PR is related to optimizations that could be applied to V. Unit: vlib Bugs/feature requests, that are related to the vlib.

Comments

@xiusin
Copy link
Contributor

xiusin commented Oct 12, 2023

Describe the bug

module cache

[heap]
struct Test {
mut:
	created_at time.Time
}

fn test_map() {
	mut b := benchmark.start()
	fn [mut b] () {
		mut m := map[string]&Test{}
		mut i := 0
		for {
			if i > 10000000 {
				break
			}

			m['test_${i}'] = &Test{}
			i++
		}

		b.measure('Test no default')
	}()


	fn [mut b] () {
		mut m := map[string]&Test{}
		mut i := 0
		for {
			if i > 10000000 {
				break
			}

			m['test_${i}'] = &Test{created_at: time.now()}
			i++
		}

		b.measure('Test default')
	}()
}


GC Warning: Repeated allocation of very large block (appr. size 162746368):
	May lead to memory leak and poor performance
SPENT 9076.469 ms in Test no default
SPENT90380.174 ms in Test default

Reproduction Steps

As shown above

Expected Behavior

High Performance

Current Behavior

Initialization makes the stress testing program slow by nearly 10x

Possible Solution

No response

Additional Information/Context

No response

V version

V 0.4.2 2332c17

Environment details (OS name and version, etc.)

V full version: V 0.4.2 b9a1b0c.2332c17
OS: macos, macOS, 13.5.1, 22G90
Processor: 8 cpus, 64bit, little endian, Intel(R) Core(TM) i5-8279U CPU @ 2.40GHz

Note

You can vote for this issue using the 👍 reaction. More votes increase the issue's priority for developers.

Take into account that only the 👍 reaction counts as a vote.
Only reactions to the issue itself will be counted as votes, not comments.

@xiusin xiusin added the Bug This tag is applied to issues which reports bugs. label Oct 12, 2023
@ArtemkaKun ArtemkaKun added Type: Optimization This issue/PR is related to optimizations that could be applied to V. Unit: vlib Bugs/feature requests, that are related to the vlib. labels Oct 12, 2023
@JalonSolov
Copy link
Contributor

JalonSolov commented Oct 24, 2023

A complete, fully runnable example:

import benchmark
import time

[heap]
struct Test {
mut:
        created_at time.Time
}

fn main() {
        mut b := benchmark.start()
        fn [mut b] () {
                mut m := map[string]&Test{}
                for i in 0..10000000 {
                        m['test_${i}'] = &Test{}
                }

                b.measure('Test no default')
        }()


        fn [mut b] () {
                mut m := map[string]&Test{}
                for i in 0..10000000 {
                        m['test_${i}'] = &Test{created_at: time.now()}
                }

                b.measure('Test default')
        }()
}

Output for me is

[jalon@7950x ~]$ v run time.v
GC Warning: Repeated allocation of very large block (appr. size 63430656):
        May lead to memory leak and poor performance
GC Warning: Repeated allocation of very large block (appr. size 134221824):
        May lead to memory leak and poor performance
 SPENT  7160.987 ms in Test no default
GC Warning: Repeated allocation of very large block (appr. size 114302976):
        May lead to memory leak and poor performance
 SPENT 15344.580 ms in Test default
[jalon@7950x ~]$

@xiusin
Copy link
Contributor Author

xiusin commented Oct 25, 2023

@JalonSolov My computer is a MacBook produced in mid-2018, and the performance gap is quite evident.

@medvednikov
Copy link
Member

M2:

 SPENT  7868.688 ms in Test no default
 SPENT 47074.378 ms in Test default

In Go the difference is only 0.5s, so there's definitely something wrong with time.now()

Good find @xiusin

Will fix

@medvednikov medvednikov added the Status: Confirmed This bug has been confirmed to be valid by a contributor. label Oct 29, 2023
@ttytm
Copy link
Member

ttytm commented Apr 16, 2024

Testing again with latest V, I can reproduce the slow down of about 10x on an m1 macbook. On an x64 linux machine it's about 2x, independent of the used compiler and if running the prog with or without -prod.

@JalonSolov
Copy link
Contributor

JalonSolov commented Apr 16, 2024

There are at least a couple of "extra" things happening here. For one, the example code is creating maps and adding to them in the loop. After you take out the "excess" code, so that you come closer to only testing the time code...

import benchmark
import time

struct Test {
mut:
        created_at time.Time
}

fn main() {
        mut b := benchmark.start()

        fn [mut b] () {
                for _ in 0 .. 10000000 {
                        _ = Test{}
                }

                b.measure('Test no default')
        }()

        fn [mut b] () {
                for _ in 0 .. 10000000 {
                        _ = Test{
                                created_at: time.now()
                        }
                }

                b.measure('Test default')
        }()
}

output for me (on linux, without -prod) is

 SPENT    76.368 ms in Test no default
 SPENT  1195.009 ms in Test default

and with -prod is

 SPENT     0.004 ms in Test no default
 SPENT   740.276 ms in Test default

The 2nd thing is that the 2nd loop is creating an initialized time.Time, while the first is creating an "empty" time.Time. So... extra code for initialization definitely will take longer.

@ttytm
Copy link
Member

ttytm commented Apr 17, 2024

Running with a 1mil instead of a 10mil loop

Linux x64

# -cc tcc (6-7x diff)
 SPENT    45.239 ms in Test no default
 SPENT   352.716 ms in Test default
# -cc gcc -prod
 SPENT     0.005 ms in Test no default
 SPENT   106.653 ms in Test default

macOS m1

# -cc tcc (600-700x diff)
 SPENT     5.058 ms in Test no default
 SPENT  3200.153 ms in Test default
# -cc clang -prod
 SPENT     0.005 ms in Test no default
 SPENT  3095.827 ms in Test default

@JalonSolov
Copy link
Contributor

Definitely something odd with m1 mac. The time for initialization should be much smaller. Weird.

@ttytm
Copy link
Member

ttytm commented Apr 17, 2024

In general with mac I think. The differences in the initial issue description (mac intel) and on alex m2 in comparison with linux are significant too.

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. Status: Confirmed This bug has been confirmed to be valid by a contributor. Type: Optimization This issue/PR is related to optimizations that could be applied to V. Unit: vlib Bugs/feature requests, that are related to the vlib.
Projects
None yet
Development

No branches or pull requests

5 participants