Skip to content

Created native memory manager to reduce nesting and simplify code - #25

Open
jshergal wants to merge 2 commits into
evolvedlight:developfrom
jshergal:refactor
Open

Created native memory manager to reduce nesting and simplify code#25
jshergal wants to merge 2 commits into
evolvedlight:developfrom
jshergal:refactor

Conversation

@jshergal

Copy link
Copy Markdown
Contributor

Feel free to reject this PR, it was just something that I thought could be helpful by aiding in readability of the main Typst compiler C# code.

There are a couple of other cleanups in the PR, so if you like some, but aren't interested in the NativeStringMemoryManager object, I can pull that stuff out and just add the others in.

My changes started with just changing this line:

var fontPathsList = fontPaths.ToList();

into this line:

var fontPathsList = fontPaths as IList<string> ?? fontPaths.ToList();

I would imagine that a majority of the time, the fontPaths value is going to come from either a string[] or List<string>, so it seemed like a good performance tweak to just check if the IEnumerable<string> is already one of those and only call ToList() if it isn't.

There are some other minor tweaks in the code as well, but the major change I added was the new NativeStringManager that encompasses the marshaling calls, tracts each returned pointer, and then on Dispose() frees them. The class itself doesn't add any real memory pressure, as it is using arrays from the ArrayPool.

Adding it allows for the removal of the try/finally blocks that are just dealing with that native memory. That reduces nesting within the code and removes some "boilerplate" noise.

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.

1 participant