Skip to content

Optimize Buffer operation with unsafe operations (was: Simple improvements to speed of Buffer module) #6148

@vicuna

Description

@vicuna

Original bug ID: 6148
Reporter: @johnwhitington
Assigned to: @damiendoligez
Status: assigned (set by @xavierleroy on 2017-02-19T17:22:57Z)
Resolution: reopened
Priority: normal
Severity: feature
Target version: 4.07.0+dev/beta2/rc1/rc2
Fixed in version: 4.02.0+dev
Category: standard library
Has duplicate: #3560
Monitored by: @hcarty

Bug description

The (tiny) attached patch to trunk uses unsafe functions in uncontroversial parts of the Buffer module. In all instances, the accesses are already checked by the logic of the Buffer module prior to the unsafe function being called.

I have avoided unsafe functions in complicated or infrequently called parts of Buffer, like the resize function.

Sample speedups in native code:
add_char heavy code: 12%
add_string heavy code: 19%
add_substring heavy code: 25%

Here's the add_string example:

let b = Buffer.create 30

let string_of_lexemes lexemes =
Buffer.clear b;
List.iter (fun l -> Buffer.add_string b l; Buffer.add_char b ' ') lexemes;
Buffer.contents b

let _ =
for x = 1 to 50_000_000 do
ignore (string_of_lexemes ["a"; "bcd"; "efg"; "hij"; "klm"])
done

The speedups in bytecode are similar.

File attachments

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions