Project

General

Profile

Bug #10961

Optimize StringBuilder usage in FWD

Added by Teodor Gorghe 8 months ago. Updated 8 months ago.

Status:
New
Priority:
Normal
Assignee:
Target version:
-
Start date:
Due date:
% Done:

0%

billable:
No
vendor_id:
GCD
case_num:
version_reported:
version_resolved:
reviewer:
production:
No
env_name:
topics:

History

#1 Updated by Teodor Gorghe 8 months ago

The initial capacity of StringBuilder is 16 characters.
When this capacity exceeds, the entire string content gets reallocated into a new memory location, which might be an expensive operation.
In FWD source code, there are lots of usages such as new StringBuilder(), like in FqlExpression.toFinalExpression, where there is a low chance that the StringBuilder buffer do not get reallocated.

The purpose for this task is to analyze and optimize all StringBuilder usages from the FWD source code.

#3 Updated by Ovidiu Maxiniuc 8 months ago

StringBuilder has a constructor which can specify the expected number of characters. It will pre-allocate the memory to store exactly that.

Based on the places you noticed in profiler, we should estimate the expected final string size and replace new StringBuilder() with new StringBuilder(N). The idea is to use a capacity (N) large enough so that there will not be any additional reallocations, but not too huge to create memory waste. The second part is only a problem for long living (non method-local) StringBuilder instances, if any.

#4 Updated by Greg Shah 8 months ago

For long lived code, we could pre-allocate a large StringBuffer instance and reuse it instead of a local instance, deleting the contents at the end of the local usage. This assumes that delete() does not call trimToSize() or the equivalent.

#5 Updated by Eduard Soltan 8 months ago

  • Assignee set to Eduard Soltan

Also available in: Atom PDF