Skip to content

improve the memory footprint of is_prefix and is_suffix - #36

Closed
hannesm wants to merge 1 commit into
mainfrom
improve-is-prefix-is-suffix
Closed

improve the memory footprint of is_prefix and is_suffix#36
hannesm wants to merge 1 commit into
mainfrom
improve-is-prefix-is-suffix

Conversation

@hannesm

@hannesm hannesm commented Oct 14, 2025

Copy link
Copy Markdown
Owner

@kit-ty-kate

Copy link
Copy Markdown
Collaborator

This seems to be a reimplementation of a subset of #24, however in my PR i was unable to find any performance improvement from such a change. What kind of benchmark did you use?

Comment thread src/lib.ml
let len_s = String.length str
and len_suf = String.length suffix in
let diff = len_s - len_suf in
let rec aux i =

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

this allocates a closure for str, diff, suffix and len_suf

Comment thread src/lib.ml
and len_suf = String.length suffix in
let diff = len_s - len_suf in
let rec aux i =
if i = len_suf then true

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
if i = len_suf then true
if Int.equal i len_suf then true

Comment thread src/lib.ml
let diff = len_s - len_suf in
let rec aux i =
if i = len_suf then true
else if String.unsafe_get str (diff + i) <> String.unsafe_get suffix i then false

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
else if String.unsafe_get str (diff + i) <> String.unsafe_get suffix i then false
else if not (Char.equal (String.unsafe_get str (diff + i)) (String.unsafe_get suffix i) then false

Comment thread src/lib.ml
else if String.unsafe_get str i <> String.unsafe_get prefix i then false
else aux (i + 1)
in
len_s >= len_pre && aux 0

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
len_s >= len_pre && aux 0
len_s >= (len_pre : int) && aux 0

@hannesm

hannesm commented Oct 15, 2025

Copy link
Copy Markdown
Owner Author

My benchmark was conex, in which I figured via memtrace that is_prefix consumes a lot of memory. But for patch I don't have figures.

@hannesm hannesm closed this Oct 15, 2025
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.

2 participants