Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Convert System.Globalization.Native to C #22378

Merged
merged 14 commits into from
Feb 4, 2019

Conversation

filipnavara
Copy link
Member

@filipnavara filipnavara commented Feb 3, 2019

Revival of #18614.

The original PR was closed and never finished. These were the remaining issues raised during the review:

I started to address them. The error handling is not always as pretty as it should be. Notably ucal_set doesn't handle NULL value in first parameter, so explicit error handling must precede it.

@filipnavara filipnavara force-pushed the native_cpp_to_C branch 2 times, most recently from f3eda5e to f82860a Compare February 3, 2019 08:00
@filipnavara
Copy link
Member Author

@dotnet-bot test CentOS7.1 x64 Debug Innerloop Build please

@filipnavara
Copy link
Member Author

I believe I have addressed all the original feedback and it should be ready for review. Some enum names are not perfect, but it should not be a regression compared to the previous C++ code. Error handling could likely be further refactored and improved, but again, it should not be a regression in the current state.

/cc @jkotas @tarekgh

@filipnavara filipnavara changed the title WIP: Convert System.Globalization.Native to C Convert System.Globalization.Native to C Feb 3, 2019
Copy link
Member

@jkotas jkotas left a comment

Choose a reason for hiding this comment

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

Looks great. Thank you!

@jkotas jkotas requested a review from tarekgh February 3, 2019 13:58
{
return false;
return FALSE;
Copy link
Member

Choose a reason for hiding this comment

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

C99 <stdbool.h> is used in multiple places in CoreFX ref, could it be used instead to save 3 bytes per instance, per call?

Copy link
Member Author

Choose a reason for hiding this comment

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

How does it actually save anything? In nearly all the instances it ends up in the registers which are not byte-sized anyway.

Copy link
Member Author

Choose a reason for hiding this comment

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

I removed usage of <stdbool.h> because it was mixed with the ICU's own UBool type and TRUE/FALSE values. There was really no need to mix two or three different boolean types.

Copy link
Member

Choose a reason for hiding this comment

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

Ok. true/false is unsigned char (integral constant macro in smallest range) in the std header, so I always tend to use it over TRUE/FALSE in Unix (and Windows, since VS2013). :)

callback(symbolBuf.data(), context);
if (symbolBuf != stackSymbolBuf)
{
free(symbolBuf);
Copy link
Member

Choose a reason for hiding this comment

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

free [](start = 12, length = 4)

would it be better to use the allocated buffer through the loop and then free only once after the loop done or when you need to reallocate?

this is just suggestion but you don't need to to do it if you think this will complicate the code especially I am expecting most of the symbols will fit on the stack buffer

Copy link
Member Author

@filipnavara filipnavara Feb 3, 2019

Choose a reason for hiding this comment

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

Most, if not all, of the things really fit into the stack buffer, so I don't expect the allocation path to be hit. As such it didn't make much sense to optimize it.

@tarekgh
Copy link
Member

tarekgh commented Feb 3, 2019

CC @janvorli

@jkotas jkotas merged commit a51af08 into dotnet:master Feb 4, 2019
@filipnavara filipnavara deleted the native_cpp_to_C branch February 4, 2019 08:06
}
else
{
pCollator = entry->second;
free(map);
pCollator = (*(TCollatorMap**)entry)->UCollator;
}

pthread_mutex_unlock(&pSortHandle->collatorsLockObject);
Copy link
Member

Choose a reason for hiding this comment

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

It seems the free(map) should be done after the if / else so that it is freed for both code paths.

Copy link
Member Author

Choose a reason for hiding this comment

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

The whole collatorsPerOption in SortHandle is unused too. I will go through the code and submit a PR to fix it. Thanks for noticing it.

Copy link
Member Author

@filipnavara filipnavara Feb 4, 2019

Choose a reason for hiding this comment

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

The tsearch adds it as a new node to a binary tree (tfind few lines above guarantees that's the case), so it is not a memory leak. The code unnecessarily does two lookups though. The memory is eventually reclaimed in GlobalizationNative_CloseSortHandle.

Copy link
Member

Choose a reason for hiding this comment

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

Ah, ok. Thank you for looking into it. I haven't realized the tsearch adds the new node.

filipnavara added a commit to filipnavara/coreclr that referenced this pull request Apr 18, 2019
Allocate initial array using malloc to avoid needlessly zeroing it
Reinstate optimization for empty array returned from GetCustomRules lost in dotnet#22378
filipnavara added a commit to filipnavara/coreclr that referenced this pull request Apr 19, 2019
Allocate initial array using malloc to avoid needlessly zeroing it
Reinstate optimization for empty array returned from GetCustomRules lost in dotnet#22378
jkotas pushed a commit that referenced this pull request Apr 25, 2019
…tions and GetCollatorFromSortHandle (#24100)

* Fix allocation size calculation when resizing array
Allocate initial array using malloc to avoid needlessly zeroing it
Reinstate optimization for empty array returned from GetCustomRules lost in #22378

* Avoid resizing arrays in GetCustomRules by preallocating the maximum size we can consume (5648 bytes)

* Avoid creating a binary search tree for something that could be easily stored as 32-entry lookup table

* Remove obsolete comment
picenka21 pushed a commit to picenka21/runtime that referenced this pull request Feb 18, 2022
Convert System.Globalization.Native to C

Commit migrated from dotnet/coreclr@a51af08
picenka21 pushed a commit to picenka21/runtime that referenced this pull request Feb 18, 2022
…tions and GetCollatorFromSortHandle (dotnet/coreclr#24100)

* Fix allocation size calculation when resizing array
Allocate initial array using malloc to avoid needlessly zeroing it
Reinstate optimization for empty array returned from GetCustomRules lost in dotnet/coreclr#22378

* Avoid resizing arrays in GetCustomRules by preallocating the maximum size we can consume (5648 bytes)

* Avoid creating a binary search tree for something that could be easily stored as 32-entry lookup table

* Remove obsolete comment


Commit migrated from dotnet/coreclr@a59ead8
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants