Coder Social home page Coder Social logo

Question on Params about httprouter HOT 3 CLOSED

julienschmidt avatar julienschmidt commented on September 25, 2024
Question on Params

from httprouter.

Comments (3)

julienschmidt avatar julienschmidt commented on September 25, 2024 1

Good question. The hashing of the string is actually more expensive than just comparing it with the names.
Now we all learned that hash map lookups are O(1) and the comparing is O(n), but in this case n is very small, since n is just the number of parameters in the matched URL pattern. I can't think of a good URL structure with n ≥ 10, usually it is just 0 ≤ n ≤ 3.
And the hash map lookup is actually O(1+c), where c is a quite big constant in this case.

Moreover hash maps need a lot more memory.
The router initially used maps ;)

from httprouter.

jackspirou avatar jackspirou commented on September 25, 2024

@julienschmidt So glad you addressed the O(1) vs O(n). Great answer!

I had no idea about O(1+c) (for hash maps) probably being greater than O(n) where n is usually 0 ≤ n ≤ 3.

Are all hash maps usually a large c constant and O(1+c) or is this just a thing in Go?

Just curious. Thanks!

from httprouter.

jackspirou avatar jackspirou commented on September 25, 2024

@julienschmidt I noticed that Brad Fitzpatrick suggested using an array instead of a map for part of the HTTP2 golang implementation. Probably because that particular map only contains 10 elements. I wonder what the performance cutoff is in go for maps?

https://www.youtube.com/watch?v=gAfoLwog_MA&feature=youtu.be&t=21m18s

Well played!

from httprouter.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.