views:

259

answers:

5

Background

I am working on a phonetic converter program which converts english text into equivalant regional language text. Regional languages will have more characters than english letters and regional language fonts uses almost all positions (1-255) in a font.

My program supports different fonts and I have created a font class which will have methods gives access to the characters. This class will have 255 methods each representing each character. All these are marked as virtual so that a new font can override necessary character methods.

The methods in this font class are trivial. All methods are single line. Example would be

string StandardFont::consonant1(){
    return "a";
}

string StandardFont::consonant2(){
    return "b";
}

..

Questions

  1. Will 255 virtual functions in a single class make any performance issues? I know about vtable stuff, but I am not sure how much impact it has in this scenario.
  2. Can anyone suggest an alternate design for this class? Main design goal is to allow derived classes to override necessary methods. I have thought about adding the characters into containers like map or vector and providing method to get the character. But since I will have 255 items and this class is used frequently, I think each time I have to loop the container to get the character, which is again an issue.

Any thought?

+2  A: 

I suggest you use a standard encoding of non-ASCII (regional) characters.

A standard encoding is called "unicode", for example http://www.joelonsoftware.com/articles/Unicode.html

Anyway: to answer your questions ...

Will 255 virtual functions in a single class make any performance issues?

In a word: no, it won't.

But since I will have 255 items and this class is used frequently, I think each time I have to loop the container to get the character, which is again an issue.

With a vector or a fixed-length array whose length is 256, you wouldn't need to loop ... instead you could index directly, for example:

const char* translations[256] = {
 "a",
 "bee",
 "c!",
 ...etc...
};

const char* translate(char c)
{
  //use the character as an index into the array
  int index = c;
  //use the translation array (using indexing, not looping)
  const char* result = translations[index];
  return result;
}
ChrisW
Thanks Chris. I also have unicode. But this library is expected to work with ASCII fonts.
Appu
That looks promising. I will give a try. Many thanks
Appu
BTW, do you think I can use static polymorphism (using templates) here?
Appu
Re. using templates: can you give an example of what you're thinking of? You might, but I don't at the moment see why you would, nor what for: because somewhere/sometime you still need to translate **variable, run-time** data.
ChrisW
You are correct. I can't think about fitting a template here. Any way thanks for your time. It is much appreciated.
Appu
+2  A: 

255 virtual functions will not generally cause performance issues (except that each instance of your class will have a large VTable which will very marginally affect caching).

However, 255 virtual functions will generally cause a maintenance nightmare.

If I understand your description correctly, then what you need is:

1) Create a class representing a character in a regional language, probably with methods to return the image or whatever you need.

2) Create is a hierarchy of classes that represent character sets.

3) Every instance of a character set will maintain a mapping from positions to instances of the character class.

4) Have a function that gets the index and returns the object.

One benefit of this design is that you can have multiple character sets using some of the same glyphs (e.g., for numbers).

All that being said, why are you not using Unicode and 16 bit characters?

Uri
Thanks for the suggestion. I have unicode also. But this library is expected to work with ASCII characters.
Appu
Your understanding is almost correct. But point 1 and 2 is combined. One class represents all characters of a specific font.
Appu
No - each class will have a large VTable, each instance only uses a single VTable pointer that points to a common VTable for a given class.
Eclipse
Sorry, I intended to mean each subclass of the class, not each instance of the class. And an effect on caching is possible but very unlikely.
Uri
A: 

I think it would be cleaner to use a single method to access the character instead of 255 methods. Indexing/subscripting comes to mind.

Can you please clarify how these classes are to be used? Since the languages and alphabets are different, it seems strange to me that you would refer to more than one letter in the same way. Letters are, from every perspective, arbitrary. They will be different and unrelated in the different languages.

Unicode's goal is to provide a solution to your problem. Have you considered using it?

A: 

Addressing your question of speed, having 255 virtual methods shouldn't cause any particular performance penalty, although the usual advice applies: if you're not sure how it's going to perform, the only way to find out will be to benchmark it.

That being said, it's quite likely that there is a better way to approach the problem. It will help if you provide more details about what this font class is supposed to do.

Charlie
I have added a code sample. Please check it.
Appu
A: 

why not just have a vector of 255 characters?

each "font" simply installs different characters in the array? or even a Character class?

or you could use a map, or something else

255 methods is definitely not the way to go.

Keith Nicholas
I though about that. But I am not sure that it is more performant than virtual functions. I understand maintaining 255 functions is a pain.
Appu