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

markup: microoptimise for many short filenames in directory #1534

Merged
merged 2 commits into from
May 9, 2017

Conversation

mappu
Copy link
Contributor

@mappu mappu commented Apr 23, 2017

Move strings.ToLower() after the early-return length check. This is a safe operation in all cases and should slightly improve directory listing performance when a directory contains many thousands of files with short filenames.

@lunny lunny added this to the 1.2.0 milestone Apr 23, 2017
@lunny lunny added the type/refactoring Existing code has been cleaned up. There should be no new functionality. label Apr 23, 2017
@lunny
Copy link
Member

lunny commented Apr 23, 2017

LGTM

@tboerger tboerger added the lgtm/need 1 This PR needs approval from one additional maintainer to be merged. label Apr 23, 2017
}

name = strings.ToLower(name)
if len(name) == 6 {
return name == "readme"
}
return name[:7] == "readme."
Copy link
Member

@sapk sapk Apr 23, 2017

Choose a reason for hiding this comment

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

Couldn't this be simplify by just return name == "readme" || name[:7] == "readme." and remove if len(name) == 6 bracket ?

Copy link
Member

Choose a reason for hiding this comment

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

Or just return/check name[:6] == "readme" this would also validate file like README_LANGUAGE.md ?

Copy link
Member

Choose a reason for hiding this comment

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

@mappu can you update the source code base on @sapk suggestions?

return name == "readme" || name[:7] == "readme."

@cez81
Copy link
Contributor

cez81 commented Apr 26, 2017

@mappu Try rebasing to latest master to pass Drone build

return name[:7] == "readme."

name = strings.ToLower(name)
return (name == "readme" || name[:7] == "readme.")
Copy link
Member

Choose a reason for hiding this comment

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

just return name == "readme" || name[:7] == "readme."

Copy link
Member

Choose a reason for hiding this comment

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

or just

func IsReadmeFile(name string) bool {
  return len(name) > 6 && strings.ToLower(name)[:6] == "readme"
}

I never really understood why we check readme. specifically...

Copy link
Member

@sapk sapk Apr 26, 2017

Choose a reason for hiding this comment

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

That what I said in my second comment, this would also validate file like README_LANGUAGE.md but maybe there is a reason ^^. I don't know in which case it is call exactly.

Copy link
Member

Choose a reason for hiding this comment

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

We only support the file name format like readme or readme.md.

Copy link
Member

Choose a reason for hiding this comment

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

@appleboy we don't really have an explicit list of files we support. :)

Copy link
Member

Choose a reason for hiding this comment

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

Yes. I think we could also support README_LANGUAGE if README is not exist.

Copy link
Member

Choose a reason for hiding this comment

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

But that's another PR's thing, this PR is a refactor.

Copy link
Member

Choose a reason for hiding this comment

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

@lunny @bkcsoft Yes. we can create another PR to discuss support list. This PR just a refactor.

@sapk
Copy link
Member

sapk commented Apr 30, 2017

@mappu Could you rebase like @cez81 suggest and make the change requested by @appleboy return (name == "readme" || name[:7] == "readme.") -> return name == "readme" || name[:7] == "readme." ?

After that you will have my L-G-T-M.

@lunny
Copy link
Member

lunny commented May 2, 2017

return name == "readme" || (len(name) > 6 && name[:7] == "readme.")

@sapk
Copy link
Member

sapk commented May 2, 2017

@lunny the goal of this PR is to do strings.ToLower(name) after checking length and not before. Your return will need to do it every time before.

@lunny
Copy link
Member

lunny commented May 2, 2017

@sapk but IsReadmeFile("readmf")?

@sapk
Copy link
Member

sapk commented May 2, 2017

@lunny I think, i didn't explain myself well enough.

What, I wanted to say is :

  • Check length (if > 6 return false)
  • toLower
  • return return name == "readme" || name[:7] == "readme."

That way we only do toLower if name length is good. That is the purpose of this PR.

Move strings.ToLower() after the early-return length check.

Your suggestion is good functionally but keep the same operation in a more simple way but not the micro-optimization that come with this re-ordering.

@mappu any news ?

@lunny
Copy link
Member

lunny commented May 2, 2017

@sapk I agree with you the three steps. I mean the last step should be return name == "readme" || (len(name) > 6 && name[:7] == "readme.") but not return name == "readme" || name[:7] == "readme." because readmf will result name[:7] panic.

@sapk
Copy link
Member

sapk commented May 2, 2017

@lunny good catch

@bkcsoft
Copy link
Member

bkcsoft commented May 2, 2017

which was why I recommended this

return len(name) >= 6 && strings.ToLower(name)[:6] == "readme"

@lunny
Copy link
Member

lunny commented May 2, 2017

@bkcsoft but readmes will be rendered? and readme will not be rendered?

@bkcsoft
Copy link
Member

bkcsoft commented May 2, 2017

@lunny sorry, that's len(name) >= 6. Updated 😂 And IMO we should not care what extension it is, if it begins with readme just render it :)

@appleboy
Copy link
Member

appleboy commented May 4, 2017

agree with @lunny solution.

@mappu mappu force-pushed the mappu-patch-isreadmefile branch from bf395d5 to 5e8f12c Compare May 7, 2017 02:29
@mappu mappu force-pushed the mappu-patch-isreadmefile branch from 5e8f12c to ac81c8f Compare May 7, 2017 02:45
mappu added 2 commits May 7, 2017 15:59
Move strings.ToLower() after the early-return length check. This is a safe operation in all cases and should slightly improve directory listing performance when a directory contains many thousands of files with short filenames.
@mappu mappu force-pushed the mappu-patch-isreadmefile branch from ac81c8f to aa36c4c Compare May 7, 2017 04:01
@mappu
Copy link
Contributor Author

mappu commented May 7, 2017

Thanks for the code review.

I restructured the patch to only make a smaller, more-targeted change to the original function, and added test case for readmf.

@sapk
Copy link
Member

sapk commented May 9, 2017

I would consider it still a improvement even if we should talk more (in other PR) about what this func should match this is not the goal of this PR.
LGTM
👍 for the test case.

@tboerger tboerger added lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. and removed lgtm/need 1 This PR needs approval from one additional maintainer to be merged. labels May 9, 2017
@lunny lunny merged commit fd76f09 into go-gitea:master May 9, 2017
bkcsoft pushed a commit that referenced this pull request Aug 23, 2017
) (#2241)

* Cleaning up public/ and documenting js/css libs.

This commit mostly addresses #1484 by moving vendor'ed plugins into a
vendor/ directory and documenting their upstream source and license in
vendor/librejs.html.

This also proves gitea is using only open source js/css libraries which
helps toward reaching #1524.

* Removing unused css file.

The version of this file in use is located at:
  vendor/plugins/highlight/github.css

* Cleaned up librejs.html and added javascript header

A SafeJS function was added to templates/helper.go to allow keeping
comments inside of javascript.

A javascript comment was added in the header of templates/base/head.tmpl
to mark all non-inline source as free.

The librejs.html file was updated to meet the current librejs spec. I
have now verified that the librejs plugin detects most of the scripts
included in gitea and suspect the non-free detections are the result of
a bug in the plugin. I believe this commit is enough to meet the C0.0
requirement of #1534.

* Updating SafeJS function per lint suggestion

* Added VERSIONS file, per request
@go-gitea go-gitea locked and limited conversation to collaborators Nov 23, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
lgtm/done This PR has enough approvals to get merged. There are no important open reservations anymore. type/refactoring Existing code has been cleaned up. There should be no new functionality.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants