Mantis Bug Tracker

View Issue Details Jump to Notes ] Issue History ] Print ]
IDProjectCategoryView StatusDate SubmittedLast Update
0006148OCamlstandard librarypublic2013-08-29 20:582017-02-22 15:50
Assigned Todoligez 
PrioritynormalSeverityminorReproducibilityhave not tried
PlatformOSOS Version
Product Version 
Target Version4.06.0+devFixed in Version4.02.0+dev 
Summary0006148: Optimize Buffer operation with unsafe operations (was: Simple improvements to speed of Buffer module)
DescriptionThe (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"])

The speedups in bytecode are similar.
TagsNo tags attached.
Attached Filesdiff file icon buffer-unsafe.diff [^] (477 bytes) 2013-08-29 20:58 [Show Content]
diff file icon buffer-unsafe-2.diff [^] (1,392 bytes) 2013-08-30 09:40 [Show Content]

- Relationships
has duplicate 0003560resolvedfrisch [WISH] Buffer 

-  Notes
ygrek (reporter)
2013-08-30 05:29

The patch seems to be reverse, and could you please use unified diff?
I.e. diff -urNw orig/ modified/
johnwhitington (reporter)
2013-08-30 09:40

See attached buffer-unsafe-2.diff
frisch (developer)
2013-08-30 15:16

Thanks! I've integrated your patch to the trunk (commit 14048).

As a side note, it's not 100% straightforward that the calls to blit in add_substring and add_string are safe. I'm thinking about possible integer overflow when computing new_position (leading to a negative value for new_position which does not trigger resizing). I think it is fine because of the limit on the maximum string length, but one needs to be careful (I've heard about a variant of the OCaml runtime developed by OCamlPro which gets rid of the limit on string lenths).
frisch (developer)
2016-12-07 18:38

Reopening, since the use of unsafe versions has be reverted in [^]

Damien: do you remember the reason for going back to the slower version?
xleroy (administrator)
2017-02-19 18:23

@doligez could you please take a decision on what to do with this code?

- Issue History
Date Modified Username Field Change
2013-08-29 20:58 johnwhitington New Issue
2013-08-29 20:58 johnwhitington File Added: buffer-unsafe.diff
2013-08-30 05:29 ygrek Note Added: 0010260
2013-08-30 09:40 johnwhitington Note Added: 0010261
2013-08-30 09:40 johnwhitington File Added: buffer-unsafe-2.diff
2013-08-30 15:16 frisch Note Added: 0010264
2013-08-30 15:16 frisch Status new => resolved
2013-08-30 15:16 frisch Fixed in Version => 4.02.0+dev
2013-08-30 15:16 frisch Resolution open => fixed
2013-08-30 15:16 frisch Assigned To => frisch
2015-12-11 19:23 xleroy Status resolved => closed
2016-12-07 18:34 frisch Relationship added has duplicate 0003560
2016-12-07 18:38 frisch Assigned To frisch =>
2016-12-07 18:38 frisch Note Added: 0016791
2016-12-07 18:38 frisch Status closed => feedback
2016-12-07 18:38 frisch Resolution fixed => reopened
2017-02-19 18:22 xleroy Assigned To => doligez
2017-02-19 18:22 xleroy Status feedback => assigned
2017-02-19 18:23 xleroy Note Added: 0017352
2017-02-20 12:04 frisch Summary Simple improvements to speed of Buffer module => Optimize Buffer operation with unsafe operations (was: Simple improvements to speed of Buffer module)
2017-02-22 15:50 frisch Target Version => 4.06.0+dev
2017-02-23 16:43 doligez Category OCaml standard library => standard library

Copyright © 2000 - 2011 MantisBT Group
Powered by Mantis Bugtracker