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

perf: reduce memory allocation in some [n..m].method() use cases #22542

Closed
1 of 2 tasks
enghitalo opened this issue Oct 16, 2024 · 2 comments
Closed
1 of 2 tasks

perf: reduce memory allocation in some [n..m].method() use cases #22542

enghitalo opened this issue Oct 16, 2024 · 2 comments
Assignees
Labels
Feature/Enhancement Request This issue is made to request a feature or an enhancement to an existing one.

Comments

@enghitalo
Copy link
Contributor

enghitalo commented Oct 16, 2024

Describe the feature

[(.+?)..(.+?)].trim_space()

-> \[(.+?)\.\.(.+?)\]\.(.+?)\(\)
-> \[(.+?)\.\.\]\.(.+?)\(\)
-> \[\.\.(.+?)\]\.(.+?)\(\)

In all string method case use substr_unsafe

[(.+?)..(.+?)].(.+?)()

vlib/crypto/bcrypt/bcrypt.v

// fn (mut h Hashed) decode_cost(sbytes []u8) !int {
	cost := sbytes[0..2].bytestr().int()

vlib/veb/veb.v and vlib/x/vweb/vweb.v

// fn handle_read[A, X](mut pv picoev.Picoev, mut params RequestParams, fd int) {
// line 422
		} else if n < bytes_to_read || params.idx[fd] + n < content_length.int() {
			// request is incomplete wait until the socket becomes ready to read again
			params.idx[fd] += n
			// TODO: change this to a memcpy function?
			req.data += buf[0..n].bytestr()
			params.incomplete_requests[fd] = req
			return
		} else {
			// request is complete: n = bytes_to_read
			params.idx[fd] += n
			req.data += buf[0..n].bytestr()
		}

vlib/io/buffered_reader.v

// pub fn (mut r BufferedReader) read(mut buf []u8) !int {
// pub fn (mut r BufferedReader) read_line(config BufferedReadLineConfig) !string {

Use Case

improve performance

Proposed Solution

No response

Other Information

No response

Acknowledgements

  • I may be able to implement this feature request
  • This feature might incur a breaking change

Version used

V 0.4.8 3326392

Environment details (OS name and version, etc.)

all

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.

@enghitalo enghitalo added the Feature/Enhancement Request This issue is made to request a feature or an enhancement to an existing one. label Oct 16, 2024
@enghitalo enghitalo changed the title perf: reduce memory allocation perf: reduce memory allocation in some [n..m].method() use cases Oct 16, 2024
@enghitalo enghitalo changed the title perf: reduce memory allocation in some [n..m].method() use cases perf: reduce memory allocation in some [n..m].method() use cases Oct 16, 2024
@enghitalo enghitalo self-assigned this Oct 16, 2024
@spytheman
Copy link
Member

substr_unsafe is not safe to use in the general case.
It will produce a V string, that is a "slice" of the original, and it will not have the ending 0, that is needed for calling C APIs later.

@spytheman
Copy link
Member

The result will also entangle the life times of the 2 strings, so it will conflict with -autofree too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature/Enhancement Request This issue is made to request a feature or an enhancement to an existing one.
Projects
None yet
Development

No branches or pull requests

2 participants