Optimize a script that processes large batches of text for tokenization and embedding generation. Implement the function below so repeated texts are tokenized and embedded once, processing respects the maximum batch token count, and results remain aligned with the original input order.
texts is a list of strings, token_vectors maps lowercase tokens to equal-length numeric vectors, and max_tokens is a positive batch limit. Return one averaged embedding per input text, using a zero vector when no token has a known vector.
def generate_embeddings(texts, token_vectors, max_tokens):