Skip to content

Conversation

setton
Copy link
Member

@setton setton commented May 20, 2019

Introduce a new function for string conversions
that avoids stack allocation, and use this in the
case of a giant string.

Adding test as part of the GPS testsuite.

procedure Store (C : Unsigned_16) is
begin
Len := Len + 1;
Result (Len) := Character'Val (C);
Copy link
Member

Choose a reason for hiding this comment

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

Can we use this:

Ada.Strings.Unbounded.Append (Result, Character'Val (C)); 

Average complexity of Append is O(1), so not worse, but we avoid explicit new and Free in the code.

This way we can do even better: just iterate over Value taking slices of some reasonable length (say 512 characters) Encode them and append to Result. Like this:

Ada.Strings.Unbounded.Append (Result, Encode (Value (J .. J + 511))); 

So we don't need copy/pasting of Encode.
What do you think?

Copy link
Member Author

Choose a reason for hiding this comment

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

very good idea, I will try!

Copy link
Member Author

Choose a reason for hiding this comment

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

Done!

@setton setton force-pushed the topic/S516-036.large_file_on_stack branch from 4a459ea to e762c9c Compare May 21, 2019 20:29
Next_Index := Natural'Min (Current_Index + Chunk_Size, Wide'Last);
Ada.Strings.Unbounded.Append
(Res, Ada.Strings.UTF_Encoding.Wide_Strings.Encode
(Wide (Current_Index .. Next_Index)));
Copy link
Member

Choose a reason for hiding this comment

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

One more idea. We can avoid potentially large Wide string on the stack if replace Wide (a .. b) with Slice function from Wide_Unbounded_Strings package.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done!

@setton setton force-pushed the topic/S516-036.large_file_on_stack branch from e762c9c to c706282 Compare May 22, 2019 19:56
Introduce a new function for string conversions that avoids stack allocation, and use this in the case of a giant string.
@setton setton force-pushed the topic/S516-036.large_file_on_stack branch from c706282 to 792604a Compare May 22, 2019 20:04
setton added 2 commits May 22, 2019 17:09
Introduce a new function for string conversions that avoids stack allocation, and use this in the case of a giant string.
…re/ada_language_server into topic/S516-036.large_file_on_stack
@setton setton closed this May 23, 2019
adacore-bot pushed a commit to AdaCore/gnatstudio that referenced this pull request May 24, 2019
This tests AdaCore/ada_language_server#112 Change-Id: Id879c26bad487d1b3f3b13db2325734a7f766a13
adacore-bot pushed a commit to AdaCore/gnatstudio that referenced this pull request May 24, 2019
This avoids blowing the stack on large files. Needs AdaCore/ada_language_server#112 Change-Id: I3e8fd4d3b8f0092e6d56331f6328a9017a215cb1
@setton setton deleted the topic/S516-036.large_file_on_stack branch July 15, 2019 13:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants