Skip to content

Conversation

@enghitalo
Copy link
Contributor

No description provided.

@enghitalo
Copy link
Contributor Author

Benchmark Source

module main


import time
import benchmark

const time_to_test = time.Time{
	year:   1980
	month:  7
	day:    11
	hour:   21
	minute: 23
	second: 42
}

fn main() {
	println('Testing time formatting performance...')

	mut http1_1_buffer1 := 'HTTP/1.1 200 OK\r\nDate: '.bytes()
	mut http1_1_buffer2 := 'HTTP/1.1 200 OK\r\nDate: '.bytes()

	mut b := benchmark.start()

	for _ in 0 .. 1_000_000 {
		a := time_to_test.http_header_string()
		unsafe {
			http1_1_buffer1.push_many(a.str, a.len)
		}
	}
	b.measure( 'http_header_string 1_000_000 times')

	for _ in 0 .. 1_000_000 {
		time_to_test.push_to_http_header(mut http1_1_buffer2)
	}
	b.measure( 'push_to_http_header 1_000_000 times')
}

@enghitalo enghitalo changed the title time: implement faster and simplier time: implement faster and simplier push_http_header Dec 26, 2025
@enghitalo
Copy link
Contributor Author

@spytheman I know that the changes to pub fn (a array) push_many(vals voidptr, size int) { are unrelated, but I only implemented the minimum to prevent CI from failing. I hope you understand. I can open an issue later reporting that push_many needs to be completed for JS since I don't have the expertise to do that.

@spytheman
Copy link
Member

@enghitalo it is not a problem - I understand why it is there.

@JalonSolov
Copy link
Contributor

JalonSolov commented Dec 27, 2025

If the target is ES6 or later, something as simple as

arr.push(...vals)

should work fine.

That would be ignoring the size parameter, though.

If the size is the number that's supposed to be pushed (even if the array is larger), you could do something like

arr.push(...vals.slice(0, size))

(that's what I read on stack overflow, at least...)

Copy link
Member

@spytheman spytheman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good work, although I would not call it simpler - it can be more performant, since it avoids allocating a new dynamic array inside push_to_http_header .

@JalonSolov JalonSolov changed the title time: implement faster and simplier push_http_header time: implement faster and simpler push_http_header Dec 27, 2025
@spytheman spytheman merged commit 39ef9b2 into vlang:master Dec 28, 2025
83 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants