3 ms·
25000 (!!) allocations are made for every keystroke in the Omnibox. The Omnibox is no doubt far more complex than simple text box since entering characters int
by userbinator 12y ago
25000 (!!) allocations are made for every keystroke in the Omnibox.
The Omnibox is no doubt far more complex than simple text box since entering characters into it can invoke things like network connections (for search suggestions), but 25k allocs is still a bit on the excessive side.
Strings are an interesting case in that in general they are of indeterminate (and variable) length, which makes them somewhat difficult to accommodate in computer memory which is finite and allocated in fixed-length pieces. Abstractions like std::string have been created to make it simpler and easier to perform operations like appending, resizing, copying, and concenating, but I think this is part of the problem: by making these operations so easy and simple for the programmer, they're more inclined to overuse them instead of asking questions like "do I really need to create a copy just to modify one character? do I really need to append to this string? how long can it be?" Essentially, the abstraction encourages ignorance of the real nature of the actual operations, leading to more inefficient code. It only helps the programmer to perform these tedious operations more easily, and doesn't help at all with the decision of whether such tedious operations should be needed at all, which I think is more important; the first question when designing shouldn't be "what abstractions should I use to do X?", but "do I really need to do X, or is there are simpler way that doesn't need to?" The most efficient way to do something is to not do it at all.
Contrast this with a language like C, in which string operations are (unless the programmer writes or uses a library) far more explicit, and the programmer can be more aware of what his/her code is actually doing. That's why I believe every programmer who has to deal with strings should have at one point been exposed to implementing a resizable string buffer and/or length-delimited string library, to see the real nature of the problem (including how to do length management correctly.) Without this basic, low-level understanding of how to use memory, the advantages of all the other fancy string abstractions won't make much sense either.
- humanrebar 12y ago> Contrast this with a language like C, in which string operations are (unless the programmer writes or uses a library) far more explicit String operations are more explicit. Mostly. But some things (like sharing between threads, type conversion, and memory ownership) are very implicit and unsafe. Some of the implicitness to C programmers is so familiar that they don't notice it: /* 1. This function returns an error code * and not a size * 2. zero is success, nonzero is an error */ int /* 3. this method belongs in mylib * 4. this function supports ASCII??? * 5. this function allows memory to overlap? */ mylib_copy_first_n( /* 6. *out_s is allocated * 7. *out_s is greater than n bytes in size * 8. out_s will hold a null terminated string */ char const * out_s, /* 9. in_s is allocated * 10. *in_s holds at least n characters * 11. it's not a big deal if in_s has null characters */ char const * in_s, /* 10. n is not negative * 11. if n is zero, something sane will happen */ int n); I wouldn't hold up C as an example of explicitness. Now, this isn't a great example of C code, but even in the best examples of C code, there is a lot of correctness by convention and documentation. To be fair, I'm not aware of any languages and libraries that are fully explicit in type, behavior, and memory usage. Maybe Ada or one of the functional languages get it right. But there are a set of C++ tools that can make this sort of thing fast and correct. That's why it's disappointing that std::string isn't fast and is only mostly correct.
- pjmlp 12y agomylib: DEFINITIONS = BEGIN copy_first_n: PROCEDURE [VAR out_s: STRING, in_s: STRING, n: CARDINAL ] RETURNS [error: BOOLEAN]; END Mesa at Xerox PARC - 1979, one of the influences for strong type systems programming languages. But the 80's startups had to go build workstations based on UNIX.
- asveikau 12y agoNitpick: I don't think the output buffer would be const.