Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce string allocations in mimetype code #4440

Merged
merged 2 commits into from
Dec 7, 2023

Conversation

Jacalz
Copy link
Member

@Jacalz Jacalz commented Dec 6, 2023

Description:

The code handling mimetypes was using strings.Split() to split the string and that allocates. We can use strings.IndexByte() to get an index and subslice instead. I also refactored some code to avoid duplicating part of the logic.

The new internal/repository/mime package exists to avoid cyclic imports.

NOTE: The split logic is slightly different because a second / would be includes as part of the subtype now but as far as I can see from reading the spec (https://specifications.freedesktop.org/shared-mime-info-spec/shared-mime-info-spec-0.21.html) it should not be a problem as the format is "mainType/subType:weight" so there should not be any more separators, I think. I also did a ripgrep cat /usr/share/mime/globs2 | rg "\d\d:\w+/.+/" on my local mime database and nothing contains a second slash.

Checklist:

  • Tests included.
  • Lint and formatter run with no errors.
  • Tests all pass.

The code handling mimetypes was using strings.Split() to split the string and that allocates. We can use strings.IndexByte() to get an index and subslice instead. I also refactored some code to avoid duplicating mimetype split logic.

The new internal/repository/mime package exists to avoid cycling imports.

NOTE: The split logic is slightly different because a second / would be includes as part of the subtype now but as far as I can see from reading the spec (https://specifications.freedesktop.org/shared-mime-info-spec/shared-mime-info-spec-0.21.html) it should not be a problem as the format is "mainType/subType;weight" so there should not be any more separators.
@coveralls
Copy link

Coverage Status

coverage: 64.617% (+0.02%) from 64.595%
when pulling 7bc2e06 on Jacalz:noalloc-mime
into 798b13b on fyne-io:develop.

@Jacalz Jacalz merged commit 2750fc8 into fyne-io:develop Dec 7, 2023
11 of 12 checks passed
@Jacalz Jacalz deleted the noalloc-mime branch December 7, 2023 06:44
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.

3 participants